2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * A controller argument
32 * @subpackage MVC\Controller
36 class Tx_Extbase_MVC_Controller_Argument
{
39 * @var Tx_Extbase_Persistence_QueryFactory
41 protected $queryFactory;
44 * @var Tx_Extbase_Property_Mapper
46 protected $propertyMapper;
49 * @var Tx_Extbase_Reflection_Service
51 protected $reflectionService;
54 * Name of this argument
60 * Short name of this argument
63 protected $shortName = NULL;
66 * Data type of this argument's value
69 protected $dataType = NULL;
72 * If the data type is an object, the class schema of the data type class is resolved
73 * @var Tx_Extbase_Reflection_ClassSchema
75 protected $dataTypeClassSchema;
78 * TRUE if this argument is required
81 protected $isRequired = FALSE;
84 * Actual value of this argument
87 protected $value = NULL;
90 * Default value. Used if argument is optional.
93 protected $defaultValue = NULL;
96 * A custom validator, used supplementary to the base validation
97 * @var Tx_Extbase_Validation_Validator_ValidatorInterface
99 protected $validator = NULL;
102 * Uid for the argument, if it has one
105 protected $uid = NULL;
107 const ORIGIN_CLIENT
= 0;
108 const ORIGIN_PERSISTENCE
= 1;
109 const ORIGIN_PERSISTENCE_AND_MODIFIED
= 2;
110 const ORIGIN_NEWLY_CREATED
= 3;
113 * The origin of the argument value. This is only meaningful after argument mapping.
115 * One of the ORIGIN_* constants above
118 protected $origin = 0;
121 * Constructs this controller argument
123 * @param string $name Name of this argument
124 * @param string $dataType The data type of this argument
125 * @throws InvalidArgumentException if $name is not a string or empty
128 public function __construct($name, $dataType) {
129 if (!is_string($name)) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
130 if (strlen($name) === 0) throw new InvalidArgumentException('$name must be a non-empty string, ' . strlen($name) . ' characters given.', 1232551853);
132 $this->dataType
= $dataType;
136 * Initializes this object
140 public function initializeObject() {
141 $this->reflectionService
= t3lib_div
::makeInstance('Tx_Extbase_Reflection_Service');
142 $this->propertyMapper
= t3lib_div
::makeInstance('Tx_Extbase_Property_Mapper');
143 $this->propertyMapper
->injectReflectionService($this->reflectionService
);
144 $this->dataTypeClassSchema
= $this->reflectionService
->getClassSchema($this->dataType
);
148 * Injects the Persistence Manager
150 * @param Tx_Extbase_Persistence_ManagerInterface
153 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface
$persistenceManager) {
154 $this->persistenceManager
= $persistenceManager;
158 * Injects a QueryFactory instance
160 * @param Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory
163 public function injectQueryFactory(Tx_Extbase_Persistence_QueryFactoryInterface
$queryFactory) {
164 $this->queryFactory
= $queryFactory;
168 * Returns the name of this argument
170 * @return string This argument's name
173 public function getName() {
178 * Sets the short name of this argument.
180 * @param string $shortName A "short name" - a single character
181 * @return Tx_Extbase_MVC_Controller_Argument $this
182 * @throws InvalidArgumentException if $shortName is not a character
185 public function setShortName($shortName) {
186 if ($shortName !== NULL && (!is_string($shortName) ||
strlen($shortName) !== 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
187 $this->shortName
= $shortName;
192 * Returns the short name of this argument
194 * @return string This argument's short name
197 public function getShortName() {
198 return $this->shortName
;
202 * Sets the data type of this argument's value
204 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
205 * @return Tx_Extbase_MVC_Controller_Argument $this
208 public function setDataType($dataType) {
209 $this->dataType
= $dataType;
214 * Returns the data type of this argument's value
216 * @return string The data type
219 public function getDataType() {
220 return $this->dataType
;
224 * Marks this argument to be required
226 * @param boolean $required TRUE if this argument should be required
227 * @return Tx_Extbase_MVC_Controller_Argument $this
230 public function setRequired($required) {
231 $this->isRequired
= (boolean
)$required;
236 * Returns TRUE if this argument is required
238 * @return boolean TRUE if this argument is required
241 public function isRequired() {
242 return $this->isRequired
;
246 * Sets the default value of the argument
248 * @param mixed $defaultValue Default value
252 public function setDefaultValue($defaultValue) {
253 $this->defaultValue
= $defaultValue;
257 * Returns the default value of this argument
259 * @return mixed The default value
262 public function getDefaultValue() {
263 return $this->defaultValue
;
267 * Sets a custom validator which is used supplementary to the base validation
269 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
270 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
273 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface
$validator) {
274 $this->validator
= $validator;
279 * Create and set a validator chain
281 * @param array Object names of the validators
282 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
285 public function setNewValidatorConjunction(array $objectNames) {
286 if ($this->validator
=== NULL) {
287 $this->validator
= t3lib_div
::makeInstance('Tx_Extbase_Validation_Validator_ConjunctionValidator');
289 foreach ($objectNames as $objectName) {
290 if (!class_exists($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
291 $this->validator
->addValidator(t3lib_div
::makeInstance($objectName));
297 * Returns the set validator
299 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
302 public function getValidator() {
303 return $this->validator
;
307 * Get the origin of the argument value. This is only meaningful after argument mapping.
309 * @return integer one of the ORIGIN_* constants
310 * @author Sebastian Kurfürst <sebastian@typo3.org>
312 public function getOrigin() {
313 return $this->origin
;
317 * Sets the value of this argument.
319 * @param mixed $value: The value of this argument
320 * @return Tx_Extbase_MVC_Controller_Argument $this
321 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
323 public function setValue($value) {
324 $this->value
= $this->transformValue($value);
330 * Checks if the value is a UUID or an array but should be an object, i.e.
331 * the argument's data type class schema is set. If that is the case, this
332 * method tries to look up the corresponding object instead.
334 * Additionally, it maps arrays to objects in case it is a normal object.
336 * @param mixed $value The value of an argument
339 protected function transformValue($value) {
340 if ($value === NULL) {
343 if (!class_exists($this->dataType
)) {
346 $transformedValue = NULL;
347 if ($this->dataTypeClassSchema
!== NULL) {
348 // The target object is an Entity or ValueObject.
349 if (is_numeric($value)) {
350 $this->origin
= self
::ORIGIN_PERSISTENCE
;
351 $transformedValue = $this->findObjectByUid($value);
352 } elseif (is_array($value)) {
353 $this->origin
= self
::ORIGIN_PERSISTENCE_AND_MODIFIED
;
354 $transformedValue = $this->propertyMapper
->map(array_keys($value), $value, $this->dataType
);
357 if (!is_array($value)) {
358 throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value was a simple type, so we could not map it to an object. Maybe the @entity or @valueobject annotations are missing?', 1251730701);
360 $this->origin
= self
::ORIGIN_NEWLY_CREATED
;
361 $transformedValue = $this->propertyMapper
->map(array_keys($value), $value, $this->dataType
);
364 if (!($transformedValue instanceof $this->dataType
)) {
365 if (is_object($transformedValue)) {
366 throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType
. '", but was of type "' . get_class($transformedValue) . '".', 1251730701);
368 throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType
. '", but was of type "' . gettype($transformedValue) . '".', 1251730702);
371 return $transformedValue;
375 * Finds an object from the repository by searching for its technical UID.
377 * @param int $uid The object's uid
378 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
380 protected function findObjectByUid($uid) {
381 $query = $this->queryFactory
->create($this->dataType
);
382 $result = $query->matching($query->withUid($uid))->execute();
384 if (count($result) > 0) {
385 $object = current($result);
391 * Returns the value of this argument
393 * @return object The value of this argument - if none was set, NULL is returned
396 public function getValue() {
397 if ($this->value
=== NULL) {
398 return $this->defaultValue
;
405 * Checks if this argument has a value set.
407 * @return boolean TRUE if a value was set, otherwise FALSE
409 public function isValue() {
410 return $this->value
!== NULL;
414 * Returns a string representation of this argument's value
419 public function __toString() {
420 return (string)$this->value
;