Skip to content

Instantly share code, notes, and snippets.

@funivan
Created July 4, 2016 13:20
Show Gist options
  • Select an option

  • Save funivan/0753e71a38c3f2930f2be856586368d8 to your computer and use it in GitHub Desktop.

Select an option

Save funivan/0753e71a38c3f2930f2be856586368d8 to your computer and use it in GitHub Desktop.
class EqExtension extends AbstractExtension {
/**
* Returns extension name.
*
* @return string
*/
public function getName() {
return 'function';
}
/**
* {@inheritdoc}
*/
public function getFunctionTranslators() {
return [
'eq' => [$this, 'translatePosition'],
];
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translatePosition(XPathExpr $xpath, FunctionNode $function) {
$arguments = $function->getArguments();
if (count($arguments) !== 1) {
throw new ExpressionErrorException('Expected 1 one argument for the function :eq()');
}
$firstArgument = (int) $arguments[0]->getValue();
if (!is_int($firstArgument)) {
throw new ExpressionErrorException('Expected position to be int');
}
return $xpath->addCondition(sprintf('position() = %s', $firstArgument));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment