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