[+FEATURE] Extbase (MVC): Now, non-domain-objects can be mapped inside an argument...
authorSebastian Kurfürst <sebastian@typo3.org>
Tue, 15 Sep 2009 11:40:09 +0000 (11:40 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Tue, 15 Sep 2009 11:40:09 +0000 (11:40 +0000)
typo3/sysext/extbase/Classes/MVC/Controller/Argument.php

index d8fc52a..0b63039 100644 (file)
@@ -329,20 +329,47 @@ class Tx_Extbase_MVC_Controller_Argument {
         * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
         */
        public function setValue($value) {
-               if ($value !== NULL && $this->dataTypeClassSchema !== NULL) {
+               $this->value = $this->transformValue($value);
+
+               return $this;
+       }
+
+       /**
+        * 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.
+        *
+        * Additionally, it maps arrays to objects in case it is a normal object.
+        *
+        * @param mixed $value The value of an argument
+        * @return mixed
+        */
+       protected function transformValue($value) {
+               if ($value === NULL) {
+                       return NULL;
+               }
+               if (!class_exists($this->dataType)) {
+                       return $value;
+               }
+               $transformedValue = NULL;
+               if ($this->dataTypeClassSchema !== NULL) {
+                       // It is an Entity or ValueObject.
                        if (is_numeric($value)) {
-                               $value = $this->findObjectByUid($value);
+                               $transformedValue = $this->findObjectByUid($value);
                        } elseif (is_array($value)) {
-                               $value = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
+                               $transformedValue = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
                        }
-
-                       if (!($value instanceof $this->dataType)) {
-                               throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType . '".', 1251730701);
+               } 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);
                        }
+                       $transformedValue = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
                }
-               $this->value = $value;
 
-               return $this;
+               if (!($transformedValue instanceof $this->dataType)) {
+                       throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType . '", but was of type "' . get_class($transformedValue) . '".', 1251730701);
+               }
+               return $transformedValue;
        }
 
        /**