TokenParserTrans.php 2.2 KB

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