/** * @param array $transformed * @param string $type * @return object * @throws \Exception */ protected function revertObject($transformed, $type) { $class = new \ReflectionClass($type); $instance = $class->newInstanceWithoutConstructor(); if (!is_array($transformed)) { throw new \Exception("Error while reverting [{$type}]. Input is not an array: " . ValuePrinter::serialize($transformed)); } $reader = new PropertyReader($this->types, $class->getName()); $properties = []; foreach ($reader->readState() as $property) { $properties[] = $property->name(); if (array_key_exists($property->name(), $transformed)) { $value = new TypedValue($transformed[$property->name()], $property->type()); $transformer = $this->transformers->toRevert($value); $property->set($instance, $transformer->revert($value)); } } foreach ($transformed as $key => $value) { if (!in_array($key, $properties)) { $transformer = $this->transformers->toRevert($value); $instance->{$key} = $transformer->revert($value); } } return $instance; }
public function __construct(array $objects, TypeFactory $types) { $this->objects = $objects; $this->properties = []; if ($objects) { $reader = new PropertyReader($types, get_class($objects[0])); $this->properties = $reader->readInterface($objects[0])->filter(function (Property $property) { return $property->canGet(); }); } }
/** * @param $value * @return array */ private function getProperties($value) { $reader = new PropertyReader($this->types, get_class($value)); $map = []; foreach ($reader->readInterface($value) as $property) { if (!$property->canGet()) { continue; } $map[$property->name()] = $property->get($value); } return $map; }
function determineTypeByHintToOtherConstants() { eval('class EnumerationHintToOtherConstants { /** @var ClassContainingTheConstants::FOO* */ public $foo; }'); eval('class ClassContainingTheConstants { const FOO_ONE = "one"; const FOO_TWO = "two"; }'); $reader = new PropertyReader(new TypeFactory(), 'EnumerationHintToOtherConstants'); $properties = $reader->readInterface(); $this->assert($properties['foo']->type(), new EnumerationType(['one' => 'One', 'two' => 'Two'], new StringType())); }
/** * @param mixed $value * @return mixed */ public function render($value) { if (method_exists($value, '__toString')) { return (string) $value; } $output = ['### ' . (new \ReflectionClass($value))->getShortName() . ' ###']; $reader = new PropertyReader($this->types, $value); foreach ($reader->readInterface($value) as $property) { if (!$property->canGet()) { continue; } $propertyValue = $property->get($value); $rendered = $this->renderers->getRenderer($propertyValue)->render($propertyValue); $output[] = ucfirst($property->name()) . ': ' . rtrim(str_replace(PHP_EOL, PHP_EOL . ' ', $rendered)); } return implode(PHP_EOL, $output) . PHP_EOL; }
/** * @param Parameter $parameter * @param null $serialized * @return object */ public function inflate(Parameter $parameter, $serialized) { $reader = new PropertyReader($this->types, $this->getClass($parameter)); $properties = []; foreach ($reader->readInterface() as $property) { $propertyParameter = new Parameter($parameter->getName() . '-' . $property->name(), $property->type()); $field = $this->getField($propertyParameter); $properties[$property->name()] = $field->inflate($propertyParameter, $this->reader->read($propertyParameter)); } $injector = new Injector(new Factory()); $instance = $injector->injectConstructor($this->getClass($parameter), $properties, function () { return false; }); foreach ($reader->readInterface() as $property) { $value = $properties[$property->name()]; if (!is_null($value) && $property->canSet()) { $property->set($instance, $value); } } return $instance; }
protected function createInstance(array $parameters) { $injector = new Injector(new Factory()); $instance = $injector->injectConstructor($this->class->name, $parameters, function () { return false; }); foreach ($this->reader->readInterface() as $property) { if ($property->canSet() && array_key_exists($property->name(), $parameters)) { $property->set($instance, $parameters[$property->name()]); } } return $instance; }
private function whenIReadTheStatePropertiesOf_WithTheFilter($class, $filter) { $reader = new PropertyReader(new TypeFactory(), $class); $this->properties = $reader->readState($filter); }
private function renderPropertyFields(Parameter $parameter, $object) { $reader = new PropertyReader($this->types, $this->getClass($parameter)); $fields = []; foreach ($reader->readInterface($object) as $property) { if (!$property->canSet()) { continue; } $param = $this->makePropertyParameter($parameter, $property); $fields[] = new Element('div', ['class' => 'form-group'], [new Element('label', [], [ucfirst($property->name()) . ($property->isRequired() ? '*' : '')]), $this->getField($param)->render($param, $object ? $property->get($object) : null)]); } return $fields; }
private function whenIDetermineThePropertiesOf($class) { $reflection = new \ReflectionClass($class); $this->object = $reflection->newInstanceArgs($this->args); $reader = new PropertyReader(new TypeFactory(), $class); $this->properties = $reader->readInterface($this->object); }
private function getProperties(Parameter $parameter, $object = null) { $reader = new PropertyReader($this->types, $this->getClass($parameter)); foreach ($reader->readInterface($object) as $property) { if ($property->canSet()) { (yield $property); } } }
/** * @param object|null $object * @return \watoki\collections\Map|\watoki\reflect\Property[] indexed by property name */ public function getProperties($object = null) { $reader = new PropertyReader(new TypeFactory(), $this->getClass()); return $reader->readInterface($object); }