public function parseTestExpression(Twig_Parser $parser, $node) { $stream = $parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE); $arguments = null; if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { $arguments = $parser->getExpressionParser()->parseArguments(); } return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine()); }
public function parseTestExpression(Twig_Parser $parser, $node) { $stream = $parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); $arguments = null; if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { $arguments = $parser->getExpressionParser()->parseArguments(); } $class = $this->getTestNodeClass($parser->getEnvironment(), $name); return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine()); }
protected function getTestName(Twig_Parser $parser, $line) { $stream = $parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); $env = $parser->getEnvironment(); $testMap = $env->getTests(); if (isset($testMap[$name])) { return $name; } if ($stream->test(Twig_Token::NAME_TYPE)) { // try 2-words tests $name = $name . ' ' . $parser->getCurrentToken()->getValue(); if (isset($testMap[$name])) { $parser->getStream()->next(); return $name; } } $message = sprintf('The test "%s" does not exist', $name); if ($alternatives = $env->computeAlternatives($name, array_keys($testMap))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $line, $parser->getFilename()); }
protected function getTestNodeClass(Twig_Parser $parser, $name, $line) { $env = $parser->getEnvironment(); $testMap = $env->getTests(); $testName = null; if (isset($testMap[$name])) { $testName = $name; } elseif ($parser->getStream()->test(Twig_Token::NAME_TYPE)) { // try 2-words tests $name = $name . ' ' . $parser->getCurrentToken()->getValue(); if (isset($testMap[$name])) { $parser->getStream()->next(); $testName = $name; } } if (null === $testName) { $message = sprintf('The test "%s" does not exist', $name); if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $line, $parser->getFilename()); } if ($testMap[$name] instanceof Twig_SimpleTest) { return $testMap[$name]->getNodeClass(); } return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test'; }
protected function getTest(Twig_Parser $parser, $line) { $stream = $parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); $env = $parser->getEnvironment(); if ($test = $env->getTest($name)) { return array($name, $test); } if ($stream->test(Twig_Token::NAME_TYPE)) { // try 2-words tests $name = $name . ' ' . $parser->getCurrentToken()->getValue(); if ($test = $env->getTest($name)) { $parser->getStream()->next(); return array($name, $test); } } $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename()); $e->addSuggestions($name, array_keys($env->getTests())); throw $e; }