|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bridge\Twig\TokenParser; |
|
|
13 |
|
|
|
14 |
use Symfony\Bridge\Twig\Node\TransNode; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
*/ |
|
|
21 |
class TransChoiceTokenParser extends TransTokenParser |
|
|
22 |
{ |
|
|
23 |
/** |
|
|
24 |
* Parses a token and returns a node. |
|
|
25 |
* |
|
|
26 |
* @param \Twig_Token $token A Twig_Token instance |
|
|
27 |
* |
|
|
28 |
* @return \Twig_NodeInterface A Twig_NodeInterface instance |
|
|
29 |
*/ |
|
|
30 |
public function parse(\Twig_Token $token) |
|
|
31 |
{ |
|
|
32 |
$lineno = $token->getLine(); |
|
|
33 |
$stream = $this->parser->getStream(); |
|
|
34 |
|
|
|
35 |
$vars = new \Twig_Node_Expression_Array(array(), $lineno); |
|
|
36 |
|
|
|
37 |
$count = $this->parser->getExpressionParser()->parseExpression(); |
|
|
38 |
|
|
|
39 |
$domain = new \Twig_Node_Expression_Constant('messages', $lineno); |
|
|
40 |
$locale = null; |
|
|
41 |
|
|
|
42 |
if ($stream->test('with')) { |
|
|
43 |
// {% transchoice count with vars %} |
|
|
44 |
$stream->next(); |
|
|
45 |
$vars = $this->parser->getExpressionParser()->parseExpression(); |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
if ($stream->test('from')) { |
|
|
49 |
// {% transchoice count from "messages" %} |
|
|
50 |
$stream->next(); |
|
|
51 |
$domain = $this->parser->getExpressionParser()->parseExpression(); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
if ($stream->test('into')) { |
|
|
55 |
// {% transchoice count into "fr" %} |
|
|
56 |
$stream->next(); |
|
|
57 |
$locale = $this->parser->getExpressionParser()->parseExpression(); |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
|
|
61 |
|
|
|
62 |
$body = $this->parser->subparse(array($this, 'decideTransChoiceFork'), true); |
|
|
63 |
|
|
|
64 |
if (!$body instanceof \Twig_Node_Text && !$body instanceof \Twig_Node_Expression) { |
|
|
65 |
throw new \Twig_Error_Syntax('A message must be a simple text.'); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
|
|
69 |
|
|
|
70 |
return new TransNode($body, $domain, $count, $vars, $locale, $lineno, $this->getTag()); |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
public function decideTransChoiceFork($token) |
|
|
74 |
{ |
|
|
75 |
return $token->test(array('endtranschoice')); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
/** |
|
|
79 |
* Gets the tag name associated with this token parser. |
|
|
80 |
* |
|
|
81 |
* @return string The tag name |
|
|
82 |
*/ |
|
|
83 |
public function getTag() |
|
|
84 |
{ |
|
|
85 |
return 'transchoice'; |
|
|
86 |
} |
|
|
87 |
} |