Extbase:
[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 // SK setNewValidatorChain does not exist! I think this needs to be changed to setNewValidatorConjunction.
114 $this->setNewValidatorChain($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 * @internal
126 */
127 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
128 $this->persistenceManager = $persistenceManager;
129 }
130
131 /**
132 * Injects a QueryFactory instance
133 *
134 * @param Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory
135 * @return void
136 * @internal
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 */
147 public function getName() {
148 return $this->name;
149 }
150
151 /**
152 * Sets the short name of this argument.
153 *
154 * @param string $shortName A "short name" - a single character
155 * @return Tx_Extbase_MVC_Controller_Argument $this
156 * @throws InvalidArgumentException if $shortName is not a character
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 */
169 public function getShortName() {
170 return $this->shortName;
171 }
172
173 /**
174 * Sets the data type of this argument's value
175 *
176 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
177 * @return Tx_Extbase_MVC_Controller_Argument $this
178 */
179 public function setDataType($dataType) {
180 $this->dataType = $dataType;
181 return $this;
182 }
183
184 /**
185 * Returns the data type of this argument's value
186 *
187 * @return string The data type
188 */
189 public function getDataType() {
190 return $this->dataType;
191 }
192
193 /**
194 * Marks this argument to be required
195 *
196 * @param boolean $required TRUE if this argument should be required
197 * @return Tx_Extbase_MVC_Controller_Argument $this
198 */
199 public function setRequired($required) {
200 $this->isRequired = (boolean)$required;
201 return $this;
202 }
203
204 /**
205 * Returns TRUE if this argument is required
206 *
207 * @return boolean TRUE if this argument is required
208 */
209 public function isRequired() {
210 return $this->isRequired;
211 }
212
213 /**
214 * Sets the default value of the argument
215 *
216 * @param mixed $defaultValue Default value
217 * @return void
218 */
219 public function setDefaultValue($defaultValue) {
220 $this->defaultValue = $defaultValue;
221 }
222
223 /**
224 * Returns the default value of this argument
225 *
226 * @return mixed The default value
227 */
228 public function getDefaultValue() {
229 return $this->defaultValue;
230 }
231
232 /**
233 * Sets a custom validator which is used supplementary to the base validation
234 *
235 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
236 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
237 */
238 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
239 $this->validator = $validator;
240 return $this;
241 }
242
243 /**
244 * Create and set a validator chain
245 *
246 * @param array Object names of the validators
247 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
248 */
249 public function setNewValidatorConjunction(array $objectNames) {
250 if ($this->validator === NULL) {
251 $this->validator = t3lib_div::makeInstance('Tx_Extbase_Validation_Validator_ConjunctionValidator');
252 }
253 foreach ($objectNames as $objectName) {
254 if (!class_exists($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
255 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
256 }
257 return $this;
258 }
259 /**
260 * Returns the set validator
261 *
262 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
263 */
264 public function getValidator() {
265 return $this->validator;
266 }
267
268 /**
269 * Sets the value of this argument.
270 *
271 * @param mixed $value: The value of this argument
272 * @return Tx_Extbase_MVC_Controller_Argument $this
273 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
274 */
275 public function setValue($value) {
276 if (is_array($value)) {
277 if (isset($value['uid'])) {
278 $existingObject = $this->findObjectByUid($value['uid']);
279 if ($existingObject === FALSE) throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('Argument "' . $this->name . '": Querying the repository for the specified object was not sucessful.', 1237305720);
280 unset($value['uid']);
281 if (count($value) === 0) {
282 $value = $existingObject;
283 } elseif ($existingObject !== NULL) {
284 $newObject = clone $existingObject;
285 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
286 $value = $newObject;
287 }
288 }
289 } else {
290 $newObject = t3lib_div::makeInstance($this->dataType);
291 if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
292 $value = $newObject;
293 }
294 }
295 }
296 $this->value = $value;
297 return $this;
298 }
299
300 /**
301 * Finds an object from the repository by searching for its technical UID.
302 *
303 * @param int $uid The object's uid
304 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
305 */
306 protected function findObjectByUid($uid) {
307 $query = $this->queryFactory->create($this->dataType);
308 $object = current($query->matching($query->withUid($uid))->execute());
309 // TODO Check if the object is an Aggregate Root (this can be quite difficult because we have no Repository registration
310 if ($object !== NULL) {
311 $this->persistenceManager->getSession()->registerReconstitutedObject($object);
312 return $object;
313 } else {
314 return FALSE;
315 }
316 }
317
318 /**
319 * Returns the value of this argument
320 *
321 * @return object The value of this argument - if none was set, NULL is returned
322 */
323 public function getValue() {
324 if ($this->value === NULL) {
325 return $this->defaultValue;
326 } else {
327 return $this->value;
328 }
329 }
330
331 /**
332 * Checks if this argument has a value set.
333 *
334 * @return boolean TRUE if a value was set, otherwise FALSE
335 */
336 public function isValue() {
337 return $this->value !== NULL;
338 }
339
340 /**
341 * Returns a string representation of this argument's value
342 *
343 * @return string
344 */
345 public function __toString() {
346 return (string)$this->value;
347 }
348 }
349 ?>