‪TYPO3CMS  11.5
Arguments.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
26 class ‪Arguments extends \ArrayObject
27 {
31  protected ‪$argumentNames = [];
32 
36  protected ‪$argumentShortNames = [];
37 
41  public function ‪__construct()
42  {
43  parent::__construct();
44  }
45 
56  #[\ReturnTypeWillChange]
57  public function ‪offsetSet($offset, $value)
58  {
59  if (!$value instanceof ‪Argument) {
60  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
61  }
62  $argumentName = $value->getName();
63  parent::offsetSet($argumentName, $value);
64  $this->argumentNames[$argumentName] = true;
65  }
66 
75  #[\ReturnTypeWillChange]
76  public function ‪append($value)
77  {
78  if (!$value instanceof ‪Argument) {
79  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
80  }
81  $this->‪offsetSet(null, $value);
82  }
83 
91  #[\ReturnTypeWillChange]
92  public function ‪offsetUnset($offset)
93  {
94  $translatedOffset = $this->‪translateToLongArgumentName($offset);
95  parent::offsetUnset($translatedOffset);
96  unset($this->argumentNames[$translatedOffset]);
97  if ($offset != $translatedOffset) {
98  unset($this->argumentShortNames[$offset]);
99  }
100  }
101 
110  #[\ReturnTypeWillChange]
111  public function ‪offsetExists($offset)
112  {
113  $translatedOffset = $this->‪translateToLongArgumentName($offset);
114  return parent::offsetExists($translatedOffset);
115  }
116 
126  #[\ReturnTypeWillChange]
127  public function ‪offsetGet($offset)
128  {
129  $translatedOffset = $this->‪translateToLongArgumentName($offset);
130  if ($translatedOffset === '') {
131  throw new ‪NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
132  }
133  return parent::offsetGet($translatedOffset);
134  }
135 
147  public function ‪addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
148  {
149  $argument = GeneralUtility::makeInstance(Argument::class, $name, $dataType);
150  $argument->setRequired($isRequired);
151  $argument->setDefaultValue($defaultValue);
152  $this->‪addArgument($argument);
153  return $argument;
154  }
155 
165  public function ‪addArgument(‪Argument $argument)
166  {
167  $this->‪offsetSet(null, $argument);
168  }
169 
177  public function ‪getArgument($argumentName)
178  {
179  if (!$this->‪offsetExists($argumentName)) {
180  throw new ‪NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
181  }
182  return $this->‪offsetGet($argumentName);
183  }
184 
192  public function ‪hasArgument($argumentName)
193  {
194  return $this->‪offsetExists($argumentName);
195  }
196 
202  public function ‪getArgumentNames()
203  {
204  return array_keys($this->argumentNames);
205  }
206 
212  public function ‪getArgumentShortNames()
213  {
216  foreach ($this as $argument) {
217  ‪$argumentShortNames[$argument->getShortName()] = true;
218  }
219  return array_keys(‪$argumentShortNames);
220  }
221 
230  public function ‪__call($methodName, array $arguments)
231  {
232  if (strpos($methodName, 'set') !== 0) {
233  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
234  }
235  $firstLowerCaseArgumentName = $this->‪translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
236  $firstUpperCaseArgumentName = $this->‪translateToLongArgumentName(ucfirst(substr($methodName, 3)));
237  if (in_array($firstLowerCaseArgumentName, $this->‪getArgumentNames())) {
238  $argument = parent::offsetGet($firstLowerCaseArgumentName);
239  $argument->‪setValue($arguments[0]);
240  } elseif (in_array($firstUpperCaseArgumentName, $this->‪getArgumentNames())) {
241  $argument = parent::offsetGet($firstUpperCaseArgumentName);
242  $argument->‪setValue($arguments[0]);
243  }
244  }
245 
256  protected function ‪translateToLongArgumentName($argumentName)
257  {
258  if (in_array($argumentName, $this->‪getArgumentNames())) {
259  return $argumentName;
260  }
262  foreach ($this as $argument) {
263  if ($argumentName === $argument->getShortName()) {
264  return $argument->getName();
265  }
266  }
267  return '';
268  }
269 
273  public function ‪removeAll()
274  {
275  foreach ($this->argumentNames as $argumentName => $booleanValue) {
276  parent::offsetUnset($argumentName);
277  }
278  $this->argumentNames = [];
279  }
280 
284  public function ‪validate(): ‪Result
285  {
286  $results = new ‪Result();
288  foreach ($this as $argument) {
289  $argumentValidationResults = $argument->validate();
290  $results->forProperty($argument->getName())->merge($argumentValidationResults);
291  }
292  return $results;
293  }
294 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:225
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\hasArgument
‪bool hasArgument($argumentName)
Definition: Arguments.php:190
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgument
‪Argument getArgument($argumentName)
Definition: Arguments.php:175
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetExists
‪bool offsetExists($offset)
Definition: Arguments.php:109
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:27
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: ActionController.php:16
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\validate
‪TYPO3 CMS Extbase Error Result validate()
Definition: Arguments.php:282
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentShortNames
‪array $argumentShortNames
Definition: Arguments.php:34
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addNewArgument
‪Argument addNewArgument($name, $dataType='Text', $isRequired=false, $defaultValue=null)
Definition: Arguments.php:145
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__construct
‪__construct()
Definition: Arguments.php:39
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetSet
‪offsetSet($offset, $value)
Definition: Arguments.php:55
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\translateToLongArgumentName
‪string translateToLongArgumentName($argumentName)
Definition: Arguments.php:254
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetUnset
‪offsetUnset($offset)
Definition: Arguments.php:90
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentNames
‪array $argumentNames
Definition: Arguments.php:30
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__call
‪__call($methodName, array $arguments)
Definition: Arguments.php:228
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetGet
‪Argument offsetGet($offset)
Definition: Arguments.php:125
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addArgument
‪addArgument(Argument $argument)
Definition: Arguments.php:163
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\append
‪append($value)
Definition: Arguments.php:74
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\removeAll
‪removeAll()
Definition: Arguments.php:271
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentShortNames
‪array getArgumentShortNames()
Definition: Arguments.php:210
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentNames
‪array getArgumentNames()
Definition: Arguments.php:200
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:27
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
Definition: NoSuchArgumentException.php:25