vendor/symfony/twig-bridge/TokenParser/TransChoiceTokenParser.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig\TokenParser;
  11. use Symfony\Bridge\Twig\Node\TransNode;
  12. use Twig\Error\SyntaxError;
  13. use Twig\Node\Expression\AbstractExpression;
  14. use Twig\Node\Expression\ArrayExpression;
  15. use Twig\Node\Node;
  16. use Twig\Node\TextNode;
  17. use Twig\Token;
  18. use Twig\TokenParser\AbstractTokenParser;
  19. /**
  20.  * Token Parser for the 'transchoice' tag.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @deprecated since Symfony 4.2, use the "trans" tag with a "%count%" parameter instead
  25.  *
  26.  * @final since Symfony 4.4
  27.  */
  28. class TransChoiceTokenParser extends AbstractTokenParser
  29. {
  30.     /**
  31.      * {@inheritdoc}
  32.      *
  33.      * @return Node
  34.      */
  35.     public function parse(Token $token)
  36.     {
  37.         $lineno $token->getLine();
  38.         $stream $this->parser->getStream();
  39.         @trigger_error(sprintf('The "transchoice" tag is deprecated since Symfony 4.2, use the "trans" one instead with a "%%count%%" parameter in %s line %d.'$stream->getSourceContext()->getName(), $lineno), \E_USER_DEPRECATED);
  40.         $vars = new ArrayExpression([], $lineno);
  41.         $count $this->parser->getExpressionParser()->parseExpression();
  42.         $domain null;
  43.         $locale null;
  44.         if ($stream->test('with')) {
  45.             // {% transchoice count with vars %}
  46.             $stream->next();
  47.             $vars $this->parser->getExpressionParser()->parseExpression();
  48.         }
  49.         if ($stream->test('from')) {
  50.             // {% transchoice count from "messages" %}
  51.             $stream->next();
  52.             $domain $this->parser->getExpressionParser()->parseExpression();
  53.         }
  54.         if ($stream->test('into')) {
  55.             // {% transchoice count into "fr" %}
  56.             $stream->next();
  57.             $locale $this->parser->getExpressionParser()->parseExpression();
  58.         }
  59.         $stream->expect(Token::BLOCK_END_TYPE);
  60.         $body $this->parser->subparse([$this'decideTransChoiceFork'], true);
  61.         if (!$body instanceof TextNode && !$body instanceof AbstractExpression) {
  62.             throw new SyntaxError('A message inside a transchoice tag must be a simple text.'$body->getTemplateLine(), $stream->getSourceContext());
  63.         }
  64.         $stream->expect(Token::BLOCK_END_TYPE);
  65.         return new TransNode($body$domain$count$vars$locale$lineno$this->getTag());
  66.     }
  67.     public function decideTransChoiceFork($token)
  68.     {
  69.         return $token->test(['endtranschoice']);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      *
  74.      * @return string
  75.      */
  76.     public function getTag()
  77.     {
  78.         return 'transchoice';
  79.     }
  80. }