/**
* @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;
}