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 script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A controller argument
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 * @scope prototype
32 */
33 class Tx_ExtBase_MVC_Controller_Argument {
34
35 /**
36 * Name of this argument
37 * @var string
38 */
39 protected $name = '';
40
41 /**
42 * Short name of this argument
43 * @var string
44 */
45 protected $shortName = NULL;
46
47 /**
48 * Data type of this argument's value
49 * @var string
50 */
51 protected $dataType = 'Text';
52
53 /**
54 * TRUE if this argument is required
55 * @var boolean
56 */
57 protected $isRequired = FALSE;
58
59 /**
60 * Actual value of this argument
61 * @var object
62 */
63 protected $value = NULL;
64
65 /**
66 * Default value. Used if argument is optional.
67 * @var mixed
68 */
69 protected $defaultValue = NULL;
70
71 /**
72 * The argument is valid
73 * @var boolean
74 */
75 protected $isValid = NULL;
76
77 /**
78 * Any error (Tx_ExtBase_Error_Error) that occured while initializing this argument (e.g. a mapping error)
79 * @var array
80 */
81 protected $errors = array();
82
83 /**
84 * The property validator for this argument
85 * @var Tx_ExtBase_Validation_Validator_ValidatorInterface
86 */
87 protected $validator = NULL;
88
89 /**
90 * The property validator for this arguments datatype
91 * @var Tx_ExtBase_Validation_Validator_ValidatorInterface
92 */
93 // TODO Remove DatatypeValidator
94 protected $datatypeValidator = NULL;
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 */
109 public function __construct($name, $dataType = 'Text') {
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->setNewValidatorChain($dataType);
114 } else {
115 $this->setDataType($dataType);
116 }
117 }
118
119 /**
120 * Returns the name of this argument
121 *
122 * @return string This argument's name
123 */
124 public function getName() {
125 return $this->name;
126 }
127
128 /**
129 * Sets the short name of this argument.
130 *
131 * @param string $shortName A "short name" - a single character
132 * @return Tx_ExtBase_MVC_Controller_Argument $this
133 * @throws InvalidArgumentException if $shortName is not a character
134 */
135 public function setShortName($shortName) {
136 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) != 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
137 $this->shortName = $shortName;
138 return $this;
139 }
140
141 /**
142 * Returns the short name of this argument
143 *
144 * @return string This argument's short name
145 */
146 public function getShortName() {
147 return $this->shortName;
148 }
149
150 /**
151 * Sets the default value of the argument
152 *
153 * @param mixed $defaultValue Default value
154 * @return void
155 */
156 public function setDefaultValue($defaultValue) {
157 $this->defaultValue = $defaultValue;
158 }
159
160 /**
161 * Sets the data type of this argument's value
162 *
163 * @param string $dataType: Name of the data type
164 * @return Tx_ExtBase_MVC_Controller_Argument $this
165 */
166 public function setDataType($dataType) {
167 $this->dataType = ($dataType != '' ? $dataType : 'Text');
168 // TODO Make validator path and class names configurable
169 $dataTypeValidatorClassName = 'Tx_ExtBase_Validation_Validator_' . $this->dataType;
170 $classFilePathAndName = t3lib_extMgm::extPath('extbase') . 'Classes/Validation/Validator/' . $this->dataType . '.php';
171 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
172 require_once($classFilePathAndName);
173 $this->datatypeValidator = t3lib_div::makeInstance($dataTypeValidatorClassName);
174 }
175 return $this;
176 }
177
178 /**
179 * Returns the data type of this argument's value
180 *
181 * @return string The data type
182 */
183 public function getDataType() {
184 return $this->dataType;
185 }
186
187 /**
188 * Marks this argument to be required
189 *
190 * @param boolean $required TRUE if this argument should be required
191 * @return Tx_ExtBase_MVC_Controller_Argument $this
192 */
193 public function setRequired($required) {
194 $this->isRequired = $required;
195 return $this;
196 }
197
198 /**
199 * Returns TRUE if this argument is required
200 *
201 * @return boolean TRUE if this argument is required
202 */
203 public function isRequired() {
204 return $this->isRequired;
205 }
206
207 /**
208 * Sets the value of this argument.
209 *
210 * @param mixed $value: The value of this argument
211 * @return Tx_ExtBase_MVC_Controller_Argument $this
212 * @throws Tx_ExtBase_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
213 */
214 public function setValue($value) {
215 if ($this->isValidValueForThisArgument($value)) {
216 $this->value = $value;
217 }
218 return $this;
219 }
220
221 /**
222 * Returns the value of this argument
223 *
224 * @return object The value of this argument - if none was set, NULL is returned
225 */
226 public function getValue() {
227 if ($this->value === NULL) {
228 return $this->defaultValue;
229 } else {
230 return $this->value;
231 }
232 }
233
234 /**
235 * Checks if this argument has a value set.
236 *
237 * @return boolean TRUE if a value was set, otherwise FALSE
238 */
239 public function isValue() {
240 return $this->value !== NULL;
241 }
242
243 /**
244 * undocumented function
245 *
246 * @param string $value
247 * @return boolean TRUE if the value is valid for this argument, otherwise FALSE
248 */
249 protected function isValidValueForThisArgument($value) {
250 $isValid = TRUE;
251 $validatorErrors = t3lib_div::makeInstance('Tx_ExtBase_Validation_Errors');
252 // TODO use only Validator; do not distinguish between Validator and DatatypeValidator
253 if ($this->getValidator() !== NULL) {
254 $isValid &= $this->getValidator()->isValid($value, $validatorErrors);
255 } elseif ($this->getDatatypeValidator() !== NULL) {
256 $isValid = $this->getDatatypeValidator()->isValid($value, $validatorErrors);
257 } else {
258 throw new Tx_ExtBase_Validation_Exception_NoValidatorFound('No appropriate validator for the argument "' . $this->getName() . '" was found.', 1235748909);
259 }
260 if (!$isValid) {
261 foreach ($validatorErrors as $error) {
262 $this->addError($error);
263 }
264 }
265 $this->isValid = $isValid;
266 return (boolean)$isValid;
267 }
268
269 /**
270 * Returns TRUE when the argument is valid
271 *
272 * @return boolean TRUE if the argument is valid
273 */
274 public function isValid() {
275 return $this->isValid;
276 }
277
278 /**
279 * Add an initialization error (e.g. a mapping error)
280 *
281 * @param string An error text
282 * @return void
283 */
284 public function addError($error) {
285 $this->errors[] = $error;
286 }
287
288 /**
289 * Get all initialization errors
290 *
291 * @return array An array containing Tx_ExtBase_Error_Error objects
292 * @see addError(Tx_ExtBase_Error_Error $error)
293 */
294 public function getErrors() {
295 return $this->errors;
296 }
297
298 /**
299 * Set an additional validator
300 *
301 * @param string Class name of a validator
302 * @return Tx_ExtBase_MVC_Controller_Argument Returns $this (used for fluent interface)
303 */
304 public function setValidator($className) {
305 $this->validator = t3lib_div::makeInstance($className);
306 return $this;
307 }
308
309 /**
310 * Returns the set validator
311 *
312 * @return Tx_ExtBase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
313 */
314 public function getValidator() {
315 return $this->validator;
316 }
317
318 /**
319 * Returns the set datatype validator
320 *
321 * @return Tx_ExtBase_Validation_Validator_ValidatorInterface The set datatype validator
322 */
323 public function getDatatypeValidator() {
324 return $this->datatypeValidator;
325 }
326
327 /**
328 * Create and set a validator chain
329 *
330 * @param array Object names of the validators
331 * @return Tx_ExtBase_MVC_Controller_Argument Returns $this (used for fluent interface)
332 */
333 public function setNewValidatorChain(array $validators) {
334 $this->validator = t3lib_div::makeInstance('Tx_ExtBase_Validation_Validator_ChainValidator');
335 foreach ($validators as $validator) {
336 if (is_array($validator)) {
337 $objectName = 'Tx_ExtBase_Validation_Validator_' . $validator[0];
338 $this->validator->addValidator(new $objectName);
339 } else {
340 $objectName = 'Tx_ExtBase_Validation_Validator_' . $validator;
341 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
342 }
343 }
344 return $this;
345 }
346
347 /**
348 * Set the uid for the argument.
349 *
350 * @param string $uid The uid for the argument.
351 * @return void
352 */
353 public function setUid($uid) {
354 $this->uid = $uid;
355 }
356
357 /**
358 * Get the uid of the argument, if it has one.
359 *
360 * @return string Uid of the argument. If none set, returns NULL.
361 */
362 public function getUid() {
363 return $this->uid;
364 }
365
366 /**
367 * Returns a string representation of this argument's value
368 *
369 * @return string
370 */
371 public function __toString() {
372 return (string)$this->value;
373 }
374 }
375 ?>