/** * @test * @author Andreas Förthner <*****@*****.**> */ public function getValueReturnsValueOfAPrivatePropertyEvenIfItIsAnObject() { $reflectionProperty = new \F3\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.'); }
/** * Serializes an object as property array. * * @param string $objectName Name of the object the object is made for * @param object $object The object to store in the registry * @return array The property array * @author Andreas Förthner <*****@*****.**> */ public function serializeObjectAsPropertyArray($objectName, $object) { if (isset($this->objectsAsArray[$objectName])) { return $this->objectsAsArray; } $className = get_class($object); $propertyArray = array(); foreach ($this->reflectionService->getClassPropertyNames($className) as $propertyName) { $propertyValue = ''; if ($this->reflectionService->isPropertyTaggedWith($className, $propertyName, 'transient')) { continue; } if ($object instanceof \F3\FLOW3\AOP\ProxyInterface) { $propertyValue = $object->FLOW3_AOP_Proxy_getProperty($propertyName); } else { $propertyReflection = new \F3\FLOW3\Reflection\PropertyReflection($className, $propertyName); $propertyValue = $propertyReflection->getValue($object); } $propertyClassName = ''; if (is_object($propertyValue)) { $propertyClassName = get_class($propertyValue); } if (is_object($propertyValue) && $propertyClassName === 'SplObjectStorage') { $propertyArray[$propertyName]['type'] = 'SplObjectStorage'; $propertyArray[$propertyName]['value'] = array(); foreach ($propertyValue as $storedObject) { $objectHash = spl_object_hash($storedObject); $propertyArray[$propertyName]['value'][] = $objectHash; $this->serializeObjectAsPropertyArray($objectHash, $storedObject); } } else { if (is_object($propertyValue) && $propertyValue instanceof \ArrayObject) { $propertyArray[$propertyName]['type'] = 'ArrayObject'; $propertyArray[$propertyName]['value'] = $this->buildStorageArrayForArrayProperty($propertyValue->getArrayCopy()); } else { if (is_object($propertyValue) && $propertyValue instanceof \F3\FLOW3\AOP\ProxyInterface && $propertyValue instanceof \F3\FLOW3\Persistence\Aspect\DirtyMonitoringInterface && $this->persistenceManager->isNewObject($propertyValue) === FALSE && ($this->reflectionService->isClassTaggedWith($propertyClassName, 'entity') || $this->reflectionService->isClassTaggedWith($propertyClassName, 'valueobject'))) { $propertyArray[$propertyName]['type'] = 'persistenceObject'; $propertyArray[$propertyName]['value']['className'] = $propertyValue->FLOW3_AOP_Proxy_getProxyTargetClassName(); $propertyArray[$propertyName]['value']['UUID'] = $this->persistenceManager->getIdentifierByObject($propertyValue); } else { if (is_object($propertyValue)) { $propertyObjectName = $this->objectManager->getObjectNameByClassName($propertyClassName); $objectConfiguration = $this->objectManager->getObjectConfiguration($propertyObjectName); if ($objectConfiguration->getScope() === 'singleton') { continue; } $objectHash = spl_object_hash($propertyValue); $propertyArray[$propertyName]['type'] = 'object'; $propertyArray[$propertyName]['value'] = $objectHash; $this->serializeObjectAsPropertyArray($objectHash, $propertyValue); } else { if (is_array($propertyValue)) { $propertyArray[$propertyName]['type'] = 'array'; $propertyArray[$propertyName]['value'] = $this->buildStorageArrayForArrayProperty($propertyValue); } else { $propertyArray[$propertyName]['type'] = 'simple'; $propertyArray[$propertyName]['value'] = $propertyValue; } } } } } } $this->objectsAsArray[$objectName] = array('className' => $className, 'properties' => $propertyArray); return $this->objectsAsArray; }
/** * Checks if the specified property of the given object is valid. * * If at least one error occurred, the result is FALSE. * * @param object $object The object containing the property to validate * @param string $propertyName Name of the property to validate * @return boolean TRUE if the property value is valid, FALSE if an error occured * @author Robert Lemke <*****@*****.**> * @api */ public function isPropertyValid($object, $propertyName) { if (!is_object($object)) { throw new \InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1241099149); } if (!isset($this->propertyValidators[$propertyName])) { return TRUE; } $result = TRUE; foreach ($this->propertyValidators[$propertyName] as $validator) { if (\F3\FLOW3\Reflection\ObjectAccess::isPropertyGettable($object, $propertyName)) { $propertyValue = \F3\FLOW3\Reflection\ObjectAccess::getProperty($object, $propertyName); } else { $propertyReflection = new \F3\FLOW3\Reflection\PropertyReflection(get_class($object), $propertyName); $propertyReflection->setAccessible(TRUE); $propertyValue = $propertyReflection->getValue($object); } if ($validator->isValid($propertyValue) === FALSE) { $this->addErrorsForProperty($validator->getErrors(), $propertyName); $result = FALSE; } } return $result; }