[!!!][~TASK] Extbase (Utility): Moved configureDispatcher() and registerPlugin()...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / Argument.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 controller argument
30 *
31 * @package Extbase
32 * @subpackage MVC\Controller
33 * @version $ID:$
34 * @scope prototype
35 */
36 class Tx_Extbase_MVC_Controller_Argument {
37
38 /**
39 * @var Tx_Extbase_Persistence_QueryFactory
40 */
41 protected $queryFactory;
42
43 /**
44 * @var Tx_Extbase_Property_Mapper
45 */
46 protected $propertyMapper;
47
48 /**
49 * Name of this argument
50 * @var string
51 */
52 protected $name = '';
53
54 /**
55 * Short name of this argument
56 * @var string
57 */
58 protected $shortName = NULL;
59
60 /**
61 * Data type of this argument's value
62 * @var string
63 */
64 protected $dataType = 'Text';
65
66 /**
67 * TRUE if this argument is required
68 * @var boolean
69 */
70 protected $isRequired = FALSE;
71
72 /**
73 * Actual value of this argument
74 * @var object
75 */
76 protected $value = NULL;
77
78 /**
79 * Default value. Used if argument is optional.
80 * @var mixed
81 */
82 protected $defaultValue = NULL;
83
84 /**
85 * A custom validator, used supplementary to the base validation
86 * @var Tx_Extbase_Validation_Validator_ValidatorInterface
87 */
88 protected $validator = NULL;
89
90 /**
91 * If validation for this argument is temporarily disabled
92 * @var boolean
93 */
94 protected $validationDisabled = FALSE;
95
96 /**
97 * Uid for the argument, if it has one
98 * @var string
99 */
100 protected $uid = NULL;
101
102 /**
103 * Constructs this controller argument
104 *
105 * @param string $name Name of this argument
106 * @param string $dataType The data type of this argument
107 * @throws InvalidArgumentException if $name is not a string or empty
108 * @api
109 */
110 public function __construct($name, $dataType = 'Text') {
111 $this->propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
112 $this->propertyMapper->injectReflectionService(t3lib_div::makeInstance('Tx_Extbase_Reflection_Service'));
113 if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
114 $this->name = $name;
115 if (is_array($dataType)) {
116 $this->setNewValidatorConjunction($dataType);
117 } else {
118 $this->setDataType($dataType);
119 }
120 }
121
122 /**
123 * Injects the Persistence Manager
124 *
125 * @param Tx_Extbase_Persistence_ManagerInterface
126 * @return void
127 */
128 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
129 $this->persistenceManager = $persistenceManager;
130 }
131
132 /**
133 * Injects a QueryFactory instance
134 *
135 * @param Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory
136 * @return void
137 */
138 public function injectQueryFactory(Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory) {
139 $this->queryFactory = $queryFactory;
140 }
141
142 /**
143 * Returns the name of this argument
144 *
145 * @return string This argument's name
146 * @api
147 */
148 public function getName() {
149 return $this->name;
150 }
151
152 /**
153 * Sets the short name of this argument.
154 *
155 * @param string $shortName A "short name" - a single character
156 * @return Tx_Extbase_MVC_Controller_Argument $this
157 * @throws InvalidArgumentException if $shortName is not a character
158 * @api
159 */
160 public function setShortName($shortName) {
161 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) !== 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
162 $this->shortName = $shortName;
163 return $this;
164 }
165
166 /**
167 * Returns the short name of this argument
168 *
169 * @return string This argument's short name
170 * @api
171 */
172 public function getShortName() {
173 return $this->shortName;
174 }
175
176 /**
177 * Sets the data type of this argument's value
178 *
179 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
180 * @return Tx_Extbase_MVC_Controller_Argument $this
181 * @api
182 */
183 public function setDataType($dataType) {
184 $this->dataType = $dataType;
185 return $this;
186 }
187
188 /**
189 * Returns the data type of this argument's value
190 *
191 * @return string The data type
192 * @api
193 */
194 public function getDataType() {
195 return $this->dataType;
196 }
197
198 /**
199 * Marks this argument to be required
200 *
201 * @param boolean $required TRUE if this argument should be required
202 * @return Tx_Extbase_MVC_Controller_Argument $this
203 * @api
204 */
205 public function setRequired($required) {
206 $this->isRequired = (boolean)$required;
207 return $this;
208 }
209
210 /**
211 * Returns TRUE if this argument is required
212 *
213 * @return boolean TRUE if this argument is required
214 * @api
215 */
216 public function isRequired() {
217 return $this->isRequired;
218 }
219
220 /**
221 * Sets the default value of the argument
222 *
223 * @param mixed $defaultValue Default value
224 * @return void
225 * @api
226 */
227 public function setDefaultValue($defaultValue) {
228 $this->defaultValue = $defaultValue;
229 }
230
231 /**
232 * Returns the default value of this argument
233 *
234 * @return mixed The default value
235 * @api
236 */
237 public function getDefaultValue() {
238 return $this->defaultValue;
239 }
240
241 /**
242 * Sets a custom validator which is used supplementary to the base validation
243 *
244 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
245 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
246 * @api
247 */
248 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
249 $this->validator = $validator;
250 return $this;
251 }
252
253 /**
254 * Create and set a validator chain
255 *
256 * @param array Object names of the validators
257 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
258 * @api
259 */
260 public function setNewValidatorConjunction(array $objectNames) {
261 if ($this->validator === NULL) {
262 $this->validator = t3lib_div::makeInstance('Tx_Extbase_Validation_Validator_ConjunctionValidator');
263 }
264 foreach ($objectNames as $objectName) {
265 if (!class_exists($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
266 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
267 }
268 return $this;
269 }
270
271 /**
272 * Returns the set validator
273 *
274 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
275 * @api
276 */
277 public function getValidator() {
278 return $this->validator;
279 }
280
281 /**
282 * Returns TRUE if validation is temporarily disabled for this argument and
283 * FALSE if it's enabled.
284 *
285 * Note that this is flag is only informational and does not have any real impact
286 * on other validation methods of this argument.
287 *
288 * @return boolean If validation is disabled
289 * @api
290 */
291 public function isValidationDisabled() {
292 return $this->validationDisabled;
293 }
294
295 /**
296 * Enables validation for this argument.
297 *
298 * @return void
299 * @api
300 */
301 public function enableValidation() {
302 $this->validationDisabled = FALSE;
303 }
304
305 /**
306 * Disables validation for this argument.
307 *
308 * @return void
309 * @api
310 */
311 public function disableValidation() {
312 $this->validationDisabled = TRUE;
313 }
314
315 /**
316 * Sets the value of this argument.
317 *
318 * @param mixed $value: The value of this argument
319 * @return Tx_Extbase_MVC_Controller_Argument $this
320 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
321 */
322 public function setValue($value) {
323 if (is_array($value)) {
324 if (isset($value['uid'])) {
325 $existingObject = $this->findObjectByUid($value['uid']);
326 if ($existingObject === FALSE) throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('Argument "' . $this->name . '": Querying the repository for the specified object was not sucessful.', 1237305720);
327 unset($value['uid']);
328 if (count($value) === 0) {
329 $value = $existingObject;
330 } elseif ($existingObject !== NULL) {
331 $newObject = clone $existingObject;
332 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
333 $value = $newObject;
334 }
335 }
336 } else {
337 $newObject = t3lib_div::makeInstance($this->dataType);
338 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
339 $value = $newObject;
340 }
341 }
342 }
343 $this->value = $value;
344 return $this;
345 }
346
347 /**
348 * Finds an object from the repository by searching for its technical UID.
349 *
350 * @param int $uid The object's uid
351 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
352 */
353 protected function findObjectByUid($uid) {
354 $query = $this->queryFactory->create($this->dataType);
355 $result = $query->matching($query->withUid($uid))->execute();
356 $object = NULL;
357 if (count($result) > 0) {
358 $object = current($result);
359 // TODO Check if the object is an Aggregate Root (this can be quite difficult because we have no Repository registration
360 $this->persistenceManager->getSession()->registerReconstitutedObject($object);
361 }
362 return $object;
363 }
364
365 /**
366 * Returns the value of this argument
367 *
368 * @return object The value of this argument - if none was set, NULL is returned
369 * @api
370 */
371 public function getValue() {
372 if ($this->value === NULL) {
373 return $this->defaultValue;
374 } else {
375 return $this->value;
376 }
377 }
378
379 /**
380 * Checks if this argument has a value set.
381 *
382 * @return boolean TRUE if a value was set, otherwise FALSE
383 */
384 public function isValue() {
385 return $this->value !== NULL;
386 }
387
388 /**
389 * Returns a string representation of this argument's value
390 *
391 * @return string
392 * @api
393 */
394 public function __toString() {
395 return (string)$this->value;
396 }
397 }
398 ?>