コード例 #1
0
 /**
  * Adds (defines) a specific property and its type.
  *
  * @param string $name Name of the property
  * @param string $type Type of the property
  * @param boolean $lazy Whether the property should be lazy-loaded when reconstituting
  * @return void
  * @author Robert Lemke <*****@*****.**>
  * @author Karsten Dambekalns <*****@*****.**>
  */
 public function addProperty($name, $type, $lazy = FALSE)
 {
     $type = \F3\FLOW3\Utility\TypeHandling::parseType($type);
     $this->properties[$name] = array('type' => $type['type'], 'elementType' => $type['elementType'], 'lazy' => $lazy);
 }
コード例 #2
0
 /**
  * Maps the given properties to the target object WITHOUT VALIDATING THE RESULT.
  * If the properties could be set, this method returns TRUE, otherwise FALSE.
  * Returning TRUE does not mean that the target object is valid and secure!
  *
  * Only use this method if you're sure that you don't need validation!
  *
  * @param array $propertyNames Names of the properties to map.
  * @param mixed $source Source containing the properties to map to the target object. Must either be an array, ArrayObject or any other object.
  * @param object $target The target object
  * @param array $optionalPropertyNames Names of optional properties. If a property is specified here and it doesn't exist in the source, no error is issued.
  * @return boolean TRUE if the properties could be mapped, otherwise FALSE
  * @see mapAndValidate()
  * @author Robert Lemke <*****@*****.**>
  * @api
  */
 public function map(array $propertyNames, $source, &$target, $optionalPropertyNames = array())
 {
     if (!is_object($source) && !is_array($source)) {
         throw new \F3\FLOW3\Property\Exception\InvalidSourceException('The source object must be a valid object or array, ' . gettype($target) . ' given.', 1187807099);
     }
     if (is_string($target) && strpos($target, '\\') !== FALSE) {
         return $this->transformToObject($source, $target, '--none--');
     }
     $this->mappingResults = $this->objectManager->getObject('F3\\FLOW3\\Property\\MappingResults');
     if (!is_object($target) && !is_array($target)) {
         throw new \F3\FLOW3\Property\Exception\InvalidTargetException('The target must be a valid object, class name or array, ' . gettype($target) . ' given.', 1187807099);
     }
     if (is_object($target)) {
         $targetClassSchema = $this->reflectionService->getClassSchema($target);
     } else {
         $targetClassSchema = NULL;
     }
     foreach ($propertyNames as $propertyName) {
         $propertyValue = NULL;
         if (is_array($source) || $source instanceof \ArrayAccess) {
             if (isset($source[$propertyName])) {
                 $propertyValue = $source[$propertyName];
             }
         } elseif (\F3\FLOW3\Reflection\ObjectAccess::isPropertyGettable($source, $propertyName)) {
             $propertyValue = \F3\FLOW3\Reflection\ObjectAccess::getProperty($source, $propertyName);
         }
         if ($propertyValue === NULL && !in_array($propertyName, $optionalPropertyNames)) {
             $this->mappingResults->addError($this->objectManager->getObject('F3\\FLOW3\\Error\\Error', "Required property '{$propertyName}' does not exist in source.", 1236785359), $propertyName);
         } else {
             if (method_exists($target, \F3\FLOW3\Reflection\ObjectAccess::buildSetterMethodName($propertyName)) && is_callable(array($target, \F3\FLOW3\Reflection\ObjectAccess::buildSetterMethodName($propertyName)))) {
                 $targetClassName = $target instanceof \F3\FLOW3\AOP\ProxyInterface ? $target->FLOW3_AOP_Proxy_getProxyTargetClassName() : get_class($target);
                 $methodParameters = $this->reflectionService->getMethodParameters($targetClassName, \F3\FLOW3\Reflection\ObjectAccess::buildSetterMethodName($propertyName));
                 $methodParameter = current($methodParameters);
                 $targetPropertyType = \F3\FLOW3\Utility\TypeHandling::parseType($methodParameter['type']);
             } elseif ($targetClassSchema !== NULL && $targetClassSchema->hasProperty($propertyName)) {
                 $targetPropertyType = $targetClassSchema->getProperty($propertyName);
             } elseif ($targetClassSchema !== NULL) {
                 $this->mappingResults->addError($this->objectManager->getObject('F3\\FLOW3\\Error\\Error', "Property '{$propertyName}' does not exist in target class schema.", 1251813614), $propertyName);
                 continue;
             }
             if (isset($targetPropertyType)) {
                 if (in_array($targetPropertyType['type'], array('array', 'ArrayObject', 'SplObjectStorage')) && strpos($targetPropertyType['elementType'], '\\') !== FALSE) {
                     $objects = array();
                     foreach ($propertyValue as $value) {
                         $objects[] = $this->transformToObject($value, $targetPropertyType['elementType'], $propertyName);
                     }
                     if ($targetPropertyType['type'] === 'ArrayObject') {
                         $propertyValue = new \ArrayObject($objects);
                     } elseif ($targetPropertyType['type'] === 'SplObjectStorage') {
                         $propertyValue = new \SplObjectStorage();
                         foreach ($objects as $object) {
                             $propertyValue->attach($object);
                         }
                     } else {
                         $propertyValue = $objects;
                     }
                 } elseif (strpos($targetPropertyType['type'], '\\') !== FALSE) {
                     $propertyValue = $this->transformToObject($propertyValue, $targetPropertyType['type'], $propertyName);
                 }
             }
             if (is_array($target)) {
                 $target[$propertyName] = $propertyValue;
             } elseif (\F3\FLOW3\Reflection\ObjectAccess::setProperty($target, $propertyName, $propertyValue) === FALSE) {
                 $this->mappingResults->addError($this->objectManager->getObject('F3\\FLOW3\\Error\\Error', "Property '{$propertyName}' could not be set.", 1236783102), $propertyName);
             }
         }
     }
     return !$this->mappingResults->hasErrors();
 }