‪TYPO3CMS  10.4
PageArguments.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 
26 {
30  protected ‪$pageId;
31 
35  protected ‪$pageType;
36 
42  protected $arguments;
43 
51  protected $staticArguments;
52 
59  protected $dynamicArguments;
60 
66  protected $routeArguments;
67 
73  protected $queryArguments = [];
74 
78  protected $dirty = false;
79 
87  public function __construct(int ‪$pageId, string ‪$pageType, array $routeArguments, array $staticArguments = [], array $remainingArguments = [])
88  {
89  $this->pageId = ‪$pageId;
90  $this->pageType = ‪$pageType;
91  $this->‪routeArguments = $this->‪sort($routeArguments);
92  $this->‪staticArguments = $this->‪sort($staticArguments);
93  $this->‪arguments = $this->routeArguments;
95  if (!empty($remainingArguments)) {
96  $this->‪updateQueryArguments($remainingArguments);
97  }
98  }
99 
103  public function ‪areDirty(): bool
104  {
105  return $this->dirty;
106  }
107 
111  public function getRouteArguments(): array
112  {
113  return $this->routeArguments;
114  }
115 
119  public function ‪getPageId(): int
120  {
121  return ‪$this->pageId;
122  }
123 
127  public function ‪getPageType(): string
128  {
129  return ‪$this->pageType;
130  }
131 
136  public function get(string $name)
137  {
138  return $this->‪arguments[$name] ?? null;
139  }
140 
144  public function getArguments(): array
145  {
146  return $this->arguments;
147  }
148 
152  public function getStaticArguments(): array
153  {
154  return $this->staticArguments;
155  }
156 
160  public function getDynamicArguments(): array
161  {
162  return $this->dynamicArguments;
163  }
164 
168  public function getQueryArguments(): array
169  {
170  return $this->queryArguments;
171  }
172 
176  protected function ‪updateQueryArguments(array $queryArguments)
177  {
178  $queryArguments = $this->‪sort($queryArguments);
179  if ($this->‪queryArguments === $queryArguments) {
180  return;
181  }
182  // in case query arguments would override route arguments,
183  // the state is considered as dirty (since it's not distinct)
184  // thus, route arguments take precedence over query arguments
185  $additionalQueryArguments = $this->‪diff($queryArguments, $this->‪routeArguments);
186  $dirty = $additionalQueryArguments !== $queryArguments;
187  $this->dirty = $this->dirty || $dirty;
188  $this->‪queryArguments = $queryArguments;
189  $this->‪arguments = array_replace_recursive($this->‪arguments, $additionalQueryArguments);
190  $this->‪updateDynamicArguments();
191  }
192 
196  protected function ‪updateDynamicArguments(): void
197  {
198  $this->‪dynamicArguments = $this->‪diff(
199  $this->‪arguments,
200  $this->‪staticArguments
201  );
202  }
203 
210  protected function ‪clean(array $array): array
211  {
212  foreach ($array as $key => &$item) {
213  if (!is_array($item)) {
214  continue;
215  }
216  if (!empty($item)) {
217  $item = $this->‪clean($item);
218  }
219  if (empty($item)) {
220  unset($array[$key]);
221  }
222  }
223  return $array;
224  }
225 
232  protected function ‪sort(array $array): array
233  {
234  $array = $this->‪clean($array);
236  return $array;
237  }
238 
246  protected function ‪diff(array $first, array $second): array
247  {
248  return ‪ArrayUtility::arrayDiffKeyRecursive($first, $second);
249  }
250 
255  public function ‪offsetExists($offset): bool
256  {
257  return $offset === 'pageId' || $offset === 'pageType' || isset($this->‪arguments[$offset]);
258  }
259 
264  public function offsetGet($offset)
265  {
266  if ($offset === 'pageId') {
267  return $this->‪getPageId();
268  }
269  if ($offset === 'pageType') {
270  return $this->‪getPageType();
271  }
272  return $this->‪arguments[$offset] ?? null;
273  }
274 
279  public function ‪offsetSet($offset, $value)
280  {
281  throw new \InvalidArgumentException('PageArguments cannot be modified.', 1538152266);
282  }
283 
287  public function ‪offsetUnset($offset)
288  {
289  throw new \InvalidArgumentException('PageArguments cannot be modified.', 1538152269);
290  }
291 }
‪TYPO3\CMS\Core\Routing\PageArguments\getPageType
‪string getPageType()
Definition: PageArguments.php:119
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Routing\RouteResultInterface
Definition: RouteResultInterface.php:24
‪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:84
‪TYPO3\CMS\Core\Routing\PageArguments\routeArguments
‪array< string, function getRouteArguments():array { return $this-> routeArguments
Definition: PageArguments.php:105
‪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:224
‪TYPO3\CMS\Core\Routing\PageArguments\offsetExists
‪bool offsetExists($offset)
Definition: PageArguments.php:247
‪TYPO3\CMS\Core\Utility\ArrayUtility\arrayDiffKeyRecursive
‪static array arrayDiffKeyRecursive(array $array1, array $array2)
Definition: ArrayUtility.php:768
‪TYPO3\CMS\Core\Routing\PageArguments\dynamicArguments
‪array< string, function getDynamicArguments():array { return $this-> dynamicArguments
Definition: PageArguments.php:154
‪TYPO3\CMS\Core\Routing\PageArguments\offsetSet
‪offsetSet($offset, $value)
Definition: PageArguments.php:271
‪TYPO3\CMS\Core\Routing\PageArguments\updateDynamicArguments
‪updateDynamicArguments()
Definition: PageArguments.php:188
‪TYPO3\CMS\Core\Routing\PageArguments\offsetUnset
‪offsetUnset($offset)
Definition: PageArguments.php:279
‪TYPO3\CMS\Core\Routing\PageArguments\$pageId
‪int $pageId
Definition: PageArguments.php:29
‪TYPO3\CMS\Core\Routing\PageArguments\arguments
‪$this arguments
Definition: PageArguments.php:85
‪TYPO3\CMS\Core\Routing\PageArguments\diff
‪array diff(array $first, array $second)
Definition: PageArguments.php:238
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Routing\PageArguments\queryArguments
‪array< string, function getQueryArguments():array { return $this-> queryArguments
Definition: PageArguments.php:162
‪TYPO3\CMS\Core\Routing\PageArguments\updateQueryArguments
‪updateQueryArguments(array $queryArguments)
Definition: PageArguments.php:168
‪TYPO3\CMS\Core\Routing\PageArguments\clean
‪array clean(array $array)
Definition: PageArguments.php:202
‪TYPO3\CMS\Core\Routing\PageArguments\areDirty
‪if(!empty($remainingArguments)) bool areDirty()
Definition: PageArguments.php:95
‪TYPO3\CMS\Core\Utility\ArrayUtility\naturalKeySortRecursive
‪static bool naturalKeySortRecursive(array &$array)
Definition: ArrayUtility.php:823
‪TYPO3\CMS\Core\Routing\PageArguments\getPageId
‪int getPageId()
Definition: PageArguments.php:111
‪TYPO3\CMS\Core\Routing\PageArguments\$pageType
‪string $pageType
Definition: PageArguments.php:33