/** * @param ReflectionParameter $parameter * @param CollectionInterface $properties * * @return bool */ private function canInject(\ReflectionParameter $parameter, CollectionInterface $properties) : bool { if (!$parameter->allowsNull() && !$properties->hasKey($parameter->name)) { return false; } else { if ($parameter->allowsNull() && !$properties->hasKey($parameter->name)) { return false; } } $property = $properties[$parameter->name]; if ($parameter->hasType()) { $type = $parameter->getType(); if ($type->isBuiltin()) { return (string) $type === gettype($property); } else { if (!is_object($property)) { return false; } } $refl = new \ReflectionObject($property); $wishedClass = (string) $type; return get_class($property) === $wishedClass || $refl->isSubClassOf($wishedClass); } return true; }
/** * {@inheritdoc} */ public function make(IdentityInterface $identity, EntityInterface $meta, CollectionInterface $data) { if (!$meta instanceof Relationship) { throw new InvalidArgumentException(); } $reflection = (new ReflectionClass((string) $meta->class()))->withProperty($meta->identity()->property(), $identity)->withProperty($meta->startNode()->property(), $this->generators->get($meta->startNode()->type())->for($data->get($meta->startNode()->property())))->withProperty($meta->endNode()->property(), $this->generators->get($meta->endNode()->type())->for($data->get($meta->endNode()->property()))); $meta->properties()->foreach(function (string $name, Property $property) use(&$reflection, $data) { if ($property->type()->isNullable() && !$data->hasKey($name)) { return; } $reflection = $reflection->withProperty($name, $property->type()->fromDatabase($data->get($name))); }); return $reflection->buildObject(); }