. */ namespace Doctrine\ORM\Mapping; use ReflectionProperty; /** * Acts as a proxy to a nested Property structure, making it look like * just a single scalar property. * * This way value objects "just work" without UnitOfWork, Persisters or Hydrators * needing any changes. * * TODO: Move this class into Common\Reflection */ class ReflectionEmbeddedProperty { /** * @var ReflectionProperty */ private $parentProperty; /** * @var ReflectionProperty */ private $childProperty; /** * @var string */ private $class; /** * @param ReflectionProperty $parentProperty * @param ReflectionProperty $childProperty * @param string $class */ public function __construct(ReflectionProperty $parentProperty, ReflectionProperty $childProperty, $class) { $this->parentProperty = $parentProperty; $this->childProperty = $childProperty; $this->class = (string) $class; } /** * @param $object * * @return object|null */ public function getValue($object) { $embeddedObject = $this->parentProperty->getValue($object); if ($embeddedObject === null) { return null; } return $this->childProperty->getValue($embeddedObject); } /** * @param object $object * @param mixed $value */ public function setValue($object, $value) { $embeddedObject = $this->parentProperty->getValue($object); if ($embeddedObject === null) { $embeddedObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->class), $this->class)); $this->parentProperty->setValue($object, $embeddedObject); } $this->childProperty->setValue($embeddedObject, $value); } }