private function isValidProperty(string $property, EntityInterface $meta) : bool { if ($meta->properties()->contains($property)) { return true; } return $meta->startNode()->property() === $property || $meta->endNode()->property() === $property; }
private function isValidProperty(string $property, EntityInterface $meta) : bool { if ($meta->properties()->contains($property)) { return true; } $property = new Str($property); if (!$property->match('/[a-zA-Z]+(\\.[a-zA-Z]+)+/')) { return false; } $pieces = $property->split('.'); $piece = (string) $pieces->get(0); if (!$meta->children()->contains($piece)) { return false; } $child = $meta->children()->get($piece); $relationship = $child->relationship(); switch ($pieces->count()) { case 2: return $relationship->properties()->contains((string) $pieces->get(1)); case 3: $subPiece = (string) $pieces->get(1); if (!$relationship->childProperty() === $subPiece) { return false; } return $child->properties()->contains((string) $pieces->get(2)); } return false; }
/** * {@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; }
/** * {@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(); }
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 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(); }
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; }