Symfony\Component\CssSelector\XPathExpr::xpathLiteral PHP Method

xpathLiteral() public static method

public static xpathLiteral ( $s )
    public static function xpathLiteral($s)
    {
        if ($s instanceof Node\ElementNode) {
            // This is probably a symbol that looks like an expression...
            $s = $s->formatElement();
        } else {
            $s = (string) $s;
        }
        if (false === strpos($s, "'")) {
            return sprintf("'%s'", $s);
        }
        if (false === strpos($s, '"')) {
            return sprintf('"%s"', $s);
        }
        $string = $s;
        $parts = array();
        while (true) {
            if (false !== ($pos = strpos($string, "'"))) {
                $parts[] = sprintf("'%s'", substr($string, 0, $pos));
                $parts[] = "\"'\"";
                $string = substr($string, $pos + 1);
            } else {
                $parts[] = "'{$string}'";
                break;
            }
        }
        return sprintf('concat(%s)', implode($parts, ', '));
    }

Usage Example

Example #1
0
 /**
  * @throws SyntaxError When unknown operator is found
  */
 public function toXpath()
 {
     $path = $this->selector->toXpath();
     $attrib = $this->xpathAttrib();
     $value = $this->value;
     if ($this->operator == 'exists') {
         $path->addCondition($attrib);
     } elseif ($this->operator == '=') {
         $path->addCondition(sprintf('%s = %s', $attrib, XPathExpr::xpathLiteral($value)));
     } elseif ($this->operator == '!=') {
         // FIXME: this seems like a weird hack...
         if ($value) {
             $path->addCondition(sprintf('not(%s) or %s != %s', $attrib, $attrib, XPathExpr::xpathLiteral($value)));
         } else {
             $path->addCondition(sprintf('%s != %s', $attrib, XPathExpr::xpathLiteral($value)));
         }
         // path.addCondition('%s != %s' % (attrib, xpathLiteral(value)))
     } elseif ($this->operator == '~=') {
         $path->addCondition(sprintf("contains(concat(' ', normalize-space(%s), ' '), %s)", $attrib, XPathExpr::xpathLiteral(' ' . $value . ' ')));
     } elseif ($this->operator == '|=') {
         // Weird, but true...
         $path->addCondition(sprintf('%s = %s or starts-with(%s, %s)', $attrib, XPathExpr::xpathLiteral($value), $attrib, XPathExpr::xpathLiteral($value . '-')));
     } elseif ($this->operator == '^=') {
         $path->addCondition(sprintf('starts-with(%s, %s)', $attrib, XPathExpr::xpathLiteral($value)));
     } elseif ($this->operator == '$=') {
         // Oddly there is a starts-with in XPath 1.0, but not ends-with
         $path->addCondition(sprintf('substring(%s, string-length(%s)-%s) = %s', $attrib, $attrib, strlen($value) - 1, XPathExpr::xpathLiteral($value)));
     } elseif ($this->operator == '*=') {
         // FIXME: case sensitive?
         $path->addCondition(sprintf('contains(%s, %s)', $attrib, XPathExpr::xpathLiteral($value)));
     } else {
         throw new SyntaxError(sprintf('Unknown operator: %s', $this->operator));
     }
     return $path;
 }
All Usage Examples Of Symfony\Component\CssSelector\XPathExpr::xpathLiteral