*/
class Tx_Extbase_MVC_Controller_Argument {
- /**
- * @var Tx_Extbase_Persistence_ManagerInterface
- */
- protected $persistenceManager;
-
/**
* @var Tx_Extbase_Persistence_QueryFactory
*/
*/
protected $propertyMapper;
+ /**
+ * @var Tx_Extbase_Reflection_Service
+ */
+ protected $reflectionService;
+
/**
* Name of this argument
* @var string
* Data type of this argument's value
* @var string
*/
- protected $dataType = 'Text';
+ protected $dataType = NULL;
+
+ /**
+ * If the data type is an object, the class schema of the data type class is resolved
+ * @var Tx_Extbase_Reflection_ClassSchema
+ */
+ protected $dataTypeClassSchema;
/**
* TRUE if this argument is required
*/
protected $validator = NULL;
- /**
- * If validation for this argument is temporarily disabled
- * @var boolean
- */
- protected $validationDisabled = FALSE;
-
/**
* Uid for the argument, if it has one
* @var string
*/
protected $uid = NULL;
+ const ORIGIN_CLIENT = 0;
+ const ORIGIN_PERSISTENCE = 1;
+ const ORIGIN_PERSISTENCE_AND_MODIFIED = 2;
+ const ORIGIN_NEWLY_CREATED = 3;
+
+ /**
+ * The origin of the argument value. This is only meaningful after argument mapping.
+ *
+ * One of the ORIGIN_* constants above
+ * @var integer
+ */
+ protected $origin = 0;
+
/**
* Constructs this controller argument
*
* @throws InvalidArgumentException if $name is not a string or empty
* @api
*/
- public function __construct($name, $dataType = 'Text') {
- $this->propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
- if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
+ public function __construct($name, $dataType) {
+ if (!is_string($name)) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
+ if (strlen($name) === 0) throw new InvalidArgumentException('$name must be a non-empty string, ' . strlen($name) . ' characters given.', 1232551853);
$this->name = $name;
- if (is_array($dataType)) {
- $this->setNewValidatorConjunction($dataType);
- } else {
- $this->setDataType($dataType);
- }
+ $this->dataType = $dataType;
+ }
+
+ /**
+ * Initializes this object
+ *
+ * @return void
+ */
+ public function initializeObject() {
+ $this->reflectionService = t3lib_div::makeInstance('Tx_Extbase_Reflection_Service');
+ $this->propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
+ $this->propertyMapper->injectReflectionService($this->reflectionService);
+ $this->dataTypeClassSchema = $this->reflectionService->getClassSchema($this->dataType);
}
/**
}
/**
- * Returns TRUE if validation is temporarily disabled for this argument and
- * FALSE if it's enabled.
- *
- * Note that this is flag is only informational and does not have any real impact
- * on other validation methods of this argument.
+ * Get the origin of the argument value. This is only meaningful after argument mapping.
*
- * @return boolean If validation is disabled
- * @api
+ * @return integer one of the ORIGIN_* constants
+ * @author Sebastian Kurfürst <sebastian@typo3.org>
*/
- public function isValidationDisabled() {
- return $this->validationDisabled;
+ public function getOrigin() {
+ return $this->origin;
}
/**
- * Enables validation for this argument.
+ * Sets the value of this argument.
*
- * @return void
- * @api
+ * @param mixed $value: The value of this argument
+ * @return Tx_Extbase_MVC_Controller_Argument $this
+ * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
*/
- public function enableValidation() {
- $this->validationDisabled = FALSE;
- }
+ public function setValue($value) {
+ $this->value = $this->transformValue($value);
- /**
- * Disables validation for this argument.
- *
- * @return void
- * @api
- */
- public function disableValidation() {
- $this->validationDisabled = TRUE;
+ return $this;
}
/**
- * Sets the value of this argument.
+ * Checks if the value is a UUID or an array but should be an object, i.e.
+ * the argument's data type class schema is set. If that is the case, this
+ * method tries to look up the corresponding object instead.
*
- * @param mixed $value: The value of this argument
- * @return Tx_Extbase_MVC_Controller_Argument $this
- * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
+ * Additionally, it maps arrays to objects in case it is a normal object.
+ *
+ * @param mixed $value The value of an argument
+ * @return mixed
*/
- public function setValue($value) {
- if (is_array($value)) {
- if (isset($value['uid'])) {
- $existingObject = $this->findObjectByUid($value['uid']);
- if ($existingObject === FALSE) throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('Argument "' . $this->name . '": Querying the repository for the specified object was not sucessful.', 1237305720);
- unset($value['uid']);
- if (count($value) === 0) {
- $value = $existingObject;
- } elseif ($existingObject !== NULL) {
- $newObject = clone $existingObject;
- if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
- $value = $newObject;
- }
- }
+ protected function transformValue($value) {
+ if ($value === NULL) {
+ return NULL;
+ }
+ if (!class_exists($this->dataType)) {
+ return $value;
+ }
+ $transformedValue = NULL;
+ if ($this->dataTypeClassSchema !== NULL) {
+ // The target object is an Entity or ValueObject.
+ if (is_numeric($value)) {
+ $this->origin = self::ORIGIN_PERSISTENCE;
+ $transformedValue = $this->findObjectByUid($value);
+ } elseif (is_array($value)) {
+ $this->origin = self::ORIGIN_PERSISTENCE_AND_MODIFIED;
+ $transformedValue = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
+ }
+ } else {
+ if (!is_array($value)) {
+ 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);
+ }
+ $this->origin = self::ORIGIN_NEWLY_CREATED;
+ $transformedValue = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
+ }
+
+ if (!($transformedValue instanceof $this->dataType)) {
+ if (is_object($transformedValue)) {
+ throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType . '", but was of type "' . get_class($transformedValue) . '".', 1251730701);
} else {
- $newObject = t3lib_div::makeInstance($this->dataType);
- if ($this->propertyMapper->map(array_keys($value), $value, $newObject)) {
- $value = $newObject;
- }
+ throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType . '", but was of type "' . gettype($transformedValue) . '".', 1251730702);
}
}
- $this->value = $value;
- return $this;
+ return $transformedValue;
}
/**
$object = NULL;
if (count($result) > 0) {
$object = current($result);
- // TODO Check if the object is an Aggregate Root (this can be quite difficult because we have no Repository registration
- $this->persistenceManager->getSession()->registerReconstitutedObject($object);
}
return $object;
}