private function buildValueObject(ValueObject $meta, CollectionInterface $data) { $reflection = new ReflectionClass((string) $meta->class()); $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 translateValueObject(ValueObject $meta, ResultInterface $result, RelationshipInterface $relationship) : CollectionInterface { $node = $result->nodes()->get($relationship->startNode()->value()); $data = new Collection([]); $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)); }); return $data; }
/** * Add the cypher clause to build the relationship and the node corresponding * to a child of the aggregate * * @param ValueObject $meta * @param Str $nodeName * @param CollectionInterface $data * @param Query $query * * @return Query */ private function createAggregateChild(ValueObject $meta, Str $nodeName, CollectionInterface $data, Query $query) : Query { $relationshipName = $nodeName->append('_')->append($meta->relationship()->property()); $endNodeName = $relationshipName->append('_')->append($meta->relationship()->childProperty()); $endNodeProperties = $this->buildProperties($meta->properties(), $endNodeParamKey = $endNodeName->append('_props')); $relationshipProperties = $this->buildProperties($meta->relationship()->properties(), $relationshipParamKey = $relationshipName->append('_props')); return $query->create((string) $nodeName)->linkedTo((string) $endNodeName, $meta->labels()->toPrimitive())->withProperties($endNodeProperties->toPrimitive())->withParameters([(string) $endNodeParamKey => $data->get($meta->relationship()->childProperty())->toPrimitive()])->through((string) $meta->relationship()->type(), (string) $relationshipName, DBALRelationship::LEFT)->withProperties($relationshipProperties->toPrimitive())->withParameters([(string) $relationshipParamKey => $data->unset($meta->relationship()->childProperty())->toPrimitive()]); }
private function extractRelationship(ValueObject $child, $entity) : CollectionInterface { $rel = (new ReflectionObject($entity))->extract([$prop = $child->relationship()->property()])->get($prop); $data = $this->extractProperties($rel, $child->relationship()->properties())->set($prop = $child->relationship()->childProperty(), $this->extractProperties((new ReflectionObject($rel))->extract([$prop])->get($prop), $child->properties())); return $data; }