|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpMyAdmin\Twig\Node\Expression; |
| 6 | + |
| 7 | +use Twig\Compiler; |
| 8 | +use Twig\Error\SyntaxError; |
| 9 | +use Twig\Node\Expression\AbstractExpression; |
| 10 | +use Twig\Node\Expression\ConstantExpression; |
| 11 | +use Twig\Node\Node; |
| 12 | + |
| 13 | +use function in_array; |
| 14 | +use function is_string; |
| 15 | +use function sprintf; |
| 16 | +use function str_replace; |
| 17 | +use function trim; |
| 18 | + |
| 19 | +final class TransExpression extends AbstractExpression |
| 20 | +{ |
| 21 | + public function __construct(string $name, Node $arguments, int $lineno) |
| 22 | + { |
| 23 | + parent::__construct(['arguments' => $arguments], ['name' => $name, 'is_defined_test' => false], $lineno); |
| 24 | + } |
| 25 | + |
| 26 | + /** @throws SyntaxError */ |
| 27 | + public function compile(Compiler $compiler): void |
| 28 | + { |
| 29 | + $parameters = $this->getParameters(); |
| 30 | + |
| 31 | + if (isset($parameters['notes'])) { |
| 32 | + $this->compileNotes($compiler, $parameters); |
| 33 | + } |
| 34 | + |
| 35 | + if ( |
| 36 | + isset($parameters['singular']) |
| 37 | + || isset($parameters['plural']) |
| 38 | + || isset($parameters['count']) |
| 39 | + || isset($parameters[1]) |
| 40 | + || isset($parameters[2]) |
| 41 | + ) { |
| 42 | + $this->compilePlural($compiler, $parameters); |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (isset($parameters['context'])) { |
| 48 | + $this->compileContext($compiler, $parameters); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $this->compileMessage($compiler, $parameters); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return Node[] |
| 58 | + * |
| 59 | + * @throws SyntaxError |
| 60 | + */ |
| 61 | + private function getParameters(): array |
| 62 | + { |
| 63 | + $parameters = []; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var int|string $name |
| 67 | + * @var Node $argument |
| 68 | + */ |
| 69 | + foreach ($this->getNode('arguments') as $name => $argument) { |
| 70 | + if (! in_array($name, [0, 1, 2, 'message', 'singular', 'plural', 'count', 'context', 'notes'], true)) { |
| 71 | + throw $this->unknownArgumentSyntaxError($name); |
| 72 | + } |
| 73 | + |
| 74 | + $parameters[$name] = $argument; |
| 75 | + } |
| 76 | + |
| 77 | + return $parameters; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param Node[] $parameters |
| 82 | + * |
| 83 | + * @throws SyntaxError |
| 84 | + */ |
| 85 | + private function compileNotes(Compiler $compiler, array $parameters): void |
| 86 | + { |
| 87 | + $notes = $this->getStringFromNode('notes', $parameters['notes'] ?? null); |
| 88 | + |
| 89 | + // line breaks are not allowed because we want a single line comment |
| 90 | + $notes = trim(str_replace(["\n", "\r"], ' ', $notes)); |
| 91 | + if ($notes === '') { |
| 92 | + throw $this->nonEmptyLiteralStringSyntaxError('notes'); |
| 93 | + } |
| 94 | + |
| 95 | + $compiler->raw("\n// l10n: " . $notes . "\n"); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param Node[] $parameters |
| 100 | + * |
| 101 | + * @throws SyntaxError |
| 102 | + */ |
| 103 | + private function compilePlural(Compiler $compiler, array $parameters): void |
| 104 | + { |
| 105 | + if (isset($parameters[0], $parameters['singular'])) { |
| 106 | + throw $this->duplicateArgumentSyntaxError('singular'); |
| 107 | + } |
| 108 | + |
| 109 | + if (isset($parameters[1], $parameters['plural'])) { |
| 110 | + throw $this->duplicateArgumentSyntaxError('plural'); |
| 111 | + } |
| 112 | + |
| 113 | + if (isset($parameters[2], $parameters['count'])) { |
| 114 | + throw $this->duplicateArgumentSyntaxError('count'); |
| 115 | + } |
| 116 | + |
| 117 | + $singular = $this->getStringFromNode('singular', $parameters[0] ?? $parameters['singular'] ?? null); |
| 118 | + $plural = $this->getStringFromNode('plural', $parameters[1] ?? $parameters['plural'] ?? null); |
| 119 | + $count = $parameters[2] ?? $parameters['count'] ?? null; |
| 120 | + if ($count === null) { |
| 121 | + throw $this->missingArgumentSyntaxError('count'); |
| 122 | + } |
| 123 | + |
| 124 | + $compiler->raw('\\_ngettext('); |
| 125 | + $compiler->string($singular); |
| 126 | + $compiler->raw(', '); |
| 127 | + $compiler->string($plural); |
| 128 | + $compiler->raw(', '); |
| 129 | + $compiler->subcompile($count); |
| 130 | + $compiler->raw(isset($parameters['notes']) ? ")\n" : ')'); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param Node[] $parameters |
| 135 | + * |
| 136 | + * @throws SyntaxError |
| 137 | + */ |
| 138 | + private function compileContext(Compiler $compiler, array $parameters): void |
| 139 | + { |
| 140 | + if (isset($parameters[0], $parameters['message'])) { |
| 141 | + throw $this->duplicateArgumentSyntaxError('message'); |
| 142 | + } |
| 143 | + |
| 144 | + $message = $this->getStringFromNode('message', $parameters[0] ?? $parameters['message'] ?? null); |
| 145 | + $context = $this->getStringFromNode('context', $parameters['context'] ?? null); |
| 146 | + |
| 147 | + $compiler->raw('\\_pgettext('); |
| 148 | + $compiler->string($context); |
| 149 | + $compiler->raw(', '); |
| 150 | + $compiler->string($message); |
| 151 | + $compiler->raw(isset($parameters['notes']) ? ")\n" : ')'); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param Node[] $parameters |
| 156 | + * |
| 157 | + * @throws SyntaxError |
| 158 | + */ |
| 159 | + private function compileMessage(Compiler $compiler, array $parameters): void |
| 160 | + { |
| 161 | + if (isset($parameters[0], $parameters['message'])) { |
| 162 | + throw $this->duplicateArgumentSyntaxError('message'); |
| 163 | + } |
| 164 | + |
| 165 | + $message = $this->getStringFromNode('message', $parameters[0] ?? $parameters['message'] ?? null); |
| 166 | + |
| 167 | + $compiler->raw('\\_gettext('); |
| 168 | + $compiler->string($message); |
| 169 | + $compiler->raw(isset($parameters['notes']) ? ")\n" : ')'); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @psalm-return non-empty-string |
| 174 | + * |
| 175 | + * @throws SyntaxError |
| 176 | + */ |
| 177 | + private function getStringFromNode(int|string $name, Node|null $node): string |
| 178 | + { |
| 179 | + if (! ($node instanceof ConstantExpression)) { |
| 180 | + throw $this->nonEmptyLiteralStringSyntaxError($name); |
| 181 | + } |
| 182 | + |
| 183 | + $value = $node->getAttribute('value'); |
| 184 | + if (! is_string($value) || $value === '') { |
| 185 | + throw $this->nonEmptyLiteralStringSyntaxError($name); |
| 186 | + } |
| 187 | + |
| 188 | + return $value; |
| 189 | + } |
| 190 | + |
| 191 | + private function unknownArgumentSyntaxError(int|string $name): SyntaxError |
| 192 | + { |
| 193 | + return new SyntaxError( |
| 194 | + sprintf('Unknown argument "%s".', $name), |
| 195 | + $this->getTemplateLine(), |
| 196 | + $this->getSourceContext(), |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + private function duplicateArgumentSyntaxError(int|string $name): SyntaxError |
| 201 | + { |
| 202 | + return new SyntaxError( |
| 203 | + sprintf('Argument "%s" is defined twice.', $name), |
| 204 | + $this->getTemplateLine(), |
| 205 | + $this->getSourceContext(), |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + private function nonEmptyLiteralStringSyntaxError(int|string $name): SyntaxError |
| 210 | + { |
| 211 | + return new SyntaxError( |
| 212 | + sprintf('Value for argument "%s" must be a non-empty literal string.', $name), |
| 213 | + $this->getTemplateLine(), |
| 214 | + $this->getSourceContext(), |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + private function missingArgumentSyntaxError(int|string $name): SyntaxError |
| 219 | + { |
| 220 | + return new SyntaxError( |
| 221 | + sprintf('Value for argument "%s" is required.', $name), |
| 222 | + $this->getTemplateLine(), |
| 223 | + $this->getSourceContext(), |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
0 commit comments