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