‪TYPO3CMS  9.5
VariableCompiler.php
Go to the documentation of this file.
1 <?php
2 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 {
20  public const ‪FLAG_MERGE_OVERRIDE = 1;
21  public const ‪FLAG_MERGE_RECURSIVE = 2;
22 
26  private ‪$items;
27 
31  private ‪$variables;
32 
36  private ‪$flags;
37 
41  private ‪$results;
42 
43  public static function ‪create(array ‪$items, ‪Variables ‪$variables, int ‪$flags = self::FLAG_MERGE_RECURSIVE): self
44  {
45  return new static(‪$items, ‪$variables, ‪$flags);
46  }
47 
48  public function ‪__construct(array ‪$items, ‪Variables ‪$variables, int ‪$flags = self::FLAG_MERGE_RECURSIVE)
49  {
50  $this->items = ‪$items;
51  $this->variables = ‪$variables;
52  $this->flags = ‪$flags;
53  }
54 
55  public function ‪compile(): self
56  {
57  $this->results = $this->‪compileKeys($this->items);
58  $this->results = $this->‪compileValues($this->results);
59  return $this;
60  }
61 
65  public function ‪getResults(): array
66  {
67  return ‪$this->results;
68  }
69 
70  private function ‪compileKeys(array $array): array
71  {
72  $result = [];
73  // applying VariableItems
74  foreach ($array as $index => $item) {
75  $result = $this->‪assignItems(
76  $result,
77  $item instanceof ‪VariableItem ? $item->‪apply($this->variables) : [$index => $item]
78  );
79  }
80  // traversing into nested array items (if any)
81  foreach ($result as $index => $item) {
82  if (is_array($item)) {
83  $result[$index] = $this->‪compileKeys($item);
84  }
85  }
86  return $result;
87  }
88 
89  private function ‪assignItems(array $array, array ‪$items): array
90  {
91  if ($this->flags & self::FLAG_MERGE_OVERRIDE) {
92  // keep possible numeric indexes
93  return $array + ‪$items;
94  }
95  return array_replace_recursive($array, ‪$items);
96  }
97 
98  private function ‪compileValues(array $array): array
99  {
100  foreach ($array as &$item) {
101  if (is_array($item)) {
102  $item = $this->‪compileValues($item);
103  } elseif ($item instanceof ‪Variable) {
104  $item = $item->apply($this->variables);
105  } elseif ($item instanceof VariableValue) {
106  $item = $item->apply($this->variables);
107  }
108  }
109  return $array;
110  }
111 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable
Definition: Variable.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem\apply
‪apply(Variables $variables)
Definition: VariableItem.php:52
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compile
‪compile()
Definition: VariableCompiler.php:51
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compileValues
‪compileValues(array $array)
Definition: VariableCompiler.php:94
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$items
‪array $items
Definition: VariableCompiler.php:25
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\compileKeys
‪compileKeys(array $array)
Definition: VariableCompiler.php:66
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\FLAG_MERGE_RECURSIVE
‪const FLAG_MERGE_RECURSIVE
Definition: VariableCompiler.php:21
‪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:39
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$flags
‪int null $flags
Definition: VariableCompiler.php:33
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler
Definition: VariableCompiler.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\getResults
‪array getResults()
Definition: VariableCompiler.php:61
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\__construct
‪__construct(array $items, Variables $variables, int $flags=self::FLAG_MERGE_RECURSIVE)
Definition: VariableCompiler.php:44
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$results
‪array $results
Definition: VariableCompiler.php:37
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:3
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem
Definition: VariableItem.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue
Definition: VariableValue.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\$variables
‪Variables $variables
Definition: VariableCompiler.php:29
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\FLAG_MERGE_OVERRIDE
‪const FLAG_MERGE_OVERRIDE
Definition: VariableCompiler.php:20
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableCompiler\assignItems
‪assignItems(array $array, array $items)
Definition: VariableCompiler.php:85