‪TYPO3CMS  9.5
Arguments.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 class ‪Arguments extends \ArrayObject
22 {
26  protected ‪$objectManager;
27 
31  protected ‪$argumentNames = [];
32 
36  protected ‪$argumentShortNames = [];
37 
42  {
43  $this->objectManager = ‪$objectManager;
44  }
45 
49  public function ‪__construct()
50  {
51  parent::__construct();
52  }
53 
62  public function ‪offsetSet($offset, $value)
63  {
64  if (!$value instanceof ‪Argument) {
65  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
66  }
67  $argumentName = $value->getName();
68  parent::offsetSet($argumentName, $value);
69  $this->argumentNames[$argumentName] = true;
70  }
71 
78  public function ‪append($value)
79  {
80  if (!$value instanceof ‪Argument) {
81  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
82  }
83  $this->‪offsetSet(null, $value);
84  }
85 
91  public function ‪offsetUnset($offset)
92  {
93  $translatedOffset = $this->‪translateToLongArgumentName($offset);
94  parent::offsetUnset($translatedOffset);
95  unset($this->argumentNames[$translatedOffset]);
96  if ($offset != $translatedOffset) {
97  unset($this->argumentShortNames[$offset]);
98  }
99  }
100 
107  public function ‪offsetExists($offset)
108  {
109  $translatedOffset = $this->‪translateToLongArgumentName($offset);
110  return parent::offsetExists($translatedOffset);
111  }
112 
120  public function ‪offsetGet($offset)
121  {
122  $translatedOffset = $this->‪translateToLongArgumentName($offset);
123  if ($translatedOffset === '') {
124  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
125  }
126  return parent::offsetGet($translatedOffset);
127  }
128 
140  public function ‪addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
141  {
143  $argument = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, $name, $dataType);
144  $argument->‪setRequired($isRequired);
145  $argument->‪setDefaultValue($defaultValue);
146  $this->‪addArgument($argument);
147  return $argument;
148  }
149 
159  public function ‪addArgument(‪Argument $argument)
160  {
161  $this->‪offsetSet(null, $argument);
162  }
163 
171  public function ‪getArgument($argumentName)
172  {
173  if (!$this->‪offsetExists($argumentName)) {
174  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
175  }
176  return $this->‪offsetGet($argumentName);
177  }
178 
186  public function ‪hasArgument($argumentName)
187  {
188  return $this->‪offsetExists($argumentName);
189  }
190 
196  public function ‪getArgumentNames()
197  {
198  return array_keys($this->argumentNames);
199  }
200 
206  public function ‪getArgumentShortNames()
207  {
210  foreach ($this as $argument) {
211  ‪$argumentShortNames[$argument->getShortName()] = true;
212  }
213  return array_keys(‪$argumentShortNames);
214  }
215 
224  public function ‪__call($methodName, array $arguments)
225  {
226  if (strpos($methodName, 'set') !== 0) {
227  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
228  }
229  $firstLowerCaseArgumentName = $this->‪translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
230  $firstUpperCaseArgumentName = $this->‪translateToLongArgumentName(ucfirst(substr($methodName, 3)));
231  if (in_array($firstLowerCaseArgumentName, $this->‪getArgumentNames())) {
232  $argument = parent::offsetGet($firstLowerCaseArgumentName);
233  $argument->‪setValue($arguments[0]);
234  } elseif (in_array($firstUpperCaseArgumentName, $this->‪getArgumentNames())) {
235  $argument = parent::offsetGet($firstUpperCaseArgumentName);
236  $argument->‪setValue($arguments[0]);
237  }
238  }
239 
250  protected function ‪translateToLongArgumentName($argumentName)
251  {
252  if (in_array($argumentName, $this->‪getArgumentNames())) {
253  return $argumentName;
254  }
256  foreach ($this as $argument) {
257  if ($argumentName === $argument->getShortName()) {
258  return $argument->getName();
259  }
260  }
261  return '';
262  }
263 
267  public function ‪removeAll()
268  {
269  foreach ($this->argumentNames as $argumentName => $booleanValue) {
270  parent::offsetUnset($argumentName);
271  }
272  $this->argumentNames = [];
273  }
274 
281  public function ‪getValidationResults()
282  {
283  trigger_error(
284  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
285  E_USER_DEPRECATED
286  );
287 
288  return $this->‪validate();
289  }
290 
295  {
296  $results = new \TYPO3\CMS\Extbase\Error\Result();
298  foreach ($this as $argument) {
299  $argumentValidationResults = $argument->validate();
300  if ($argumentValidationResults === null) {
301  continue;
302  }
303  $results->forProperty($argument->getName())->merge($argumentValidationResults);
304  }
305  return $results;
306  }
307 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:243
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setRequired
‪TYPO3 CMS Extbase Mvc Controller Argument setRequired($required)
Definition: Argument.php:175
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\hasArgument
‪bool hasArgument($argumentName)
Definition: Arguments.php:183
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgument
‪Argument getArgument($argumentName)
Definition: Arguments.php:168
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetExists
‪bool offsetExists($offset)
Definition: Arguments.php:104
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:22
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getValidationResults
‪TYPO3 CMS Extbase Error Result getValidationResults()
Definition: Arguments.php:278
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Arguments.php:38
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: AbstractController.php:2
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Arguments.php:25
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:20
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\validate
‪TYPO3 CMS Extbase Error Result validate()
Definition: Arguments.php:291
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentShortNames
‪array $argumentShortNames
Definition: Arguments.php:33
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addNewArgument
‪Argument addNewArgument($name, $dataType='Text', $isRequired=false, $defaultValue=null)
Definition: Arguments.php:137
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setDefaultValue
‪TYPO3 CMS Extbase Mvc Controller Argument setDefaultValue($defaultValue)
Definition: Argument.php:197
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__construct
‪__construct()
Definition: Arguments.php:46
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetSet
‪offsetSet($offset, $value)
Definition: Arguments.php:59
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\translateToLongArgumentName
‪string translateToLongArgumentName($argumentName)
Definition: Arguments.php:247
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetUnset
‪offsetUnset($offset)
Definition: Arguments.php:88
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\$argumentNames
‪array $argumentNames
Definition: Arguments.php:29
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\__call
‪__call($methodName, array $arguments)
Definition: Arguments.php:221
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\offsetGet
‪Argument offsetGet($offset)
Definition: Arguments.php:117
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\addArgument
‪addArgument(Argument $argument)
Definition: Arguments.php:156
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\append
‪append($value)
Definition: Arguments.php:75
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\removeAll
‪removeAll()
Definition: Arguments.php:264
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentShortNames
‪array getArgumentShortNames()
Definition: Arguments.php:203
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments\getArgumentNames
‪array getArgumentNames()
Definition: Arguments.php:193
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:25