vendor/symfony/expression-language/Node/ArrayNode.php line 30

  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\Component\ExpressionLanguage\Node;
  11. use Symfony\Component\ExpressionLanguage\Compiler;
  12. /**
  13.  * @author Fabien Potencier <fabien@symfony.com>
  14.  *
  15.  * @internal
  16.  */
  17. class ArrayNode extends Node
  18. {
  19.     protected $index;
  20.     public function __construct()
  21.     {
  22.         $this->index = -1;
  23.     }
  24.     public function addElement(Node $valueNode $key null)
  25.     {
  26.         $key ??= new ConstantNode(++$this->index);
  27.         array_push($this->nodes$key$value);
  28.     }
  29.     /**
  30.      * Compiles the node to PHP.
  31.      */
  32.     public function compile(Compiler $compiler)
  33.     {
  34.         $compiler->raw('[');
  35.         $this->compileArguments($compiler);
  36.         $compiler->raw(']');
  37.     }
  38.     public function evaluate(array $functions, array $values)
  39.     {
  40.         $result = [];
  41.         foreach ($this->getKeyValuePairs() as $pair) {
  42.             $result[$pair['key']->evaluate($functions$values)] = $pair['value']->evaluate($functions$values);
  43.         }
  44.         return $result;
  45.     }
  46.     public function toArray()
  47.     {
  48.         $value = [];
  49.         foreach ($this->getKeyValuePairs() as $pair) {
  50.             $value[$pair['key']->attributes['value']] = $pair['value'];
  51.         }
  52.         $array = [];
  53.         if ($this->isHash($value)) {
  54.             foreach ($value as $k => $v) {
  55.                 $array[] = ', ';
  56.                 $array[] = new ConstantNode($k);
  57.                 $array[] = ': ';
  58.                 $array[] = $v;
  59.             }
  60.             $array[0] = '{';
  61.             $array[] = '}';
  62.         } else {
  63.             foreach ($value as $v) {
  64.                 $array[] = ', ';
  65.                 $array[] = $v;
  66.             }
  67.             $array[0] = '[';
  68.             $array[] = ']';
  69.         }
  70.         return $array;
  71.     }
  72.     protected function getKeyValuePairs()
  73.     {
  74.         $pairs = [];
  75.         foreach (array_chunk($this->nodes2) as $pair) {
  76.             $pairs[] = ['key' => $pair[0], 'value' => $pair[1]];
  77.         }
  78.         return $pairs;
  79.     }
  80.     protected function compileArguments(Compiler $compilerbool $withKeys true)
  81.     {
  82.         $first true;
  83.         foreach ($this->getKeyValuePairs() as $pair) {
  84.             if (!$first) {
  85.                 $compiler->raw(', ');
  86.             }
  87.             $first false;
  88.             if ($withKeys) {
  89.                 $compiler
  90.                     ->compile($pair['key'])
  91.                     ->raw(' => ')
  92.                 ;
  93.             }
  94.             $compiler->compile($pair['value']);
  95.         }
  96.     }
  97. }