2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * A controller argument
33 class Tx_ExtBase_MVC_Controller_Argument
{
36 * Name of this argument
42 * Short name of this argument
45 protected $shortName = NULL;
48 * Data type of this argument's value
51 protected $dataType = 'Text';
54 * TRUE if this argument is required
57 protected $isRequired = FALSE;
60 * Actual value of this argument
63 protected $value = NULL;
66 * Default value. Used if argument is optional.
69 protected $defaultValue = NULL;
72 * The argument is valid
75 protected $isValid = FALSE;
78 * Any error (Tx_ExtBase_Error_Error) that occured while initializing this argument (e.g. a mapping error)
81 protected $errors = array();
84 * The property validator for this argument
85 * @var Tx_ExtBase_Validation_Validator_ValidatorInterface
87 protected $validator = NULL;
90 * Uid for the argument, if it has one
93 protected $uid = NULL;
96 * Constructs this controller argument
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
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);
105 if (is_array($dataType)) {
106 $this->setNewValidatorChain($dataType);
108 $this->setDataType($dataType);
113 * Returns the name of this argument
115 * @return string This argument's name
117 public function getName() {
122 * Sets the short name of this argument.
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
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;
135 * Returns the short name of this argument
137 * @return string This argument's short name
139 public function getShortName() {
140 return $this->shortName
;
144 * Sets the default value of the argument
146 * @param mixed $defaultValue Default value
149 public function setDefaultValue($defaultValue) {
150 $this->defaultValue
= $defaultValue;
154 * Sets the data type of this argument's value
156 * @param string $dataType: Name of the data type
157 * @return Tx_ExtBase_MVC_Controller_Argument $this
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);
172 * Returns the data type of this argument's value
174 * @return string The data type
176 public function getDataType() {
177 return $this->dataType
;
181 * Marks this argument to be required
183 * @param boolean $required TRUE if this argument should be required
184 * @return Tx_ExtBase_MVC_Controller_Argument $this
186 public function setRequired($required) {
187 $this->isRequired
= $required;
192 * Returns TRUE if this argument is required
194 * @return boolean TRUE if this argument is required
196 public function isRequired() {
197 return $this->isRequired
;
201 * Sets the value of this argument.
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
207 public function setValue($value) {
208 if ($this->isValidValueForThisArgument($value)) {
209 $this->value
= $value;
215 * Returns the value of this argument
217 * @return object The value of this argument - if none was set, NULL is returned
219 public function getValue() {
220 if ($this->value
=== NULL) {
221 return $this->defaultValue
;
228 * Checks if this argument has a value set.
230 * @return boolean TRUE if a value was set, otherwise FALSE
232 public function isValue() {
233 return $this->value
!== NULL;
237 * undocumented function
239 * @param string $value
240 * @return boolean TRUE if the value is valid for this argument, otherwise FALSE
242 protected function isValidValueForThisArgument($value) {
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);
249 throw new Tx_ExtBase_Validation_Exception_NoValidatorFound('No appropriate validator for the argument "' . $this->getName() . '" was found.', 1235748909);
252 foreach ($validatorErrors as $error) {
253 $this->addError($error);
256 $this->setIsValid($isValid);
261 * Sets the validity of the argument
265 // TODO naming of the method setIsValid()
266 public function setIsValid($isValid) {
267 return $this->isValid
= $isValid;
271 * Returns TRUE when the argument is valid
273 * @return boolean TRUE if the argument is valid
275 public function isValid() {
276 return $this->isValid
;
280 * Add an initialization error (e.g. a mapping error)
282 * @param string An error text
285 public function addError($error) {
286 $this->errors
[] = $error;
294 public function clearErrors() {
295 $this->errors
= array();
299 * Get all initialization errors
301 * @return array An array containing Tx_ExtBase_Error_Error objects
302 * @see addError(Tx_ExtBase_Error_Error $error)
304 public function getErrors() {
305 return $this->errors
;
309 * Returns true if any error was recognized
311 * @return boolean True if an error occured
313 public function hasErrors() {
314 return (count($this->errors
) > 0);
318 * Set an additional validator
320 * @param string Class name of a validator
321 * @return Tx_ExtBase_MVC_Controller_Argument Returns $this (used for fluent interface)
323 public function setValidator($className) {
324 $this->validator
= t3lib_div
::makeInstance($className);
329 * Returns the set validator
331 * @return Tx_ExtBase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
333 public function getValidator() {
334 return $this->validator
;
338 * Create and set a validator chain
340 * @param array Object names of the validators
341 * @return Tx_ExtBase_MVC_Controller_Argument Returns $this (used for fluent interface)
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);
350 $objectName = 'Tx_ExtBase_Validation_Validator_' . $validator;
351 $this->validator
->addValidator(t3lib_div
::makeInstance($objectName));
358 * Set the uid for the argument.
360 * @param string $uid The uid for the argument.
363 public function setUid($uid) {
368 * Get the uid of the argument, if it has one.
370 * @return string Uid of the argument. If none set, returns NULL.
372 public function getUid() {
377 * Returns a string representation of this argument's value
381 public function __toString() {
382 return (string)$this->value
;