‪TYPO3CMS  ‪main
Arguments.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 
23 
28 class ‪Arguments extends \ArrayObject
29 {
33  protected ‪$argumentNames = [];
34 
38  protected ‪$argumentShortNames = [];
39 
43  public function ‪__construct()
44  {
45  parent::__construct();
46  }
47 
56  public function ‪offsetSet($offset, $value): void
57  {
58  if (!$value instanceof ‪Argument) {
59  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
60  }
61  $argumentName = $value->getName();
62  parent::offsetSet($argumentName, $value);
63  $this->argumentNames[$argumentName] = true;
64  }
65 
72  public function ‪append($value): void
73  {
74  if (!$value instanceof ‪Argument) {
75  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
76  }
77  $this->‪offsetSet(null, $value);
78  }
79 
83  public function ‪offsetUnset($offset): void
84  {
85  $translatedOffset = $this->‪translateToLongArgumentName($offset);
86  parent::offsetUnset($translatedOffset);
87  unset($this->argumentNames[$translatedOffset]);
88  if ($offset != $translatedOffset) {
89  unset($this->argumentShortNames[$offset]);
90  }
91  }
92 
96  public function ‪offsetExists($offset): bool
97  {
98  $translatedOffset = $this->‪translateToLongArgumentName($offset);
99  return parent::offsetExists($translatedOffset);
100  }
101 
109  public function ‪offsetGet($offset): ‪Argument
110  {
111  $translatedOffset = $this->‪translateToLongArgumentName($offset);
112  if ($translatedOffset === '') {
113  throw new ‪NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
114  }
115  return parent::offsetGet($translatedOffset);
116  }
117 
129  public function ‪addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
130  {
131  $argument = GeneralUtility::makeInstance(Argument::class, $name, $dataType);
132  $argument->setRequired($isRequired);
133  $argument->setDefaultValue($defaultValue);
134  $this->‪addArgument($argument);
135  return $argument;
136  }
137 
147  public function ‪addArgument(‪Argument $argument)
148  {
149  $this->‪offsetSet(null, $argument);
150  }
151 
159  public function ‪getArgument($argumentName)
160  {
161  if (!$this->‪offsetExists($argumentName)) {
162  throw new ‪NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
163  }
164  return $this->‪offsetGet($argumentName);
165  }
166 
174  public function ‪hasArgument($argumentName)
175  {
176  return $this->‪offsetExists($argumentName);
177  }
178 
184  public function ‪getArgumentNames()
185  {
186  return array_keys($this->argumentNames);
187  }
188 
194  public function ‪getArgumentShortNames()
195  {
198  foreach ($this as $argument) {
199  ‪$argumentShortNames[$argument->getShortName()] = true;
200  }
201  return array_keys(‪$argumentShortNames);
202  }
203 
212  public function ‪__call($methodName, array $arguments)
213  {
214  if (!str_starts_with($methodName, 'set')) {
215  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
216  }
217  $firstLowerCaseArgumentName = $this->‪translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
218  $firstUpperCaseArgumentName = $this->‪translateToLongArgumentName(ucfirst(substr($methodName, 3)));
219  if (in_array($firstLowerCaseArgumentName, $this->‪getArgumentNames())) {
220  $argument = parent::offsetGet($firstLowerCaseArgumentName);
221  $argument->‪setValue($arguments[0]);
222  } elseif (in_array($firstUpperCaseArgumentName, $this->‪getArgumentNames())) {
223  $argument = parent::offsetGet($firstUpperCaseArgumentName);
224  $argument->‪setValue($arguments[0]);
225  }
226  }
227 
238  protected function ‪translateToLongArgumentName($argumentName)
239  {
240  if (in_array($argumentName, $this->‪getArgumentNames())) {
241  return $argumentName;
242  }
244  foreach ($this as $argument) {
245  if ($argumentName === $argument->getShortName()) {
246  return $argument->getName();
247  }
248  }
249  return '';
250  }
251 
255  public function ‪removeAll()
256  {
257  foreach ($this->argumentNames as $argumentName => $booleanValue) {
258  parent::offsetUnset($argumentName);
259  }
260  $this->argumentNames = [];
261  }
262 
263  public function ‪validate(): ‪Result
264  {
265  $results = new ‪Result();
267  foreach ($this as $argument) {
268  $argumentValidationResults = $argument->validate();
269  $results->forProperty($argument->getName())->merge($argumentValidationResults);
270  }
271  return $results;
272  }
273 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\hasArgument
‪bool hasArgument($argumentName)
Definition: Arguments.php:172
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgument
‪Argument getArgument($argumentName)
Definition: Arguments.php:157
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:29
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: ActionController.php:16
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentShortNames
‪array $argumentShortNames
Definition: Arguments.php:36
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addNewArgument
‪Argument addNewArgument($name, $dataType='Text', $isRequired=false, $defaultValue=null)
Definition: Arguments.php:127
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__construct
‪__construct()
Definition: Arguments.php:41
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetSet
‪offsetSet($offset, $value)
Definition: Arguments.php:54
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetExists
‪offsetExists($offset)
Definition: Arguments.php:94
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\translateToLongArgumentName
‪string translateToLongArgumentName($argumentName)
Definition: Arguments.php:236
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetUnset
‪offsetUnset($offset)
Definition: Arguments.php:81
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentNames
‪array $argumentNames
Definition: Arguments.php:32
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__call
‪__call($methodName, array $arguments)
Definition: Arguments.php:210
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetGet
‪Argument offsetGet($offset)
Definition: Arguments.php:107
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addArgument
‪addArgument(Argument $argument)
Definition: Arguments.php:145
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\append
‪append($value)
Definition: Arguments.php:70
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\removeAll
‪removeAll()
Definition: Arguments.php:253
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentShortNames
‪array getArgumentShortNames()
Definition: Arguments.php:192
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\validate
‪validate()
Definition: Arguments.php:261
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentNames
‪array getArgumentNames()
Definition: Arguments.php:182
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:27
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
Definition: NoSuchArgumentException.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪Argument setValue($rawValue)
Definition: Argument.php:225