‪TYPO3CMS  9.5
PageArguments.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
20 
25 {
29  protected ‪$pageId;
30 
34  protected ‪$pageType;
35 
41  protected $arguments;
42 
50  protected $staticArguments;
51 
58  protected $dynamicArguments;
59 
65  protected $routeArguments;
66 
72  protected $queryArguments = [];
73 
77  protected $dirty = false;
78 
86  public function __construct(int ‪$pageId, string ‪$pageType, array $routeArguments, array $staticArguments = [], array $remainingArguments = [])
87  {
88  $this->pageId = ‪$pageId;
89  $this->pageType = ‪$pageType;
90  $this->‪routeArguments = $this->‪sort($routeArguments);
91  $this->‪staticArguments = $this->‪sort($staticArguments);
92  $this->‪arguments = $this->routeArguments;
94  if (!empty($remainingArguments)) {
95  $this->‪updateQueryArguments($remainingArguments);
96  }
97  }
98 
102  public function ‪areDirty(): bool
103  {
104  return $this->dirty;
105  }
106 
110  public function getRouteArguments(): array
111  {
112  return $this->routeArguments;
113  }
114 
118  public function ‪getPageId(): int
119  {
120  return ‪$this->pageId;
121  }
122 
126  public function ‪getPageType(): string
127  {
128  return ‪$this->pageType;
129  }
130 
135  public function get(string $name)
136  {
137  return $this->‪arguments[$name] ?? null;
138  }
139 
143  public function getArguments(): array
144  {
145  return $this->arguments;
146  }
147 
151  public function getStaticArguments(): array
152  {
153  return $this->staticArguments;
154  }
155 
159  public function getDynamicArguments(): array
160  {
161  return $this->dynamicArguments;
162  }
163 
167  public function getQueryArguments(): array
168  {
169  return $this->queryArguments;
170  }
171 
177  public function ‪withQueryArguments(array $queryArguments): self
178  {
179  $queryArguments = $this->‪sort($queryArguments);
180  if ($this->‪queryArguments === $queryArguments) {
181  return $this;
182  }
183  // in case query arguments would override route arguments,
184  // the state is considered as dirty (since it's not distinct)
185  // thus, route arguments take precedence over query arguments
186  $additionalQueryArguments = $this->‪diff($queryArguments, $this->‪routeArguments);
187  $dirty = $additionalQueryArguments !== $queryArguments;
188  // apply changes
189  $target = clone $this;
190  $target->dirty = $this->dirty || $dirty;
191  $target->queryArguments = $queryArguments;
192  $target->arguments = array_replace_recursive($target->arguments, $additionalQueryArguments);
193  $target->updateDynamicArguments();
194  return $target;
195  }
196 
200  protected function ‪updateQueryArguments(array $queryArguments)
201  {
202  $queryArguments = $this->‪sort($queryArguments);
203  if ($this->‪queryArguments === $queryArguments) {
204  return;
205  }
206  // in case query arguments would override route arguments,
207  // the state is considered as dirty (since it's not distinct)
208  // thus, route arguments take precedence over query arguments
209  $additionalQueryArguments = $this->‪diff($queryArguments, $this->‪routeArguments);
210  $dirty = $additionalQueryArguments !== $queryArguments;
211  $this->dirty = $this->dirty || $dirty;
212  $this->‪queryArguments = $queryArguments;
213  $this->‪arguments = array_replace_recursive($this->‪arguments, $additionalQueryArguments);
214  $this->‪updateDynamicArguments();
215  }
216 
220  protected function ‪updateDynamicArguments(): void
221  {
222  $this->‪dynamicArguments = $this->‪diff(
223  $this->‪arguments,
224  $this->‪staticArguments
225  );
226  }
227 
234  protected function ‪clean(array $array): array
235  {
236  foreach ($array as $key => &$item) {
237  if (!is_array($item)) {
238  continue;
239  }
240  if (!empty($item)) {
241  $item = $this->‪clean($item);
242  }
243  if (empty($item)) {
244  unset($array[$key]);
245  }
246  }
247  return $array;
248  }
249 
256  protected function ‪sort(array $array): array
257  {
258  $array = $this->‪clean($array);
260  return $array;
261  }
262 
270  protected function ‪diff(array $first, array $second): array
271  {
272  return ‪ArrayUtility::arrayDiffAssocRecursive($first, $second);
273  }
274 
279  public function ‪offsetExists($offset): bool
280  {
281  return $offset === 'pageId' || $offset === 'pageType' || isset($this->‪arguments[$offset]);
282  }
283 
288  public function offsetGet($offset)
289  {
290  if ($offset === 'pageId') {
291  return $this->‪getPageId();
292  }
293  if ($offset === 'pageType') {
294  return $this->‪getPageType();
295  }
296  return $this->‪arguments[$offset] ?? null;
297  }
298 
303  public function ‪offsetSet($offset, $value)
304  {
305  throw new \InvalidArgumentException('PageArguments cannot be modified.', 1538152266);
306  }
307 
311  public function ‪offsetUnset($offset)
312  {
313  throw new \InvalidArgumentException('PageArguments cannot be modified.', 1538152269);
314  }
315 }
‪TYPO3\CMS\Core\Routing\PageArguments\getPageType
‪string getPageType()
Definition: PageArguments.php:118
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Core\Routing\RouteResultInterface
Definition: RouteResultInterface.php:23
‪TYPO3\CMS\Core\Routing\PageArguments\staticArguments
‪array< string, $arguments;protected array< string, $staticArguments;protected array< string, $dynamicArguments;protected array< string, $routeArguments;protected array< string, $queryArguments=array();protected bool $dirty=false;public function __construct(int $pageId, string $pageType, array $routeArguments, array $staticArguments=[], array $remainingArguments=[]) { $this->pageId=$pageId;$this->pageType=$pageType;$this->routeArguments=$this->sort( $routeArguments);$this-> staticArguments
Definition: PageArguments.php:83
‪TYPO3\CMS\Core\Routing\PageArguments\routeArguments
‪array< string, function getRouteArguments():array { return $this-> routeArguments
Definition: PageArguments.php:104
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\PageArguments\getPageId
‪string array< string, offsetGet( $offset) { if( $offset==='pageId') { return $this-> getPageId()
‪TYPO3\CMS\Core\Routing\PageArguments\sort
‪array sort(array $array)
Definition: PageArguments.php:248
‪TYPO3\CMS\Core\Routing\PageArguments\offsetExists
‪bool offsetExists($offset)
Definition: PageArguments.php:271
‪TYPO3\CMS\Core\Routing\PageArguments\dynamicArguments
‪array< string, function getDynamicArguments():array { return $this-> dynamicArguments
Definition: PageArguments.php:153
‪TYPO3\CMS\Core\Routing\PageArguments\offsetSet
‪offsetSet($offset, $value)
Definition: PageArguments.php:295
‪TYPO3\CMS\Core\Routing\PageArguments\withQueryArguments
‪static withQueryArguments(array $queryArguments)
Definition: PageArguments.php:169
‪TYPO3\CMS\Core\Routing\PageArguments\updateDynamicArguments
‪updateDynamicArguments()
Definition: PageArguments.php:212
‪TYPO3\CMS\Core\Routing\PageArguments\offsetUnset
‪offsetUnset($offset)
Definition: PageArguments.php:303
‪TYPO3\CMS\Core\Routing\PageArguments\$pageId
‪int $pageId
Definition: PageArguments.php:28
‪TYPO3\CMS\Core\Routing\PageArguments\arguments
‪$this arguments
Definition: PageArguments.php:84
‪TYPO3\CMS\Core\Routing\PageArguments\diff
‪array diff(array $first, array $second)
Definition: PageArguments.php:262
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Routing\PageArguments\queryArguments
‪array< string, function getQueryArguments():array { return $this-> queryArguments
Definition: PageArguments.php:161
‪TYPO3\CMS\Core\Routing\PageArguments\updateQueryArguments
‪updateQueryArguments(array $queryArguments)
Definition: PageArguments.php:192
‪TYPO3\CMS\Core\Routing\PageArguments\clean
‪array clean(array $array)
Definition: PageArguments.php:226
‪TYPO3\CMS\Core\Routing\PageArguments\areDirty
‪if(!empty($remainingArguments)) bool areDirty()
Definition: PageArguments.php:94
‪TYPO3\CMS\Core\Utility\ArrayUtility\naturalKeySortRecursive
‪static bool naturalKeySortRecursive(array &$array)
Definition: ArrayUtility.php:752
‪TYPO3\CMS\Core\Routing\PageArguments\getPageId
‪int getPageId()
Definition: PageArguments.php:110
‪TYPO3\CMS\Core\Utility\ArrayUtility\arrayDiffAssocRecursive
‪static array arrayDiffAssocRecursive(array $array1, array $array2)
Definition: ArrayUtility.php:728
‪TYPO3\CMS\Core\Routing\PageArguments\$pageType
‪string $pageType
Definition: PageArguments.php:32