4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\TokenParser
;
14 use Symfony\Bridge\Twig\Node\TransNode
;
17 * Token Parser for the 'transchoice' tag.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class TransChoiceTokenParser
extends TransTokenParser
24 * Parses a token and returns a node.
26 * @param \Twig_Token $token A Twig_Token instance
28 * @return \Twig_NodeInterface A Twig_NodeInterface instance
30 * @throws \Twig_Error_Syntax
32 public function parse(\Twig_Token
$token)
34 $lineno = $token->getLine();
35 $stream = $this->parser
->getStream();
37 $vars = new \
Twig_Node_Expression_Array(array(), $lineno);
39 $count = $this->parser
->getExpressionParser()->parseExpression();
44 if ($stream->test('with')) {
45 // {% transchoice count with vars %}
47 $vars = $this->parser
->getExpressionParser()->parseExpression();
50 if ($stream->test('from')) {
51 // {% transchoice count from "messages" %}
53 $domain = $this->parser
->getExpressionParser()->parseExpression();
56 if ($stream->test('into')) {
57 // {% transchoice count into "fr" %}
59 $locale = $this->parser
->getExpressionParser()->parseExpression();
62 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
);
64 $body = $this->parser
->subparse(array($this, 'decideTransChoiceFork'), true);
66 if (!$body instanceof \Twig_Node_Text
&& !$body instanceof \Twig_Node_Expression
) {
67 throw new \
Twig_Error_Syntax('A message must be a simple text.');
70 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
);
72 return new TransNode($body, $domain, $count, $vars, $locale, $lineno, $this->getTag());
75 public function decideTransChoiceFork($token)
77 return $token->test(array('endtranschoice'));
81 * Gets the tag name associated with this token parser.
83 * @return string The tag name
85 public function getTag()