Пример #1
0
 /**
  * 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
  * @author Robert Lemke <*****@*****.**>
  * @author Karsten Dambekalns <*****@*****.**>
  * @author Bastian Waidelich <*****@*****.**>
  * @author Sebastian Kurfürst <*****@*****.**>
  */
 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_string($value) && preg_match(self::PATTERN_MATCH_UUID, $value) === 1) {
             $this->origin = self::ORIGIN_PERSISTENCE;
             $transformedValue = $this->persistenceManager->getObjectByIdentifier($value);
         } elseif (is_array($value)) {
             if (array_keys($value) === array('__identity')) {
                 // If there is only an __identity array _and nothing else_, then the property mapper will not clone the object.
                 $this->origin = self::ORIGIN_PERSISTENCE;
             } else {
                 $this->origin = self::ORIGIN_PERSISTENCE_AND_MODIFIED;
             }
             $transformedValue = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
         }
     } else {
         if (!is_array($value)) {
             throw new \F3\FLOW3\MVC\Exception\InvalidArgumentValueException('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) {
         throw new \F3\FLOW3\MVC\Exception\InvalidArgumentValueException('The value must be of type "' . $this->dataType . '", but was of type "' . (is_object($transformedValue) ? get_class($transformedValue) : gettype($transformedValue)) . '".', 1251730701);
     }
     return $transformedValue;
 }
Пример #2
0
 /**
  * Finds an object matching the given UUID.
  *
  * @param string $uuid The UUID of the object to find
  * @return object The matching object if found, otherwise NULL
  * @author Karsten Dambekalns <*****@*****.**>
  * @api
  */
 public function findByUUID($uuid)
 {
     $object = $this->persistenceManager->getObjectByIdentifier($uuid);
     if (is_a($object, $this->objectType)) {
         return $object;
     } else {
         return NULL;
     }
 }
 /**
  * Transforms strings with UUIDs or arrays with UUIDs/identity properties
  * into the requested type, if possible.
  *
  * @param mixed $propertyValue The value to transform, string or array
  * @param string $targetType The type to transform to
  * @param string $propertyName In case of an error we add this to the error message
  * @return object The object, when no transformation was possible this may return NULL as well
  */
 protected function transformToObject($propertyValue, $targetType, $propertyName)
 {
     if (is_string($propertyValue) && preg_match(self::PATTERN_MATCH_UUID, $propertyValue) === 1) {
         $object = $this->persistenceManager->getObjectByIdentifier($propertyValue);
         if ($object === FALSE) {
             $this->mappingResults->addError($this->objectManager->getObject('F3\\FLOW3\\Error\\Error', 'Querying the repository for the specified object with UUID ' . $propertyValue . ' was not successful.', 1249379517), $propertyName);
         }
     } elseif (is_array($propertyValue)) {
         if (isset($propertyValue['__identity'])) {
             $existingObject = is_array($propertyValue['__identity']) ? $this->findObjectByIdentityProperties($propertyValue['__identity'], $targetType) : $this->persistenceManager->getObjectByIdentifier($propertyValue['__identity']);
             if ($existingObject === FALSE) {
                 throw new \F3\FLOW3\Property\Exception\TargetNotFoundException('Querying the repository for the specified object was not successful.', 1237305720);
             }
             unset($propertyValue['__identity']);
             if (count($propertyValue) === 0) {
                 $object = $existingObject;
             } elseif ($existingObject !== NULL) {
                 $newObject = clone $existingObject;
                 if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
                     $object = $newObject;
                 }
             }
         } else {
             if (isset($this->objectConverters[$targetType])) {
                 $conversionResult = $this->objectConverters[$targetType]->convertFrom($propertyValue);
                 if ($conversionResult instanceof \F3\FLOW3\Error\Error) {
                     $this->mappingResults->addError($conversionResult, $propertyName);
                     return NULL;
                 } elseif (is_object($conversionResult) || $conversionResult === NULL) {
                     return $conversionResult;
                 }
             }
             $newObject = $this->objectManager->getObject($targetType);
             if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
                 return $newObject;
             }
             throw new \F3\FLOW3\Property\Exception\InvalidTargetException('Values could not be mapped to new object of type ' . $targetType . ' for property "' . $propertyName . '". (Map errors: ' . implode(' - ', $this->mappingResults->getErrors()) . ')', 1259770027);
         }
     } else {
         throw new \InvalidArgumentException('transformToObject() accepts only strings and arrays.', 1251814355);
     }
     return $object;
 }