/** * {@inheritdoc} * * @param Type $type * @param mixed $value * @return mixed * @throws InvalidArgumentException */ public function createObject(Type $type, $value) { if (!$type->isClass()) { throw new InvalidArgumentException("Cannot create object for non-class type: '{$type->toString()}'"); } if (!is_array($value)) { throw new InvalidArgumentException("Cannot create object from non-array value: '{$value}'"); } $class = $type->toString(); $object = new $class(); foreach ($value as $name => $property) { $object->{$name} = $property; } return $object; }
/** * {@inheritdoc} * * @param Type $class * @param string $name * @return Type */ public function getType(Type $class, $name) { $reflectionObject = new ReflectionClass($class->toString()); if (!$reflectionObject->hasProperty($name)) { return Type::fromString(null); } $reflectionProperty = $reflectionObject->getProperty($name); $contextFactory = new ContextFactory(); $context = $contextFactory->createFromReflector($reflectionProperty); $phpdoc = new DocBlock($reflectionProperty, $this->convertToDocBlockContext($context)); /** @var VarTag[] $vars */ $vars = $phpdoc->getTagsByName('var'); if (count($vars) === 0) { return Type::fromString(null); } return Type::fromString($vars[0]->getType()); }
/** * Turn an object graph into another object graph * * @param mixed $value * @param string $type * @return mixed */ public function map($value, $type) { $this->visitor->visitType(Type::fromString($type)); $this->navigator->accept($this->navigator, $this->visitor, $value); return $this->visitor->getResult(); }