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