‪TYPO3CMS  10.4
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 ‪$objectManager;
32 
36  protected ‪$argumentNames = [];
37 
41  protected ‪$argumentShortNames = [];
42 
47  {
48  $this->objectManager = ‪$objectManager;
49  }
50 
54  public function ‪__construct()
55  {
56  parent::__construct();
57  }
58 
67  public function ‪offsetSet($offset, $value)
68  {
69  if (!$value instanceof ‪Argument) {
70  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
71  }
72  $argumentName = $value->getName();
73  parent::offsetSet($argumentName, $value);
74  $this->argumentNames[$argumentName] = true;
75  }
76 
83  public function ‪append($value)
84  {
85  if (!$value instanceof ‪Argument) {
86  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
87  }
88  $this->‪offsetSet(null, $value);
89  }
90 
96  public function ‪offsetUnset($offset)
97  {
98  $translatedOffset = $this->‪translateToLongArgumentName($offset);
99  parent::offsetUnset($translatedOffset);
100  unset($this->argumentNames[$translatedOffset]);
101  if ($offset != $translatedOffset) {
102  unset($this->argumentShortNames[$offset]);
103  }
104  }
105 
112  public function ‪offsetExists($offset)
113  {
114  $translatedOffset = $this->‪translateToLongArgumentName($offset);
115  return parent::offsetExists($translatedOffset);
116  }
117 
125  public function ‪offsetGet($offset)
126  {
127  $translatedOffset = $this->‪translateToLongArgumentName($offset);
128  if ($translatedOffset === '') {
129  throw new ‪NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
130  }
131  return parent::offsetGet($translatedOffset);
132  }
133 
145  public function ‪addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
146  {
148  $argument = $this->objectManager->get(Argument::class, $name, $dataType);
149  $argument->‪setRequired($isRequired);
150  $argument->‪setDefaultValue($defaultValue);
151  $this->‪addArgument($argument);
152  return $argument;
153  }
154 
164  public function ‪addArgument(‪Argument $argument)
165  {
166  $this->‪offsetSet(null, $argument);
167  }
168 
176  public function ‪getArgument($argumentName)
177  {
178  if (!$this->‪offsetExists($argumentName)) {
179  throw new ‪NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
180  }
181  return $this->‪offsetGet($argumentName);
182  }
183 
191  public function ‪hasArgument($argumentName)
192  {
193  return $this->‪offsetExists($argumentName);
194  }
195 
201  public function ‪getArgumentNames()
202  {
203  return array_keys($this->argumentNames);
204  }
205 
211  public function ‪getArgumentShortNames()
212  {
215  foreach ($this as $argument) {
216  ‪$argumentShortNames[$argument->getShortName()] = true;
217  }
218  return array_keys(‪$argumentShortNames);
219  }
220 
229  public function ‪__call($methodName, array $arguments)
230  {
231  if (strpos($methodName, 'set') !== 0) {
232  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
233  }
234  $firstLowerCaseArgumentName = $this->‪translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
235  $firstUpperCaseArgumentName = $this->‪translateToLongArgumentName(ucfirst(substr($methodName, 3)));
236  if (in_array($firstLowerCaseArgumentName, $this->‪getArgumentNames())) {
237  $argument = parent::offsetGet($firstLowerCaseArgumentName);
238  $argument->‪setValue($arguments[0]);
239  } elseif (in_array($firstUpperCaseArgumentName, $this->‪getArgumentNames())) {
240  $argument = parent::offsetGet($firstUpperCaseArgumentName);
241  $argument->‪setValue($arguments[0]);
242  }
243  }
244 
255  protected function ‪translateToLongArgumentName($argumentName)
256  {
257  if (in_array($argumentName, $this->‪getArgumentNames())) {
258  return $argumentName;
259  }
261  foreach ($this as $argument) {
262  if ($argumentName === $argument->getShortName()) {
263  return $argument->getName();
264  }
265  }
266  return '';
267  }
268 
272  public function ‪removeAll()
273  {
274  foreach ($this->argumentNames as $argumentName => $booleanValue) {
275  parent::offsetUnset($argumentName);
276  }
277  $this->argumentNames = [];
278  }
279 
283  public function ‪validate(): ‪Result
284  {
285  $results = new ‪Result();
287  foreach ($this as $argument) {
288  $argumentValidationResults = $argument->validate();
289  if ($argumentValidationResults === null) {
290  continue;
291  }
292  $results->forProperty($argument->getName())->merge($argumentValidationResults);
293  }
294  return $results;
295  }
296 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:246
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setRequired
‪TYPO3 CMS Extbase Mvc Controller Argument setRequired($required)
Definition: Argument.php:178
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\hasArgument
‪bool hasArgument($argumentName)
Definition: Arguments.php:188
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgument
‪Argument getArgument($argumentName)
Definition: Arguments.php:173
‪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: AbstractController.php:16
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Arguments.php:30
‪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:280
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentShortNames
‪array $argumentShortNames
Definition: Arguments.php:38
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addNewArgument
‪Argument addNewArgument($name, $dataType='Text', $isRequired=false, $defaultValue=null)
Definition: Arguments.php:142
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setDefaultValue
‪TYPO3 CMS Extbase Mvc Controller Argument setDefaultValue($defaultValue)
Definition: Argument.php:200
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__construct
‪__construct()
Definition: Arguments.php:51
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetSet
‪offsetSet($offset, $value)
Definition: Arguments.php:64
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\translateToLongArgumentName
‪string translateToLongArgumentName($argumentName)
Definition: Arguments.php:252
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetUnset
‪offsetUnset($offset)
Definition: Arguments.php:93
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentNames
‪array $argumentNames
Definition: Arguments.php:34
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__call
‪__call($methodName, array $arguments)
Definition: Arguments.php:226
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetGet
‪Argument offsetGet($offset)
Definition: Arguments.php:122
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addArgument
‪addArgument(Argument $argument)
Definition: Arguments.php:161
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\append
‪append($value)
Definition: Arguments.php:80
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\removeAll
‪removeAll()
Definition: Arguments.php:269
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentShortNames
‪array getArgumentShortNames()
Definition: Arguments.php:208
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: Arguments.php:43
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentNames
‪array getArgumentNames()
Definition: Arguments.php:198
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
Definition: NoSuchArgumentException.php:26