Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / Arguments.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A composite of controller arguments
30 *
31 * @package Extbase
32 * @subpackage MVC\Controller
33 * @version $ID:$
34 * @scope prototype
35 */
36 class Tx_Extbase_MVC_Controller_Arguments extends ArrayObject {
37
38 /**
39 * @var Tx_Extbase_Persistence_ManagerInterface
40 */
41 protected $persistenceManager;
42
43 /**
44 * @var Tx_Extbase_Persistence_QueryFactory
45 */
46 protected $queryFactory;
47
48 /**
49 * @var array Names of the arguments contained by this object
50 */
51 protected $argumentNames = array();
52
53 /**
54 * Injects the persistence manager
55 *
56 * @param Tx_Extbase_Persistence_ManagerInterface $persistenceManager
57 * @return void
58 */
59 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
60 $this->persistenceManager = $persistenceManager;
61 }
62
63 /**
64 * Injects a QueryFactory instance
65 *
66 * @param Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory
67 * @return void
68 */
69 public function injectQueryFactory(Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory) {
70 $this->queryFactory = $queryFactory;
71 }
72
73 /**
74 * Adds or replaces the argument specified by $value. The argument's name is taken from the
75 * argument object itself, therefore the $offset does not have any meaning in this context.
76 *
77 * @param mixed $offset Offset - not used here
78 * @param mixed $value The argument
79 * @return void
80 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object
81 */
82 public function offsetSet($offset, $value) {
83 if (!$value instanceof Tx_Extbase_MVC_Controller_Argument) throw new InvalidArgumentException('Controller arguments must be valid Tx_Extbase_MVC_Controller_Argument objects.', 1187953786);
84
85 $argumentName = $value->getName();
86 parent::offsetSet($argumentName, $value);
87 $this->argumentNames[$argumentName] = TRUE;
88 }
89
90 /**
91 * Sets an argument, aliased to offsetSet()
92 *
93 * @param mixed $value The value
94 * @return void
95 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object
96 */
97 public function append($value) {
98 if (!$value instanceof Tx_Extbase_MVC_Controller_Argument) throw new InvalidArgumentException('Controller arguments must be valid Tx_Extbase_MVC_Controller_Argument objects.', 1187953786);
99 $this->offsetSet(NULL, $value);
100 }
101
102 /**
103 * Unsets an argument
104 *
105 * @param mixed $offset Offset
106 * @return void
107 */
108 public function offsetUnset($offset) {
109 $translatedOffset = $this->translateToLongArgumentName($offset);
110 parent::offsetUnset($translatedOffset);
111
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 Tx_Extbase_MVC_Controller_Argument The requested argument object
134 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if the argument does not exist
135 */
136 public function offsetGet($offset) {
137 $translatedOffset = $this->translateToLongArgumentName($offset);
138 if ($translatedOffset === '') throw new Tx_Extbase_MVC_Exception_NoSuchArgument('The argument "' . $offset . '" does not exist.', 1216909923);
139 return parent::offsetGet($translatedOffset);
140 }
141
142 /**
143 * Creates, adds and returns a new controller argument to this composite object.
144 * If an argument with the same name exists already, it will be replaced by the
145 * new argument object.
146 *
147 * @param string $name Name of the argument
148 * @param string $dataType Name of one of the built-in data types
149 * @param boolean $isRequired TRUE if this argument should be marked as required
150 * @param mixed $defaultValue Default value of the argument. Only makes sense if $isRequired==FALSE
151 * @return Tx_Extbase_MVC_Controller_Argument The new argument
152 */
153 public function addNewArgument($name, $dataType = 'Text', $isRequired = FALSE, $defaultValue = NULL) {
154 $argument = $this->createArgument($name, $dataType);
155 $argument->setRequired($isRequired);
156 $argument->setDefaultValue($defaultValue);
157 $this->addArgument($argument);
158 return $argument;
159 }
160
161 /**
162 * Creates a new argument. This is a replacement for $this->objectFactory->create() of FLOW3.
163 *
164 * @param string $name Name of the argument
165 * @param string $dataType Name of one of the built-in data types
166 * @return Tx_Extbase_MVC_Controller_Argument The created argument
167 */
168 protected function createArgument($name, $dataType) {
169 $argument = new Tx_Extbase_MVC_Controller_Argument($name, $dataType);
170 $argument->injectPersistenceManager($this->persistenceManager);
171 $argument->injectQueryFactory($this->queryFactory);
172 $argument->initializeObject();
173 return $argument;
174 }
175
176 /**
177 * Adds the specified controller argument to this composite object.
178 * If an argument with the same name exists already, it will be replaced by the
179 * new argument object.
180 *
181 * Note that the argument will be cloned, not referenced.
182 *
183 * @param Tx_Extbase_MVC_Controller_Argument $argument The argument to add
184 * @return void
185 */
186 public function addArgument(Tx_Extbase_MVC_Controller_Argument $argument) {
187 $this->offsetSet(NULL, $argument);
188 }
189
190 /**
191 * Returns an argument specified by name
192 *
193 * @param string $argumentName Name of the argument to retrieve
194 * @return Tx_Extbase_MVC_Controller_Argument
195 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument
196 */
197 public function getArgument($argumentName) {
198 if (!$this->offsetExists($argumentName)) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist.', 1195815178);
199 return $this->offsetGet($argumentName);
200 }
201
202 /**
203 * Checks if an argument with the specified name exists
204 *
205 * @param string $argumentName Name of the argument to check for
206 * @return boolean TRUE if such an argument exists, otherwise FALSE
207 * @see offsetExists()
208 */
209 public function hasArgument($argumentName) {
210 return $this->offsetExists($argumentName);
211 }
212
213 /**
214 * Returns the names of all arguments contained in this object
215 *
216 * @return array Argument names
217 */
218 public function getArgumentNames() {
219 return array_keys($this->argumentNames);
220 }
221
222 /**
223 * Returns the short names of all arguments contained in this object that have one.
224 *
225 * @return array Argument short names
226 */
227 public function getArgumentShortNames() {
228 $argumentShortNames = array();
229 foreach ($this as $argument) {
230 $argumentShortNames[$argument->getShortName()] = TRUE;
231 }
232 return array_keys($argumentShortNames);
233 }
234
235 /**
236 * Magic setter method for the argument values. Each argument
237 * value can be set by just calling the setArgumentName() method.
238 *
239 * @param string $methodName Name of the method
240 * @param array $arguments Method arguments
241 * @return void
242 */
243 public function __call($methodName, array $arguments) {
244 if (substr($methodName, 0, 3) !== 'set') throw new LogicException('Unknown method "' . $methodName . '".', 1210858451);
245
246 $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName{3}) . substr($methodName, 4));
247 $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3)));
248
249 if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) {
250 $argument = parent::offsetGet($firstLowerCaseArgumentName);
251 $argument->setValue($arguments[0]);
252 } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) {
253 $argument = parent::offsetGet($firstUpperCaseArgumentName);
254 $argument->setValue($arguments[0]);
255 }
256 }
257
258 /**
259 * Translates a short argument name to its corresponding long name. If the
260 * specified argument name is a real argument name already, it will be returned again.
261 *
262 * If an argument with the specified name or short name does not exist, an empty
263 * string is returned.
264 *
265 * @param string argument name
266 * @return string long argument name or empty string
267 */
268 protected function translateToLongArgumentName($argumentName) {
269 if (in_array($argumentName, $this->getArgumentNames())) return $argumentName;
270
271 foreach ($this as $argument) {
272 if ($argumentName === $argument->getShortName()) return $argument->getName();
273 }
274 return '';
275 }
276
277 /**
278 * Remove all arguments and resets this object
279 *
280 * @return void
281 */
282 public function removeAll() {
283 foreach ($this->argumentNames as $argumentName => $booleanValue) {
284 parent::offsetUnset($argumentName);
285 }
286 $this->argumentNames = array();
287 }
288 }
289 ?>