‪TYPO3CMS  10.4
VariableCompiler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 {
22  public const ‪FLAG_MERGE_OVERRIDE = 1;
23  public const ‪FLAG_MERGE_RECURSIVE = 2;
24 
28  private ‪$items;
29 
33  private ‪$variables;
34 
38  private ‪$flags;
39 
43  private ‪$results;
44 
45  public static function ‪create(array ‪$items, ‪Variables ‪$variables, int ‪$flags = self::FLAG_MERGE_RECURSIVE): self
46  {
47  return new static(‪$items, ‪$variables, ‪$flags);
48  }
49 
50  public function ‪__construct(array ‪$items, ‪Variables ‪$variables, int ‪$flags = self::FLAG_MERGE_RECURSIVE)
51  {
52  $this->items = ‪$items;
53  $this->variables = ‪$variables;
54  $this->flags = ‪$flags;
55  }
56 
57  public function ‪compile(): self
58  {
59  $this->results = $this->‪compileKeys($this->items);
60  $this->results = $this->‪compileValues($this->results);
61  return $this;
62  }
63 
67  public function ‪getResults(): array
68  {
69  return ‪$this->results;
70  }
71 
72  private function ‪compileKeys(array $array): array
73  {
74  $result = [];
75  // applying VariableItems
76  foreach ($array as $index => $item) {
77  $result = $this->‪assignItems(
78  $result,
79  $item instanceof ‪VariableItem ? $item->‪apply($this->variables) : [$index => $item]
80  );
81  }
82  // traversing into nested array items (if any)
83  foreach ($result as $index => $item) {
84  if (is_array($item)) {
85  $result[$index] = $this->‪compileKeys($item);
86  }
87  }
88  return $result;
89  }
90 
91  private function ‪assignItems(array $array, array ‪$items): array
92  {
93  if ($this->flags & self::FLAG_MERGE_OVERRIDE) {
94  // keep possible numeric indexes
95  return $array + ‪$items;
96  }
97  return array_replace_recursive($array, ‪$items);
98  }
99 
100  private function ‪compileValues(array $array): array
101  {
102  foreach ($array as &$item) {
103  if (is_array($item)) {
104  $item = $this->‪compileValues($item);
105  } elseif ($item instanceof ‪Variable) {
106  $item = $item->apply($this->variables);
107  } elseif ($item instanceof VariableValue) {
108  $item = $item->apply($this->variables);
109  }
110  }
111  return $array;
112  }
113 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable
Definition: Variable.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem\apply
‪apply(Variables $variables)
Definition: VariableItem.php:54
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compile
‪compile()
Definition: VariableCompiler.php:53
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compileValues
‪compileValues(array $array)
Definition: VariableCompiler.php:96
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$items
‪array $items
Definition: VariableCompiler.php:27
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compileKeys
‪compileKeys(array $array)
Definition: VariableCompiler.php:68
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\FLAG_MERGE_RECURSIVE
‪const FLAG_MERGE_RECURSIVE
Definition: VariableCompiler.php:23
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\create
‪static create(array $items, Variables $variables, int $flags=self::FLAG_MERGE_RECURSIVE)
Definition: VariableCompiler.php:41
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$flags
‪int null $flags
Definition: VariableCompiler.php:35
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler
Definition: VariableCompiler.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\getResults
‪array getResults()
Definition: VariableCompiler.php:63
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\__construct
‪__construct(array $items, Variables $variables, int $flags=self::FLAG_MERGE_RECURSIVE)
Definition: VariableCompiler.php:46
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$results
‪array $results
Definition: VariableCompiler.php:39
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:18
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem
Definition: VariableItem.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue
Definition: VariableValue.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$variables
‪Variables $variables
Definition: VariableCompiler.php:31
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\FLAG_MERGE_OVERRIDE
‪const FLAG_MERGE_OVERRIDE
Definition: VariableCompiler.php:22
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\assignItems
‪assignItems(array $array, array $items)
Definition: VariableCompiler.php:87