TYPO3 CMS  TYPO3_8-7
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 
61  public function offsetSet($offset, $value)
62  {
63  if (!$value instanceof Argument) {
64  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
65  }
66  $argumentName = $value->getName();
67  parent::offsetSet($argumentName, $value);
68  $this->argumentNames[$argumentName] = true;
69  }
70 
77  public function append($value)
78  {
79  if (!$value instanceof Argument) {
80  throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
81  }
82  $this->offsetSet(null, $value);
83  }
84 
90  public function offsetUnset($offset)
91  {
92  $translatedOffset = $this->translateToLongArgumentName($offset);
93  parent::offsetUnset($translatedOffset);
94  unset($this->argumentNames[$translatedOffset]);
95  if ($offset != $translatedOffset) {
96  unset($this->argumentShortNames[$offset]);
97  }
98  }
99 
106  public function offsetExists($offset)
107  {
108  $translatedOffset = $this->translateToLongArgumentName($offset);
109  return parent::offsetExists($translatedOffset);
110  }
111 
119  public function offsetGet($offset)
120  {
121  $translatedOffset = $this->translateToLongArgumentName($offset);
122  if ($translatedOffset === '') {
123  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
124  }
125  return parent::offsetGet($translatedOffset);
126  }
127 
139  public function addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null)
140  {
142  $argument = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $name, $dataType);
143  $argument->setRequired($isRequired);
144  $argument->setDefaultValue($defaultValue);
145  $this->addArgument($argument);
146  return $argument;
147  }
148 
158  public function addArgument(Argument $argument)
159  {
160  $this->offsetSet(null, $argument);
161  }
162 
170  public function getArgument($argumentName)
171  {
172  if (!$this->offsetExists($argumentName)) {
173  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
174  }
175  return $this->offsetGet($argumentName);
176  }
177 
185  public function hasArgument($argumentName)
186  {
187  return $this->offsetExists($argumentName);
188  }
189 
195  public function getArgumentNames()
196  {
197  return array_keys($this->argumentNames);
198  }
199 
205  public function getArgumentShortNames()
206  {
207  $argumentShortNames = [];
209  foreach ($this as $argument) {
210  $argumentShortNames[$argument->getShortName()] = true;
211  }
212  return array_keys($argumentShortNames);
213  }
214 
223  public function __call($methodName, array $arguments)
224  {
225  if (substr($methodName, 0, 3) !== 'set') {
226  throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
227  }
228  $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
229  $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3)));
230  if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) {
231  $argument = parent::offsetGet($firstLowerCaseArgumentName);
232  $argument->setValue($arguments[0]);
233  } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) {
234  $argument = parent::offsetGet($firstUpperCaseArgumentName);
235  $argument->setValue($arguments[0]);
236  }
237  }
238 
249  protected function translateToLongArgumentName($argumentName)
250  {
251  if (in_array($argumentName, $this->getArgumentNames())) {
252  return $argumentName;
253  }
255  foreach ($this as $argument) {
256  if ($argumentName === $argument->getShortName()) {
257  return $argument->getName();
258  }
259  }
260  return '';
261  }
262 
266  public function removeAll()
267  {
268  foreach ($this->argumentNames as $argumentName => $booleanValue) {
269  parent::offsetUnset($argumentName);
270  }
271  $this->argumentNames = [];
272  }
273 
279  public function getValidationResults()
280  {
281  $results = new \TYPO3\CMS\Extbase\Error\Result();
283  foreach ($this as $argument) {
284  $argumentValidationResults = $argument->getValidationResults();
285  if ($argumentValidationResults === null) {
286  continue;
287  }
288  $results->forProperty($argument->getName())->merge($argumentValidationResults);
289  }
290  return $results;
291  }
292 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Arguments.php:40
__call($methodName, array $arguments)
Definition: Arguments.php:223