| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace ElementorDeps\Twig\Node\Expression; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Compiler; |
| 14 |
class ArrayExpression extends AbstractExpression |
| 15 |
{ |
| 16 |
private $index; |
| 17 |
public function __construct(array $elements, int $lineno) |
| 18 |
{ |
| 19 |
parent::__construct($elements, [], $lineno); |
| 20 |
$this->index = -1; |
| 21 |
foreach ($this->getKeyValuePairs() as $pair) { |
| 22 |
if ($pair['key'] instanceof ConstantExpression && \ctype_digit((string) $pair['key']->getAttribute('value')) && $pair['key']->getAttribute('value') > $this->index) { |
| 23 |
$this->index = $pair['key']->getAttribute('value'); |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
public function getKeyValuePairs() : array |
| 28 |
{ |
| 29 |
$pairs = []; |
| 30 |
foreach (\array_chunk($this->nodes, 2) as $pair) { |
| 31 |
$pairs[] = ['key' => $pair[0], 'value' => $pair[1]]; |
| 32 |
} |
| 33 |
return $pairs; |
| 34 |
} |
| 35 |
public function hasElement(AbstractExpression $key) : bool |
| 36 |
{ |
| 37 |
foreach ($this->getKeyValuePairs() as $pair) { |
| 38 |
// we compare the string representation of the keys |
| 39 |
// to avoid comparing the line numbers which are not relevant here. |
| 40 |
if ((string) $key === (string) $pair['key']) { |
| 41 |
return \true; |
| 42 |
} |
| 43 |
} |
| 44 |
return \false; |
| 45 |
} |
| 46 |
public function addElement(AbstractExpression $value, ?AbstractExpression $key = null) : void |
| 47 |
{ |
| 48 |
if (null === $key) { |
| 49 |
$key = new ConstantExpression(++$this->index, $value->getTemplateLine()); |
| 50 |
} |
| 51 |
\array_push($this->nodes, $key, $value); |
| 52 |
} |
| 53 |
public function compile(Compiler $compiler) : void |
| 54 |
{ |
| 55 |
$keyValuePairs = $this->getKeyValuePairs(); |
| 56 |
$needsArrayMergeSpread = \PHP_VERSION_ID < 80100 && $this->hasSpreadItem($keyValuePairs); |
| 57 |
if ($needsArrayMergeSpread) { |
| 58 |
$compiler->raw('CoreExtension::merge('); |
| 59 |
} |
| 60 |
$compiler->raw('['); |
| 61 |
$first = \true; |
| 62 |
$reopenAfterMergeSpread = \false; |
| 63 |
$nextIndex = 0; |
| 64 |
foreach ($keyValuePairs as $pair) { |
| 65 |
if ($reopenAfterMergeSpread) { |
| 66 |
$compiler->raw(', ['); |
| 67 |
$reopenAfterMergeSpread = \false; |
| 68 |
} |
| 69 |
if ($needsArrayMergeSpread && $pair['value']->hasAttribute('spread')) { |
| 70 |
$compiler->raw('], ')->subcompile($pair['value']); |
| 71 |
$first = \true; |
| 72 |
$reopenAfterMergeSpread = \true; |
| 73 |
continue; |
| 74 |
} |
| 75 |
if (!$first) { |
| 76 |
$compiler->raw(', '); |
| 77 |
} |
| 78 |
$first = \false; |
| 79 |
if ($pair['value']->hasAttribute('spread') && !$needsArrayMergeSpread) { |
| 80 |
$compiler->raw('...')->subcompile($pair['value']); |
| 81 |
++$nextIndex; |
| 82 |
} else { |
| 83 |
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null; |
| 84 |
if ($nextIndex !== $key) { |
| 85 |
if (\is_int($key)) { |
| 86 |
$nextIndex = $key + 1; |
| 87 |
} |
| 88 |
$compiler->subcompile($pair['key'])->raw(' => '); |
| 89 |
} else { |
| 90 |
++$nextIndex; |
| 91 |
} |
| 92 |
$compiler->subcompile($pair['value']); |
| 93 |
} |
| 94 |
} |
| 95 |
if (!$reopenAfterMergeSpread) { |
| 96 |
$compiler->raw(']'); |
| 97 |
} |
| 98 |
if ($needsArrayMergeSpread) { |
| 99 |
$compiler->raw(')'); |
| 100 |
} |
| 101 |
} |
| 102 |
private function hasSpreadItem(array $pairs) : bool |
| 103 |
{ |
| 104 |
foreach ($pairs as $pair) { |
| 105 |
if ($pair['value']->hasAttribute('spread')) { |
| 106 |
return \true; |
| 107 |
} |
| 108 |
} |
| 109 |
return \false; |
| 110 |
} |
| 111 |
} |
| 112 |
|