/**
  * @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;
 }
示例#2
0
 /**
  * {@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();
 }
示例#3
0
 /**
  * Check if the given collection is compatible with the current one
  *
  * @throws BadMethodCallException If the collection is not compatible
  *
  * @param CollectionInterface $collection
  *
  * @return void
  */
 private function validateCollection(CollectionInterface $collection)
 {
     if (!$collection instanceof self || $collection->getType() !== $this->type) {
         throw new BadMethodCallException('The given collection is not compatible');
     }
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function associativeIntersect(CollectionInterface $collection) : CollectionInterface
 {
     return new self(array_intersect_assoc($this->values, $collection->toPrimitive()));
 }