TokenParserTrans.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold PhpMyAdmin\Twig\I18n\TokenParserTrans class
  5. *
  6. * @package PhpMyAdmin\Twig\I18n
  7. */
  8. namespace PhpMyAdmin\Twig\I18n;
  9. use Twig\Extensions\TokenParser\TransTokenParser;
  10. use Twig\Token;
  11. /**
  12. * Class TokenParserTrans
  13. *
  14. * @package PhpMyAdmin\Twig\I18n
  15. */
  16. class TokenParserTrans extends TransTokenParser
  17. {
  18. /**
  19. * Parses a token and returns a node.
  20. *
  21. * @param Token $token Twig token to parse
  22. *
  23. * @return Twig_NodeInterface
  24. *
  25. * @throws \Twig\Error\SyntaxError
  26. */
  27. public function parse(Token $token)
  28. {
  29. $lineno = $token->getLine();
  30. $stream = $this->parser->getStream();
  31. $count = null;
  32. $plural = null;
  33. $notes = null;
  34. $context = null;
  35. if (!$stream->test(Token::BLOCK_END_TYPE)) {
  36. $body = $this->parser->getExpressionParser()->parseExpression();
  37. } else {
  38. $stream->expect(Token::BLOCK_END_TYPE);
  39. $body = $this->parser->subparse(array($this, 'decideForFork'));
  40. $next = $stream->next()->getValue();
  41. if ('plural' === $next) {
  42. $count = $this->parser->getExpressionParser()->parseExpression();
  43. $stream->expect(Token::BLOCK_END_TYPE);
  44. $plural = $this->parser->subparse(array($this, 'decideForFork'));
  45. if ('notes' === $stream->next()->getValue()) {
  46. $stream->expect(Token::BLOCK_END_TYPE);
  47. $notes = $this->parser->subparse(array($this, 'decideForEnd'), true);
  48. }
  49. } elseif ('context' === $next) {
  50. $stream->expect(Token::BLOCK_END_TYPE);
  51. $context = $this->parser->subparse(array($this, 'decideForEnd'), true);
  52. } elseif ('notes' === $next) {
  53. $stream->expect(Token::BLOCK_END_TYPE);
  54. $notes = $this->parser->subparse(array($this, 'decideForEnd'), true);
  55. }
  56. }
  57. $stream->expect(Token::BLOCK_END_TYPE);
  58. $this->checkTransString($body, $lineno);
  59. return new NodeTrans($body, $plural, $count, $context, $notes, $lineno, $this->getTag());
  60. }
  61. /**
  62. * Tests the current token for a type.
  63. *
  64. * @param Token $token Twig token to test
  65. *
  66. * @return bool
  67. */
  68. public function decideForFork(Token $token)
  69. {
  70. return $token->test(array('plural', 'context', 'notes', 'endtrans'));
  71. }
  72. }