className = $className; $this->properties = $properties; } /** * @param array $data * @param object $instance * @throws \Assert\AssertionFailedException */ public function hydrate($instance, array $data) { Assertion::isInstanceOf($instance, $this->className); foreach ($data as $key => $value) { $this->getReflectionProperty($key)->setValue($instance, $value); } } /** * @param object $instance * @return array * @throws \Assert\AssertionFailedException */ public function extract($instance) { Assertion::isInstanceOf($instance, $this->className); $data = []; foreach ($this->getReflectionProperties() as $name => $property) { $data[$name] = $property->getValue($instance); } return $data; } /** * @return \ReflectionClass */ private function getReflectionClass() { if (null === $this->reflectionClass) { $this->reflectionClass = new \ReflectionClass($this->className); } return $this->reflectionClass; } /** * @param string $name * @return \ReflectionProperty * @throws \RuntimeException */ private function getReflectionProperty($name) { $this->loadReflectionProperties(); return $this->reflectionProperties[$name]; } /** * @return \ReflectionProperty[] */ private function getReflectionProperties() { $this->loadReflectionProperties(); return $this->reflectionProperties; } private function loadReflectionProperties() { if (null !== $this->reflectionProperties) { return; } $class = $this->getReflectionClass(); $properties = []; foreach ($this->properties as $name) { $property = $class->getProperty($name); $property->setAccessible(true); $properties[$name] = $property; } $this->reflectionProperties = $properties; } }