[TASK] Extbase (Persistence): Removed PreparedQuery for now, as it is not used at...
[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_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 * @api
108 */
109 public function __construct($name, $dataType = 'Text') {
110 $this->propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
111 if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
112 $this->name = $name;
113 if (is_array($dataType)) {
114 $this->setNewValidatorConjunction($dataType);
115 } else {
116 $this->setDataType($dataType);
117 }
118 }
119
120 /**
121 * Injects the Persistence Manager
122 *
123 * @param Tx_Extbase_Persistence_ManagerInterface
124 * @return void
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 */
136 public function injectQueryFactory(Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory) {
137 $this->queryFactory = $queryFactory;
138 }
139
140 /**
141 * Returns the name of this argument
142 *
143 * @return string This argument's name
144 * @api
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 * @api
157 */
158 public function setShortName($shortName) {
159 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) !== 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
160 $this->shortName = $shortName;
161 return $this;
162 }
163
164 /**
165 * Returns the short name of this argument
166 *
167 * @return string This argument's short name
168 * @api
169 */
170 public function getShortName() {
171 return $this->shortName;
172 }
173
174 /**
175 * Sets the data type of this argument's value
176 *
177 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
178 * @return Tx_Extbase_MVC_Controller_Argument $this
179 * @api
180 */
181 public function setDataType($dataType) {
182 $this->dataType = $dataType;
183 return $this;
184 }
185
186 /**
187 * Returns the data type of this argument's value
188 *
189 * @return string The data type
190 * @api
191 */
192 public function getDataType() {
193 return $this->dataType;
194 }
195
196 /**
197 * Marks this argument to be required
198 *
199 * @param boolean $required TRUE if this argument should be required
200 * @return Tx_Extbase_MVC_Controller_Argument $this
201 * @api
202 */
203 public function setRequired($required) {
204 $this->isRequired = (boolean)$required;
205 return $this;
206 }
207
208 /**
209 * Returns TRUE if this argument is required
210 *
211 * @return boolean TRUE if this argument is required
212 * @api
213 */
214 public function isRequired() {
215 return $this->isRequired;
216 }
217
218 /**
219 * Sets the default value of the argument
220 *
221 * @param mixed $defaultValue Default value
222 * @return void
223 * @api
224 */
225 public function setDefaultValue($defaultValue) {
226 $this->defaultValue = $defaultValue;
227 }
228
229 /**
230 * Returns the default value of this argument
231 *
232 * @return mixed The default value
233 * @api
234 */
235 public function getDefaultValue() {
236 return $this->defaultValue;
237 }
238
239 /**
240 * Sets a custom validator which is used supplementary to the base validation
241 *
242 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
243 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
244 * @api
245 */
246 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
247 $this->validator = $validator;
248 return $this;
249 }
250
251 /**
252 * Create and set a validator chain
253 *
254 * @param array Object names of the validators
255 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
256 * @api
257 */
258 public function setNewValidatorConjunction(array $objectNames) {
259 if ($this->validator === NULL) {
260 $this->validator = t3lib_div::makeInstance('Tx_Extbase_Validation_Validator_ConjunctionValidator');
261 }
262 foreach ($objectNames as $objectName) {
263 if (!class_exists($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
264 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
265 }
266 return $this;
267 }
268
269 /**
270 * Returns the set validator
271 *
272 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
273 * @api
274 */
275 public function getValidator() {
276 return $this->validator;
277 }
278
279 /**
280 * Sets the value of this argument.
281 *
282 * @param mixed $value: The value of this argument
283 * @return Tx_Extbase_MVC_Controller_Argument $this
284 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
285 */
286 public function setValue($value) {
287 if (is_array($value)) {
288 if (isset($value['uid'])) {
289 $existingObject = $this->findObjectByUid($value['uid']);
290 if ($existingObject === FALSE) throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('Argument "' . $this->name . '": Querying the repository for the specified object was not sucessful.', 1237305720);
291 unset($value['uid']);
292 if (count($value) === 0) {
293 $value = $existingObject;
294 } elseif ($existingObject !== NULL) {
295 $newObject = clone $existingObject;
296 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
297 $value = $newObject;
298 }
299 }
300 } else {
301 $newObject = t3lib_div::makeInstance($this->dataType);
302 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
303 $value = $newObject;
304 }
305 }
306 }
307 $this->value = $value;
308 return $this;
309 }
310
311 /**
312 * Finds an object from the repository by searching for its technical UID.
313 *
314 * @param int $uid The object's uid
315 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
316 */
317 protected function findObjectByUid($uid) {
318 $query = $this->queryFactory->create($this->dataType);
319 $result = $query->matching($query->withUid($uid))->execute();
320 $object = NULL;
321 if (count($result) > 0) {
322 $object = current($result);
323 // TODO Check if the object is an Aggregate Root (this can be quite difficult because we have no Repository registration
324 $this->persistenceManager->getSession()->registerReconstitutedObject($object);
325 }
326 return $object;
327 }
328
329 /**
330 * Returns the value of this argument
331 *
332 * @return object The value of this argument - if none was set, NULL is returned
333 * @api
334 */
335 public function getValue() {
336 if ($this->value === NULL) {
337 return $this->defaultValue;
338 } else {
339 return $this->value;
340 }
341 }
342
343 /**
344 * Checks if this argument has a value set.
345 *
346 * @return boolean TRUE if a value was set, otherwise FALSE
347 */
348 public function isValue() {
349 return $this->value !== NULL;
350 }
351
352 /**
353 * Returns a string representation of this argument's value
354 *
355 * @return string
356 * @api
357 */
358 public function __toString() {
359 return (string)$this->value;
360 }
361 }
362 ?>