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