Created
July 4, 2016 13:20
-
-
Save funivan/0753e71a38c3f2930f2be856586368d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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