/** * @param string|null $class */ public function __construct($class = null) { if ($class) { $this->class = $class; $propertyClass = Factory::$propertyClass; foreach (Reflection::getClass($class)->getProperties() as $property) { $this->add(new $propertyClass($class, $property->getName(), $property)); } } }
/** * @param string $class * @param string $property * @param \ReflectionProperty $reflection * * @throws \RuntimeException */ public function __construct($class, $property, \ReflectionProperty $reflection = null) { if (is_string($class)) { if (empty($class)) { throw new \RuntimeException('Class name can not be empty'); } if (!class_exists($class)) { throw new \RuntimeException('Class is not defined'); } } else { if (!is_object($class)) { throw new \RuntimeException('Invalid class type, type - ' . gettype($class)); } } $this->class = $class; if (!is_string($property) || empty($property)) { throw new \RuntimeException('Property can not be null and should be string'); } $this->name = $property; $this->setReflection($reflection); if (property_exists($class, $property)) { $reflection = $this->getReflection(); /** @var \ReflectionProperty $reflection */ if ($reflection->isPublic()) { $this->setAccess(Access::ACCESS_PUBLIC); } else { if ($reflection->isPrivate()) { $this->setAccess(Access::ACCESS_PRIVATE); } else { if ($reflection->isProtected()) { $this->setAccess(Access::ACCESS_PROTECTED); } } } $reflection->setAccessible(true); if (is_object($class)) { $this->setValue($reflection->getValue($class)); } else { $properties = Reflection::getClass($class)->getDefaultProperties(); if (array_key_exists($property, $properties)) { $this->setValue($properties[$property]); } } } }