TYPO3 CMS  TYPO3_7-6
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 
20 class Arguments extends \ArrayObject
21 {
25  protected $objectManager;
26 
30  protected $argumentNames = [];
31 
35  protected $argumentShortNames = [];
36 
40  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
41  {
42  $this->objectManager = $objectManager;
43  }
44 
48  public function __construct()
49  {
50  parent::__construct();
51  }
52 
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 
79  public function append($value)
80  {
81  if (!$value instanceof Argument) {
82  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
83  }
84  $this->offsetSet(null, $value);
85  }
86 
93  public function offsetUnset($offset)
94  {
95  $translatedOffset = $this->translateToLongArgumentName($offset);
96  parent::offsetUnset($translatedOffset);
97  unset($this->argumentNames[$translatedOffset]);
98  if ($offset != $translatedOffset) {
99  unset($this->argumentShortNames[$offset]);
100  }
101  }
102 
109  public function offsetExists($offset)
110  {
111  $translatedOffset = $this->translateToLongArgumentName($offset);
112  return parent::offsetExists($translatedOffset);
113  }
114 
122  public function offsetGet($offset)
123  {
124  $translatedOffset = $this->translateToLongArgumentName($offset);
125  if ($translatedOffset === '') {
126  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
127  }
128  return parent::offsetGet($translatedOffset);
129  }
130 
142  public function addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
143  {
145  $argument = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $name, $dataType);
146  $argument->setRequired($isRequired);
147  $argument->setDefaultValue($defaultValue);
148  $this->addArgument($argument);
149  return $argument;
150  }
151 
162  public function addArgument(Argument $argument)
163  {
164  $this->offsetSet(null, $argument);
165  }
166 
174  public function getArgument($argumentName)
175  {
176  if (!$this->offsetExists($argumentName)) {
177  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
178  }
179  return $this->offsetGet($argumentName);
180  }
181 
189  public function hasArgument($argumentName)
190  {
191  return $this->offsetExists($argumentName);
192  }
193 
199  public function getArgumentNames()
200  {
201  return array_keys($this->argumentNames);
202  }
203 
209  public function getArgumentShortNames()
210  {
211  $argumentShortNames = [];
213  foreach ($this as $argument) {
214  $argumentShortNames[$argument->getShortName()] = true;
215  }
216  return array_keys($argumentShortNames);
217  }
218 
228  public function __call($methodName, array $arguments)
229  {
230  if (substr($methodName, 0, 3) !== 'set') {
231  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
232  }
233  $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
234  $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3)));
235  if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) {
236  $argument = parent::offsetGet($firstLowerCaseArgumentName);
237  $argument->setValue($arguments[0]);
238  } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) {
239  $argument = parent::offsetGet($firstUpperCaseArgumentName);
240  $argument->setValue($arguments[0]);
241  }
242  }
243 
254  protected function translateToLongArgumentName($argumentName)
255  {
256  if (in_array($argumentName, $this->getArgumentNames())) {
257  return $argumentName;
258  }
260  foreach ($this as $argument) {
261  if ($argumentName === $argument->getShortName()) {
262  return $argument->getName();
263  }
264  }
265  return '';
266  }
267 
273  public function removeAll()
274  {
275  foreach ($this->argumentNames as $argumentName => $booleanValue) {
276  parent::offsetUnset($argumentName);
277  }
278  $this->argumentNames = [];
279  }
280 
286  public function getValidationResults()
287  {
288  $results = new \TYPO3\CMS\Extbase\Error\Result();
290  foreach ($this as $argument) {
291  $argumentValidationResults = $argument->getValidationResults();
292  if ($argumentValidationResults === null) {
293  continue;
294  }
295  $results->forProperty($argument->getName())->merge($argumentValidationResults);
296  }
297  return $results;
298  }
299 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Arguments.php:40
__call($methodName, array $arguments)
Definition: Arguments.php:228