private function makeEntity(EntityInterface $meta, CollectionInterface $data) { $identity = $this->generators->get($meta->identity()->type())->for($data->get($meta->identity()->property())); if ($this->entities->contains($identity)) { return $this->entities->get($identity); } $entity = $this->resolver->get($meta)->make($identity, $meta, $data); $this->entities = $this->entities->push($identity, $entity, Container::STATE_MANAGED); return $entity; }
private function translateRelationship($identity, EntityInterface $meta, ResultInterface $result) : CollectionInterface { $relationship = $result->relationships()->filter(function (RelationshipInterface $relationship) use($identity, $meta) { $id = $meta->identity()->property(); $properties = $relationship->properties(); return $properties->hasKey($id) && $properties->get($id) === $identity; })->first(); $data = (new Collection([]))->set($meta->identity()->property(), $relationship->properties()->get($meta->identity()->property()))->set($meta->startNode()->property(), $result->nodes()->get($relationship->startNode()->value())->properties()->get($meta->startNode()->target()))->set($meta->endNode()->property(), $result->nodes()->get($relationship->endNode()->value())->properties()->get($meta->endNode()->target())); $meta->properties()->foreach(function (string $name, Property $property) use(&$data, $relationship) { if ($property->type()->isNullable() && !$relationship->properties()->hasKey($name)) { return; } $data = $data->set($name, $relationship->properties()->get($name)); }); return $data; }
/** * {@inheritdoc} */ public function translate(EntityInterface $meta, IdentityInterface $identity) : IdentityMatch { $query = (new Query())->match('entity', $meta->labels()->toPrimitive())->withProperty($meta->identity()->property(), '{entity_identity}')->withParameter('entity_identity', $identity->value())->with('entity'); $variables = new Set('string'); $meta->children()->foreach(function (string $property, ValueObject $child) use(&$query, &$variables) { $relName = (new Str('entity_'))->append($property); $childName = $relName->append('_')->append($child->relationship()->childProperty()); $variables = $variables->add((string) $relName)->add((string) $childName); $query = $query->match('entity')->linkedTo((string) $childName, $child->labels()->toPrimitive())->through((string) $child->relationship()->type(), (string) $relName, Relationship::LEFT); }); return new IdentityMatch($query->return('entity', ...$variables->toPrimitive()), (new Map('string', EntityInterface::class))->put('entity', $meta)); }
/** * {@inheritdoc} */ public function extract($entity, EntityInterface $meta) : CollectionInterface { if (!$meta instanceof Aggregate) { throw new InvalidArgumentException(); } $data = $this->extractProperties($entity, $meta->properties()); $data = $data->set($id = $meta->identity()->property(), (new ReflectionObject($entity))->extract([$id])->get($id)->value()); $meta->children()->foreach(function (string $property, ValueObject $child) use(&$data, $entity) { $data = $data->set($property, $this->extractRelationship($child, $entity)); }); return $data; }
/** * {@inheritdoc} */ public function extract($entity, EntityInterface $meta) : CollectionInterface { if (!$meta instanceof Relationship) { throw new InvalidArgumentException(); } $refl = new ReflectionObject($entity); $data = $refl->extract([$id = $meta->identity()->property(), $start = $meta->startNode()->property(), $end = $meta->endNode()->property()]); $data = $data->set($id, $data->get($id)->value())->set($start, $data->get($start)->value())->set($end, $data->get($end)->value()); $meta->properties()->foreach(function (string $name, Property $property) use(&$data, $refl) { $data = $data->set($name, $property->type()->forDatabase($refl->extract([$name])->get($name))); }); return $data; }
private function translateNode($identity, EntityInterface $meta, ResultInterface $result) : CollectionInterface { $node = $result->nodes()->filter(function (NodeInterface $node) use($identity, $meta) { $id = $meta->identity()->property(); $properties = $node->properties(); return $properties->hasKey($id) && $properties->get($id) === $identity; })->first(); $data = (new Collection([]))->set($meta->identity()->property(), $node->properties()->get($meta->identity()->property())); $meta->properties()->foreach(function (string $name, Property $property) use(&$data, $node) { if ($property->type()->isNullable() && !$node->properties()->hasKey($name)) { return; } $data = $data->set($name, $node->properties()->get($name)); }); try { $meta->children()->foreach(function (string $name, ValueObject $meta) use(&$data, $node, $result) { $data = $data->set($name, $this->translateChild($meta, $result, $node)); }); } catch (MoreThanOneRelationshipFoundException $e) { throw $e->on($meta); } return $data; }
/** * {@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(); }
/** * {@inheritdoc} */ public function make(IdentityInterface $identity, EntityInterface $meta, CollectionInterface $data) { if (!$meta instanceof Aggregate) { throw new InvalidArgumentException(); } $reflection = (new ReflectionClass((string) $meta->class()))->withProperty($meta->identity()->property(), $identity); $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))); }); $meta->children()->foreach(function (string $property, ValueObject $meta) use(&$reflection, $data) { $reflection = $reflection->withProperty($property, $this->buildChild($meta, $data)); }); return $reflection->buildObject(); }
/** * {@inheritdoc} */ public function translate(EntityInterface $meta, IdentityInterface $identity) : IdentityMatch { $query = (new Query())->match('start')->linkedTo('end')->through((string) $meta->type(), 'entity', Relationship::RIGHT)->withProperty($meta->identity()->property(), '{entity_identity}')->withParameter('entity_identity', $identity->value())->return('start', 'end', 'entity'); return new IdentityMatch($query, (new Map('string', EntityInterface::class))->put('entity', $meta)); }