[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / Arguments.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * A composite of controller arguments
31 *
32 * @package Extbase
33 * @subpackage MVC\Controller
34 * @version $ID:$
35 * @scope prototype
36 */
37 class Arguments extends \ArrayObject {
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * @var array Names of the arguments contained by this object
46 */
47 protected $argumentNames = array();
48
49 /**
50 * @var array
51 */
52 protected $argumentShortNames = array();
53
54 /**
55 * Constructor. If this one is removed, reflection breaks.
56 */
57 public function __construct() {
58 parent::__construct();
59 }
60
61 /**
62 * Injects the object manager
63 *
64 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
65 * @return void
66 */
67 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
68 $this->objectManager = $objectManager;
69 }
70
71 /**
72 * Adds or replaces the argument specified by $value. The argument's name is taken from the
73 * argument object itself, therefore the $offset does not have any meaning in this context.
74 *
75 * @param mixed $offset Offset - not used here
76 * @param mixed $value The argument
77 * @return void
78 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object
79 */
80 public function offsetSet($offset, $value) {
81 if (!$value instanceof \TYPO3\CMS\Extbase\Mvc\Controller\Argument) {
82 throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
83 }
84 $argumentName = $value->getName();
85 parent::offsetSet($argumentName, $value);
86 $this->argumentNames[$argumentName] = TRUE;
87 }
88
89 /**
90 * Sets an argument, aliased to offsetSet()
91 *
92 * @param mixed $value The value
93 * @return void
94 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object
95 */
96 public function append($value) {
97 if (!$value instanceof \TYPO3\CMS\Extbase\Mvc\Controller\Argument) {
98 throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786);
99 }
100 $this->offsetSet(NULL, $value);
101 }
102
103 /**
104 * Unsets an argument
105 *
106 * @param mixed $offset Offset
107 * @return void
108 */
109 public function offsetUnset($offset) {
110 $translatedOffset = $this->translateToLongArgumentName($offset);
111 parent::offsetUnset($translatedOffset);
112 unset($this->argumentNames[$translatedOffset]);
113 if ($offset != $translatedOffset) {
114 unset($this->argumentShortNames[$offset]);
115 }
116 }
117
118 /**
119 * Returns whether the requested index exists
120 *
121 * @param mixed $offset Offset
122 * @return boolean
123 */
124 public function offsetExists($offset) {
125 $translatedOffset = $this->translateToLongArgumentName($offset);
126 return parent::offsetExists($translatedOffset);
127 }
128
129 /**
130 * Returns the value at the specified index
131 *
132 * @param mixed $offset Offset
133 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument The requested argument object
134 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if the argument does not exist
135 */
136 public function offsetGet($offset) {
137 $translatedOffset = $this->translateToLongArgumentName($offset);
138 if ($translatedOffset === '') {
139 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException(('The argument "' . $offset) . '" does not exist.', 1216909923);
140 }
141 return parent::offsetGet($translatedOffset);
142 }
143
144 /**
145 * Creates, adds and returns a new controller argument to this composite object.
146 * If an argument with the same name exists already, it will be replaced by the
147 * new argument object.
148 *
149 * @param string $name Name of the argument
150 * @param string $dataType Name of one of the built-in data types
151 * @param boolean $isRequired TRUE if this argument should be marked as required
152 * @param mixed $defaultValue Default value of the argument. Only makes sense if $isRequired==FALSE
153 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument The new argument
154 */
155 public function addNewArgument($name, $dataType = 'Text', $isRequired = FALSE, $defaultValue = NULL) {
156 /** @var $argument \TYPO3\CMS\Extbase\Mvc\Controller\Argument */
157 $argument = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', $name, $dataType);
158 $argument->setRequired($isRequired);
159 $argument->setDefaultValue($defaultValue);
160 $this->addArgument($argument);
161 return $argument;
162 }
163
164 /**
165 * Adds the specified controller argument to this composite object.
166 * If an argument with the same name exists already, it will be replaced by the
167 * new argument object.
168 *
169 * Note that the argument will be cloned, not referenced.
170 *
171 * @param \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument The argument to add
172 * @return void
173 */
174 public function addArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument) {
175 $this->offsetSet(NULL, $argument);
176 }
177
178 /**
179 * Returns an argument specified by name
180 *
181 * @param string $argumentName Name of the argument to retrieve
182 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument
183 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
184 */
185 public function getArgument($argumentName) {
186 if (!$this->offsetExists($argumentName)) {
187 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException(('An argument "' . $argumentName) . '" does not exist.', 1195815178);
188 }
189 return $this->offsetGet($argumentName);
190 }
191
192 /**
193 * Checks if an argument with the specified name exists
194 *
195 * @param string $argumentName Name of the argument to check for
196 * @return boolean TRUE if such an argument exists, otherwise FALSE
197 * @see offsetExists()
198 */
199 public function hasArgument($argumentName) {
200 return $this->offsetExists($argumentName);
201 }
202
203 /**
204 * Returns the names of all arguments contained in this object
205 *
206 * @return array Argument names
207 */
208 public function getArgumentNames() {
209 return array_keys($this->argumentNames);
210 }
211
212 /**
213 * Returns the short names of all arguments contained in this object that have one.
214 *
215 * @return array Argument short names
216 */
217 public function getArgumentShortNames() {
218 $argumentShortNames = array();
219 foreach ($this as $argument) {
220 $argumentShortNames[$argument->getShortName()] = TRUE;
221 }
222 return array_keys($argumentShortNames);
223 }
224
225 /**
226 * Magic setter method for the argument values. Each argument
227 * value can be set by just calling the setArgumentName() method.
228 *
229 * @param string $methodName Name of the method
230 * @param array $arguments Method arguments
231 * @return void
232 */
233 public function __call($methodName, array $arguments) {
234 if (substr($methodName, 0, 3) !== 'set') {
235 throw new \LogicException(('Unknown method "' . $methodName) . '".', 1210858451);
236 }
237 $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4));
238 $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3)));
239 if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) {
240 $argument = parent::offsetGet($firstLowerCaseArgumentName);
241 $argument->setValue($arguments[0]);
242 } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) {
243 $argument = parent::offsetGet($firstUpperCaseArgumentName);
244 $argument->setValue($arguments[0]);
245 }
246 }
247
248 /**
249 * Translates a short argument name to its corresponding long name. If the
250 * specified argument name is a real argument name already, it will be returned again.
251 *
252 * If an argument with the specified name or short name does not exist, an empty
253 * string is returned.
254 *
255 * @param string argument name
256 * @return string long argument name or empty string
257 */
258 protected function translateToLongArgumentName($argumentName) {
259 if (in_array($argumentName, $this->getArgumentNames())) {
260 return $argumentName;
261 }
262 foreach ($this as $argument) {
263 if ($argumentName === $argument->getShortName()) {
264 return $argument->getName();
265 }
266 }
267 return '';
268 }
269
270 /**
271 * Remove all arguments and resets this object
272 *
273 * @return void
274 */
275 public function removeAll() {
276 foreach ($this->argumentNames as $argumentName => $booleanValue) {
277 parent::offsetUnset($argumentName);
278 }
279 $this->argumentNames = array();
280 }
281
282 /**
283 * Get all property mapping / validation errors
284 *
285 * @return \TYPO3\CMS\Extbase\Error\Result
286 */
287 public function getValidationResults() {
288 $results = new \TYPO3\CMS\Extbase\Error\Result();
289 foreach ($this as $argument) {
290 $argumentValidationResults = $argument->getValidationResults();
291 if ($argumentValidationResults === NULL) {
292 continue;
293 }
294 $results->forProperty($argument->getName())->merge($argumentValidationResults);
295 }
296 return $results;
297 }
298
299 }
300
301
302 ?>