[TASK] Use fully qualified name resolution in PHP 5.5
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / Arguments.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
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 /**
17 * A composite of controller arguments
18 */
19 class Arguments extends \ArrayObject {
20
21 /**
22 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
23 * @inject
24 */
25 protected $objectManager;
26
27 /**
28 * @var array Names of the arguments contained by this object
29 */
30 protected $argumentNames = array();
31
32 /**
33 * @var array
34 */
35 protected $argumentShortNames = array();
36
37 /**
38 * Constructor. If this one is removed, reflection breaks.
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Adds or replaces the argument specified by $value. The argument's name is taken from the
46 * argument object itself, therefore the $offset does not have any meaning in this context.
47 *
48 * @param mixed $offset Offset - not used here
49 * @param mixed $value The argument
50 * @throws \InvalidArgumentException if the argument is not a valid Controller Argument object
51 * @return void
52 */
53 public function offsetSet($offset, $value) {
54 if (!$value instanceof Argument) {
55 throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
56 }
57 $argumentName = $value->getName();
58 parent::offsetSet($argumentName, $value);
59 $this->argumentNames[$argumentName] = TRUE;
60 }
61
62 /**
63 * Sets an argument, aliased to offsetSet()
64 *
65 * @param mixed $value The value
66 * @throws \InvalidArgumentException if the argument is not a valid Controller Argument object
67 * @return void
68 */
69 public function append($value) {
70 if (!$value instanceof Argument) {
71 throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787);
72 }
73 $this->offsetSet(NULL, $value);
74 }
75
76 /**
77 * Unsets an argument
78 *
79 * @param mixed $offset Offset
80 * @return void
81 */
82 public function offsetUnset($offset) {
83 $translatedOffset = $this->translateToLongArgumentName($offset);
84 parent::offsetUnset($translatedOffset);
85 unset($this->argumentNames[$translatedOffset]);
86 if ($offset != $translatedOffset) {
87 unset($this->argumentShortNames[$offset]);
88 }
89 }
90
91 /**
92 * Returns whether the requested index exists
93 *
94 * @param mixed $offset Offset
95 * @return bool
96 */
97 public function offsetExists($offset) {
98 $translatedOffset = $this->translateToLongArgumentName($offset);
99 return parent::offsetExists($translatedOffset);
100 }
101
102 /**
103 * Returns the value at the specified index
104 *
105 * @param mixed $offset Offset
106 * @return Argument The requested argument object
107 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if the argument does not exist
108 */
109 public function offsetGet($offset) {
110 $translatedOffset = $this->translateToLongArgumentName($offset);
111 if ($translatedOffset === '') {
112 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923);
113 }
114 return parent::offsetGet($translatedOffset);
115 }
116
117 /**
118 * Creates, adds and returns a new controller argument to this composite object.
119 * If an argument with the same name exists already, it will be replaced by the
120 * new argument object.
121 *
122 * @param string $name Name of the argument
123 * @param string $dataType Name of one of the built-in data types
124 * @param bool $isRequired TRUE if this argument should be marked as required
125 * @param mixed $defaultValue Default value of the argument. Only makes sense if $isRequired==FALSE
126 * @return Argument The new argument
127 */
128 public function addNewArgument($name, $dataType = 'Text', $isRequired = FALSE, $defaultValue = NULL) {
129 /** @var $argument Argument */
130 $argument = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $name, $dataType);
131 $argument->setRequired($isRequired);
132 $argument->setDefaultValue($defaultValue);
133 $this->addArgument($argument);
134 return $argument;
135 }
136
137 /**
138 * Adds the specified controller argument to this composite object.
139 * If an argument with the same name exists already, it will be replaced by the
140 * new argument object.
141 *
142 * Note that the argument will be cloned, not referenced.
143 *
144 * @param Argument $argument The argument to add
145 * @return void
146 */
147 public function addArgument(Argument $argument) {
148 $this->offsetSet(NULL, $argument);
149 }
150
151 /**
152 * Returns an argument specified by name
153 *
154 * @param string $argumentName Name of the argument to retrieve
155 * @return Argument
156 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
157 */
158 public function getArgument($argumentName) {
159 if (!$this->offsetExists($argumentName)) {
160 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178);
161 }
162 return $this->offsetGet($argumentName);
163 }
164
165 /**
166 * Checks if an argument with the specified name exists
167 *
168 * @param string $argumentName Name of the argument to check for
169 * @return bool TRUE if such an argument exists, otherwise FALSE
170 * @see offsetExists()
171 */
172 public function hasArgument($argumentName) {
173 return $this->offsetExists($argumentName);
174 }
175
176 /**
177 * Returns the names of all arguments contained in this object
178 *
179 * @return array Argument names
180 */
181 public function getArgumentNames() {
182 return array_keys($this->argumentNames);
183 }
184
185 /**
186 * Returns the short names of all arguments contained in this object that have one.
187 *
188 * @return array Argument short names
189 */
190 public function getArgumentShortNames() {
191 $argumentShortNames = array();
192 /** @var Argument $argument */
193 foreach ($this as $argument) {
194 $argumentShortNames[$argument->getShortName()] = TRUE;
195 }
196 return array_keys($argumentShortNames);
197 }
198
199 /**
200 * Magic setter method for the argument values. Each argument
201 * value can be set by just calling the setArgumentName() method.
202 *
203 * @param string $methodName Name of the method
204 * @param array $arguments Method arguments
205 * @throws \LogicException
206 * @return void
207 */
208 public function __call($methodName, array $arguments) {
209 if (substr($methodName, 0, 3) !== 'set') {
210 throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451);
211 }
212 $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
213 $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3)));
214 if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) {
215 $argument = parent::offsetGet($firstLowerCaseArgumentName);
216 $argument->setValue($arguments[0]);
217 } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) {
218 $argument = parent::offsetGet($firstUpperCaseArgumentName);
219 $argument->setValue($arguments[0]);
220 }
221 }
222
223 /**
224 * Translates a short argument name to its corresponding long name. If the
225 * specified argument name is a real argument name already, it will be returned again.
226 *
227 * If an argument with the specified name or short name does not exist, an empty
228 * string is returned.
229 *
230 * @param string $argumentName argument name
231 * @return string long argument name or empty string
232 */
233 protected function translateToLongArgumentName($argumentName) {
234 if (in_array($argumentName, $this->getArgumentNames())) {
235 return $argumentName;
236 }
237 /** @var Argument $argument */
238 foreach ($this as $argument) {
239 if ($argumentName === $argument->getShortName()) {
240 return $argument->getName();
241 }
242 }
243 return '';
244 }
245
246 /**
247 * Remove all arguments and resets this object
248 *
249 * @return void
250 */
251 public function removeAll() {
252 foreach ($this->argumentNames as $argumentName => $booleanValue) {
253 parent::offsetUnset($argumentName);
254 }
255 $this->argumentNames = array();
256 }
257
258 /**
259 * Get all property mapping / validation errors
260 *
261 * @return \TYPO3\CMS\Extbase\Error\Result
262 */
263 public function getValidationResults() {
264 $results = new \TYPO3\CMS\Extbase\Error\Result();
265 /** @var Argument $argument */
266 foreach ($this as $argument) {
267 $argumentValidationResults = $argument->getValidationResults();
268 if ($argumentValidationResults === NULL) {
269 continue;
270 }
271 $results->forProperty($argument->getName())->merge($argumentValidationResults);
272 }
273 return $results;
274 }
275 }