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 * @var Tx_Extbase_Persistence_QueryFactory
38 protected $queryFactory;
41 * @var Tx_Extbase_Property_Mapper
43 protected $propertyMapper;
46 * Name of this argument
52 * Short name of this argument
55 protected $shortName = NULL;
58 * Data type of this argument's value
61 protected $dataType = 'Text';
64 * TRUE if this argument is required
67 protected $isRequired = FALSE;
70 * Actual value of this argument
73 protected $value = NULL;
76 * Default value. Used if argument is optional.
79 protected $defaultValue = NULL;
82 * A custom validator, used supplementary to the base validation
83 * @var Tx_Extbase_Validation_Validator_ValidatorInterface
85 protected $validator = NULL;
88 * Uid for the argument, if it has one
91 protected $uid = NULL;
94 * Constructs this controller argument
96 * @param string $name Name of this argument
97 * @param string $dataType The data type of this argument
98 * @throws InvalidArgumentException if $name is not a string or empty
100 public function __construct($name, $dataType = 'Text') {
101 $this->queryFactory
= t3lib_div
::makeInstance('Tx_Extbase_Persistence_QueryFactory');
102 $this->propertyMapper
= t3lib_div
::makeInstance('Tx_Extbase_Property_Mapper');
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 data type of this argument's value
146 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
147 * @return Tx_Extbase_MVC_Controller_Argument $this
149 public function setDataType($dataType) {
150 $this->dataType
= $dataType;
155 * Returns the data type of this argument's value
157 * @return string The data type
159 public function getDataType() {
160 return $this->dataType
;
164 * Marks this argument to be required
166 * @param boolean $required TRUE if this argument should be required
167 * @return Tx_Extbase_MVC_Controller_Argument $this
169 public function setRequired($required) {
170 $this->isRequired
= (boolean
)$required;
175 * Returns TRUE if this argument is required
177 * @return boolean TRUE if this argument is required
179 public function isRequired() {
180 return $this->isRequired
;
184 * Sets the default value of the argument
186 * @param mixed $defaultValue Default value
189 public function setDefaultValue($defaultValue) {
190 $this->defaultValue
= $defaultValue;
194 * Returns the default value of this argument
196 * @return mixed The default value
198 public function getDefaultValue() {
199 return $this->defaultValue
;
203 * Sets a custom validator which is used supplementary to the base validation
205 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
206 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
208 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface
$validator) {
209 $this->validator
= $validator;
214 * Create and set a validator chain
216 * @param array Object names of the validators
217 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
219 public function setNewValidatorChain(array $objectNames) {
220 $this->validator
= $this->objectFactory
->create('Tx_Extbase_Validation_Validator_ChainValidator');
221 foreach ($objectNames as $objectName) {
222 if (!$this->objectManager
->isObjectRegistered($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
223 $this->validator
->addValidator(t3lib_div
::makeInstance($objectName));
228 * Returns the set validator
230 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
232 public function getValidator() {
233 return $this->validator
;
237 * Sets the value of this argument.
239 * @param mixed $value: The value of this argument
240 * @return Tx_Extbase_MVC_Controller_Argument $this
241 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
242 * @author Robert Lemke <robert@typo3.org>
244 public function setValue($value) {
245 if (is_array($value)) {
246 if (isset($value['uid'])) {
247 $existingObject = $this->findObjectByUid($value['uid']);
248 if ($existingObject === FALSE) throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('Argument "' . $this->name
. '": Querying the repository for the specified object was not sucessful.', 1237305720);
249 unset($value['uid']);
250 if (count($value) === 0) {
251 $value = $existingObject;
252 } elseif ($existingObject !== NULL) {
253 $newObject = clone $existingObject;
254 if ($this->propertyMapper
->map(array_keys($value), $value, $newObject)) {
259 $newObject = t3lib_div
::makeInstance($this->dataType
);
260 if ($this->propertyMapper
->map(array_keys($value), $value, $newObject)) {
265 $this->value
= $value;
270 * Finds an object from the repository by searching for its technical UID.
272 * @param int $uid The object's uid
273 * @return mixed Either the object matching the uuid or, if none or more than one object was found, FALSE
275 protected function findObjectByUid($uid) {
276 $query = $this->queryFactory
->create($this->dataType
);
277 $query->matching('uid=' . intval($uid));
278 $objects = $query->execute();
279 if (count($objects) === 1 ) return current($objects);
284 * Returns the value of this argument
286 * @return object The value of this argument - if none was set, NULL is returned
288 public function getValue() {
289 if ($this->value
=== NULL) {
290 return $this->defaultValue
;
297 * Checks if this argument has a value set.
299 * @return boolean TRUE if a value was set, otherwise FALSE
301 public function isValue() {
302 return $this->value
!== NULL;
306 * Returns a string representation of this argument's value
310 public function __toString() {
311 return (string)$this->value
;