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 = FALSE;
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 * Uid for the argument, if it has one
91 * @var string
92 */
93 protected $uid = NULL;
94
95 /**
96 * Constructs this controller argument
97 *
98 * @param string $name Name of this argument
99 * @param string $dataType The data type of this argument
100 * @throws InvalidArgumentException if $name is not a string or empty
101 */
102 public function __construct($name, $dataType = 'Text') {
103 if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
104 $this->name = $name;
105 if (is_array($dataType)) {
106 $this->setNewValidatorChain($dataType);
107 } else {
108 $this->setDataType($dataType);
109 }
110 }
111
112 /**
113 * Returns the name of this argument
114 *
115 * @return string This argument's name
116 */
117 public function getName() {
118 return $this->name;
119 }
120
121 /**
122 * Sets the short name of this argument.
123 *
124 * @param string $shortName A "short name" - a single character
125 * @return Tx_Extbase_MVC_Controller_Argument $this
126 * @throws InvalidArgumentException if $shortName is not a character
127 */
128 public function setShortName($shortName) {
129 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) != 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
130 $this->shortName = $shortName;
131 return $this;
132 }
133
134 /**
135 * Returns the short name of this argument
136 *
137 * @return string This argument's short name
138 */
139 public function getShortName() {
140 return $this->shortName;
141 }
142
143 /**
144 * Sets the default value of the argument
145 *
146 * @param mixed $defaultValue Default value
147 * @return void
148 */
149 public function setDefaultValue($defaultValue) {
150 $this->defaultValue = $defaultValue;
151 }
152
153 /**
154 * Sets the data type of this argument's value
155 *
156 * @param string $dataType: Name of the data type
157 * @return Tx_Extbase_MVC_Controller_Argument $this
158 */
159 public function setDataType($dataType) {
160 $this->dataType = ($dataType != '' ? $dataType : 'Text');
161 // TODO Make validator path and class names configurable
162 $validatorClassName = 'Tx_Extbase_Validation_Validator_' . $this->dataType;
163 $classFilePathAndName = t3lib_extMgm::extPath('extbase') . 'Classes/Validation/Validator/' . $this->dataType . '.php';
164 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
165 require_once($classFilePathAndName);
166 $this->validator = t3lib_div::makeInstance($validatorClassName);
167 }
168 return $this;
169 }
170
171 /**
172 * Returns the data type of this argument's value
173 *
174 * @return string The data type
175 */
176 public function getDataType() {
177 return $this->dataType;
178 }
179
180 /**
181 * Marks this argument to be required
182 *
183 * @param boolean $required TRUE if this argument should be required
184 * @return Tx_Extbase_MVC_Controller_Argument $this
185 */
186 public function setRequired($required) {
187 $this->isRequired = $required;
188 return $this;
189 }
190
191 /**
192 * Returns TRUE if this argument is required
193 *
194 * @return boolean TRUE if this argument is required
195 */
196 public function isRequired() {
197 return $this->isRequired;
198 }
199
200 /**
201 * Sets the value of this argument.
202 *
203 * @param mixed $value: The value of this argument
204 * @return Tx_Extbase_MVC_Controller_Argument $this
205 * @throws Tx_Extbase_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
206 */
207 public function setValue($value) {
208 if ($this->isValidValueForThisArgument($value)) {
209 $this->value = $value;
210 }
211 return $this;
212 }
213
214 /**
215 * Returns the value of this argument
216 *
217 * @return object The value of this argument - if none was set, NULL is returned
218 */
219 public function getValue() {
220 if ($this->value === NULL) {
221 return $this->defaultValue;
222 } else {
223 return $this->value;
224 }
225 }
226
227 /**
228 * Checks if this argument has a value set.
229 *
230 * @return boolean TRUE if a value was set, otherwise FALSE
231 */
232 public function isValue() {
233 return $this->value !== NULL;
234 }
235
236 /**
237 * undocumented function
238 *
239 * @param string $value
240 * @return boolean TRUE if the value is valid for this argument, otherwise FALSE
241 */
242 protected function isValidValueForThisArgument($value) {
243 $isValid = TRUE;
244 $this->clearErrors();
245 $validatorErrors = t3lib_div::makeInstance('Tx_Extbase_Validation_Errors');
246 if ($this->getValidator() !== NULL) {
247 $isValid &= (boolean)$this->getValidator()->isValid($value, $validatorErrors);
248 } else {
249 throw new Tx_Extbase_Validation_Exception_NoValidatorFound('No appropriate validator for the argument "' . $this->getName() . '" was found.', 1235748909);
250 }
251 if (!$isValid) {
252 foreach ($validatorErrors as $error) {
253 $this->addError($error);
254 }
255 }
256 $this->setIsValid($isValid);
257 return $isValid;
258 }
259
260 /**
261 * Sets the validity of the argument
262 *
263 * @return void
264 */
265 // TODO naming of the method setIsValid()
266 public function setIsValid($isValid) {
267 return $this->isValid = $isValid;
268 }
269
270 /**
271 * Returns TRUE when the argument is valid
272 *
273 * @return boolean TRUE if the argument is valid
274 */
275 public function isValid() {
276 return $this->isValid;
277 }
278
279 /**
280 * Add an initialization error (e.g. a mapping error)
281 *
282 * @param string An error text
283 * @return void
284 */
285 public function addError($error) {
286 $this->errors[] = $error;
287 }
288
289 /**
290 * Removes all errors
291 *
292 * @return void
293 */
294 public function clearErrors() {
295 $this->errors = array();
296 }
297
298 /**
299 * Get all initialization errors
300 *
301 * @return array An array containing Tx_Extbase_Error_Error objects
302 * @see addError(Tx_Extbase_Error_Error $error)
303 */
304 public function getErrors() {
305 return $this->errors;
306 }
307
308 /**
309 * Returns true if any error was recognized
310 *
311 * @return boolean True if an error occured
312 */
313 public function hasErrors() {
314 return (count($this->errors) > 0);
315 }
316
317 /**
318 * Set an additional validator
319 *
320 * @param string Class name of a validator
321 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
322 */
323 public function setValidator($className) {
324 $this->validator = t3lib_div::makeInstance($className);
325 return $this;
326 }
327
328 /**
329 * Returns the set validator
330 *
331 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
332 */
333 public function getValidator() {
334 return $this->validator;
335 }
336
337 /**
338 * Create and set a validator chain
339 *
340 * @param array Object names of the validators
341 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
342 */
343 public function setNewValidatorChain(array $validators) {
344 $this->validator = t3lib_div::makeInstance('Tx_Extbase_Validation_Validator_ChainValidator');
345 foreach ($validators as $validator) {
346 if (is_array($validator)) {
347 $objectName = 'Tx_Extbase_Validation_Validator_' . $validator[0];
348 $this->validator->addValidator(new $objectName);
349 } else {
350 $objectName = 'Tx_Extbase_Validation_Validator_' . $validator;
351 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
352 }
353 }
354 return $this;
355 }
356
357 /**
358 * Set the uid for the argument.
359 *
360 * @param string $uid The uid for the argument.
361 * @return void
362 */
363 public function setUid($uid) {
364 $this->uid = $uid;
365 }
366
367 /**
368 * Get the uid of the argument, if it has one.
369 *
370 * @return string Uid of the argument. If none set, returns NULL.
371 */
372 public function getUid() {
373 return $this->uid;
374 }
375
376 /**
377 * Returns a string representation of this argument's value
378 *
379 * @return string
380 */
381 public function __toString() {
382 return (string)$this->value;
383 }
384 }
385 ?>