private function diff(CollectionInterface $source, CollectionInterface $target) : CollectionInterface { $changeset = new Collection([]); $target->each(function (string $property, $value) use(&$changeset, $source) { if (!$source->hasKey($property) || $value !== $source->get($property)) { $changeset = $changeset->set($property, $value); } }); $source->each(function (string $property) use(&$changeset, $target) { if (!$target->hasKey($property)) { $changeset = $changeset->set($property, null); } }); $changeset->each(function (string $property, $value) use(&$changeset, $source, $target) { if (!$value instanceof CollectionInterface) { return; } $changeset = $changeset->set($property, $this->diff($source->get($property), $target->get($property))); }); $changeset = $changeset->filter(function ($value) { if (!$value instanceof CollectionInterface) { return true; } return $value->count() !== 0; }); return $changeset; }
private function appendProperties(Relationship $relationship, CollectionInterface $properties) : Relationship { $properties->each(function (string $name, array $config) use(&$relationship) { $relationship = $relationship->withProperty($name, $this->types->build($config['type'], new Collection($config))); }); return $relationship; }
private function appendChildren(Aggregate $entity, CollectionInterface $children) : Aggregate { $children->each(function (string $name, array $config) use(&$entity) { $config = new Collection($config); $rel = new ValueObjectRelationship(new ClassName($config->get('class')), new RelationshipType($config->get('type')), $name, $config->get('child')['property'], $config->hasKey('collection') ? (bool) $config->get('collection') : false); if ($config->hasKey('properties')) { $rel = $this->appendProperties($rel, new Collection($config->get('properties'))); } $config = new Collection($config->get('child')); $child = new ValueObject(new ClassName($config->get('class')), $config->get('labels'), $rel); if ($config->hasKey('properties')) { $child = $this->appendProperties($child, new Collection($config->get('properties'))); } $entity = $entity->withChild($child); }); return $entity; }