コード例 #1
0
ファイル: ObjectAccess.php プロジェクト: nxpthx/FLOW3
 /**
  * Set a property for a given object.
  * Tries to set the property the following ways:
  * - if target is an array, set value
  * - if super cow powers should be used, set value through reflection
  * - if public setter method exists, call it.
  * - if public property exists, set it directly.
  * - if the target object is an instance of ArrayAccess, it sets the property
  *   on it without checking if it existed.
  * - else, return FALSE
  *
  * @param mixed $subject The target object or array
  * @param string|integer $propertyName Name or index of the property to set
  * @param mixed $propertyValue Value of the property
  * @param boolean $forceDirectAccess directly access property using reflection(!)
  * @return boolean TRUE if the property could be set, FALSE otherwise
  * @throws \InvalidArgumentException in case $object was not an object or $propertyName was not a string
  */
 public static function setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess = FALSE)
 {
     if (is_array($subject)) {
         $subject[$propertyName] = $propertyValue;
         return TRUE;
     }
     if (!is_object($subject)) {
         throw new \InvalidArgumentException('subject must be an object or array, ' . gettype($subject) . ' given.', 1237301368);
     }
     if (!is_string($propertyName) && !is_integer($propertyName)) {
         throw new \InvalidArgumentException('Given property name/index is not of type string or integer.', 1231178878);
     }
     if ($forceDirectAccess === TRUE) {
         if (property_exists(get_class($subject), $propertyName)) {
             $propertyReflection = new \TYPO3\FLOW3\Reflection\PropertyReflection(get_class($subject), $propertyName);
             $propertyReflection->setValue($subject, $propertyValue);
         } else {
             $subject->{$propertyName} = $propertyValue;
         }
     } elseif (is_callable(array($subject, $setterMethodName = self::buildSetterMethodName($propertyName)))) {
         $subject->{$setterMethodName}($propertyValue);
     } elseif ($subject instanceof \ArrayAccess) {
         $subject[$propertyName] = $propertyValue;
     } elseif (array_key_exists($propertyName, get_object_vars($subject))) {
         $subject->{$propertyName} = $propertyValue;
     } else {
         return FALSE;
     }
     return TRUE;
 }
コード例 #2
0
ファイル: ObjectSerializer.php プロジェクト: nxpthx/FLOW3
 /**
  * Serializes an object as property array.
  *
  * @param object $object The object to store in the registry
  * @param boolean $isTopLevelItem Internal flag for managing the recursion
  * @return array The property array
  */
 public function serializeObjectAsPropertyArray($object, $isTopLevelItem = TRUE)
 {
     if ($isTopLevelItem) {
         $this->objectReferences = new \SplObjectStorage();
     }
     $this->objectReferences->attach($object);
     $className = get_class($object);
     $propertyArray = array();
     foreach ($this->reflectionService->getClassPropertyNames($className) as $propertyName) {
         if ($this->reflectionService->isPropertyTaggedWith($className, $propertyName, 'transient')) {
             continue;
         }
         $propertyReflection = new \TYPO3\FLOW3\Reflection\PropertyReflection($className, $propertyName);
         $propertyValue = $propertyReflection->getValue($object);
         if (is_object($propertyValue) && isset($this->objectReferences[$propertyValue])) {
             $propertyArray[$propertyName][self::TYPE] = 'object';
             $propertyArray[$propertyName][self::VALUE] = \spl_object_hash($propertyValue);
             continue;
         }
         $propertyClassName = is_object($propertyValue) ? get_class($propertyValue) : '';
         if ($propertyClassName === 'SplObjectStorage') {
             $propertyArray[$propertyName][self::TYPE] = 'SplObjectStorage';
             $propertyArray[$propertyName][self::VALUE] = array();
             foreach ($propertyValue as $storedObject) {
                 $propertyArray[$propertyName][self::VALUE][] = spl_object_hash($storedObject);
                 $this->serializeObjectAsPropertyArray($storedObject, FALSE);
             }
         } elseif (is_object($propertyValue) && $propertyValue instanceof \Doctrine\Common\Collections\Collection) {
             $propertyArray[$propertyName][self::TYPE] = 'Collection';
             $propertyArray[$propertyName][self::CLASSNAME] = get_class($propertyValue);
             $propertyArray[$propertyName][self::VALUE] = $this->buildStorageArrayForArrayProperty($propertyValue->toArray());
         } elseif (is_object($propertyValue) && $propertyValue instanceof \ArrayObject) {
             $propertyArray[$propertyName][self::TYPE] = 'ArrayObject';
             $propertyArray[$propertyName][self::VALUE] = $this->buildStorageArrayForArrayProperty($propertyValue->getArrayCopy());
         } elseif (is_object($propertyValue) && $this->persistenceManager->isNewObject($propertyValue) === FALSE && ($this->reflectionService->isClassAnnotatedWith($propertyClassName, 'TYPO3\\FLOW3\\Annotations\\Entity') || $this->reflectionService->isClassAnnotatedWith($propertyClassName, 'TYPO3\\FLOW3\\Annotations\\ValueObject') || $this->reflectionService->isClassAnnotatedWith($propertyClassName, 'Doctrine\\ORM\\Mapping\\Entity'))) {
             $propertyArray[$propertyName][self::TYPE] = 'persistenceObject';
             $propertyArray[$propertyName][self::VALUE] = get_class($propertyValue) . ':' . $this->persistenceManager->getIdentifierByObject($propertyValue);
         } elseif (is_object($propertyValue)) {
             $propertyObjectName = $this->objectManager->getObjectNameByClassName($propertyClassName);
             if ($propertyObjectName != '' && $this->objectManager->getScope($propertyObjectName) === \TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_SINGLETON) {
                 continue;
             }
             $propertyArray[$propertyName][self::TYPE] = 'object';
             $propertyArray[$propertyName][self::VALUE] = spl_object_hash($propertyValue);
             $this->serializeObjectAsPropertyArray($propertyValue, FALSE);
         } elseif (is_array($propertyValue)) {
             $propertyArray[$propertyName][self::TYPE] = 'array';
             $propertyArray[$propertyName][self::VALUE] = $this->buildStorageArrayForArrayProperty($propertyValue);
         } else {
             $propertyArray[$propertyName][self::TYPE] = 'simple';
             $propertyArray[$propertyName][self::VALUE] = $propertyValue;
         }
     }
     $this->objectsAsArray[spl_object_hash($object)] = array(self::CLASSNAME => $className, self::PROPERTIES => $propertyArray);
     if ($isTopLevelItem) {
         return $this->objectsAsArray;
     }
 }
コード例 #3
0
 /**
  * @test
  */
 public function getValueReturnsValueOfAPrivatePropertyEvenIfItIsAnObject()
 {
     $reflectionProperty = new \TYPO3\FLOW3\Reflection\PropertyReflection(__CLASS__, 'privateProperty');
     $this->protectedProperty = new \ArrayObject(array('1', '2', '3'));
     $this->assertEquals($this->privateProperty, $reflectionProperty->getValue($this), 'ReflectionProperty->getValue($this) did not return the object of our private property.');
     $this->privateProperty = $this;
     $this->assertSame($this, $reflectionProperty->getValue($this), 'ReflectionProperty->getValue($this) did not return the reference to $this.');
 }