| @@ 24-47 (lines=24) @@ | ||
| 21 | use TwigJs\JsCompiler; |
|
| 22 | use TwigJs\TypeCompilerInterface; |
|
| 23 | ||
| 24 | class AddCompiler implements TypeCompilerInterface |
|
| 25 | { |
|
| 26 | public function getType() |
|
| 27 | { |
|
| 28 | return 'Twig_Node_Expression_Binary_Add'; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
| 32 | { |
|
| 33 | if (!$node instanceof \Twig_Node_Expression_Binary) { |
|
| 34 | throw new \RuntimeException( |
|
| 35 | sprintf('$node must be an instanceof of \Twig_Node_Expression_Binary, but got "%s".', get_class($node)) |
|
| 36 | ); |
|
| 37 | } |
|
| 38 | ||
| 39 | $compiler |
|
| 40 | ->raw('(Number(') |
|
| 41 | ->subcompile($node->getNode('left')) |
|
| 42 | ->raw(') + Number(') |
|
| 43 | ->subcompile($node->getNode('right')) |
|
| 44 | ->raw('))') |
|
| 45 | ; |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| @@ 24-53 (lines=30) @@ | ||
| 21 | use TwigJs\JsCompiler; |
|
| 22 | use TwigJs\TypeCompilerInterface; |
|
| 23 | ||
| 24 | class InCompiler implements TypeCompilerInterface |
|
| 25 | { |
|
| 26 | public function getType() |
|
| 27 | { |
|
| 28 | return 'Twig_Node_Expression_Binary_In'; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
| 32 | { |
|
| 33 | if (!$node instanceof \Twig_Node_Expression_Binary_In) { |
|
| 34 | throw new \RuntimeException( |
|
| 35 | sprintf( |
|
| 36 | '$node must be an instanceof of \Twig_Node_Expression_Binary_In, but got "%s".', |
|
| 37 | get_class($node) |
|
| 38 | ) |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | // order left,right is reversed in JsCompiler compared to Twig_Compiler |
|
| 43 | // since the twig.contains filter takes reverse arguments to follow |
|
| 44 | // Google Closure conventions |
|
| 45 | $compiler |
|
| 46 | ->raw('twig.contains(') |
|
| 47 | ->subcompile($node->getNode('right')) |
|
| 48 | ->raw(', ') |
|
| 49 | ->subcompile($node->getNode('left')) |
|
| 50 | ->raw(')') |
|
| 51 | ; |
|
| 52 | } |
|
| 53 | } |
|
| 54 | ||
| @@ 24-51 (lines=28) @@ | ||
| 21 | use TwigJs\JsCompiler; |
|
| 22 | use TwigJs\TypeCompilerInterface; |
|
| 23 | ||
| 24 | class NotInCompiler implements TypeCompilerInterface |
|
| 25 | { |
|
| 26 | public function getType() |
|
| 27 | { |
|
| 28 | return 'Twig_Node_Expression_Binary_NotIn'; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
| 32 | { |
|
| 33 | if (!$node instanceof \Twig_Node_Expression_Binary_NotIn) { |
|
| 34 | throw new \RuntimeException( |
|
| 35 | sprintf( |
|
| 36 | '$node must be an instanceof of \Twig_Node_Expression_Binary_NotIn, but got "%s".', |
|
| 37 | get_class($node) |
|
| 38 | ) |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | // order of arguments is reversed, see the InCompiler |
|
| 43 | $compiler |
|
| 44 | ->raw('!twig.contains(') |
|
| 45 | ->subcompile($node->getNode('right')) |
|
| 46 | ->raw(', ') |
|
| 47 | ->subcompile($node->getNode('left')) |
|
| 48 | ->raw(')') |
|
| 49 | ; |
|
| 50 | } |
|
| 51 | } |
|
| 52 | ||
| @@ 24-50 (lines=27) @@ | ||
| 21 | use TwigJs\JsCompiler; |
|
| 22 | use TwigJs\TypeCompilerInterface; |
|
| 23 | ||
| 24 | class PowerCompiler implements TypeCompilerInterface |
|
| 25 | { |
|
| 26 | public function getType() |
|
| 27 | { |
|
| 28 | return 'Twig_Node_Expression_Binary_Power'; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
| 32 | { |
|
| 33 | if (!$node instanceof \Twig_Node_Expression_Binary_Power) { |
|
| 34 | throw new \RuntimeException( |
|
| 35 | sprintf( |
|
| 36 | '$node must be an instanceof of \Twig_Node_Expression_Binary_Power, but got "%s".', |
|
| 37 | get_class($node) |
|
| 38 | ) |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | $compiler |
|
| 43 | ->raw('Math.pow(') |
|
| 44 | ->subcompile($this->getNode('left')) |
|
| 45 | ->raw(', ') |
|
| 46 | ->subcompile($this->getNode('right')) |
|
| 47 | ->raw(')') |
|
| 48 | ; |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| @@ 24-50 (lines=27) @@ | ||
| 21 | use TwigJs\JsCompiler; |
|
| 22 | use TwigJs\TypeCompilerInterface; |
|
| 23 | ||
| 24 | class RangeCompiler implements TypeCompilerInterface |
|
| 25 | { |
|
| 26 | public function getType() |
|
| 27 | { |
|
| 28 | return 'Twig_Node_Expression_Binary_Range'; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
| 32 | { |
|
| 33 | if (!$node instanceof \Twig_Node_Expression_Binary_Range) { |
|
| 34 | throw new \RuntimeException( |
|
| 35 | sprintf( |
|
| 36 | '$node must be an instanceof of \Twig_Node_Expression_Binary_Range, but got "%s".', |
|
| 37 | get_class($node) |
|
| 38 | ) |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | $compiler |
|
| 43 | ->raw('twig.range(') |
|
| 44 | ->subcompile($node->getNode('left')) |
|
| 45 | ->raw(', ') |
|
| 46 | ->subcompile($node->getNode('right')) |
|
| 47 | ->raw(')') |
|
| 48 | ; |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||