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; }
/** * {@inheritdoc} */ public static function fromConfig(CollectionInterface $config) : TypeInterface { $type = new self(); if ($config->hasKey('nullable')) { $type->nullable = true; } return $type; }
/** * * @param CollectionInterface $collection * @return boolean */ public function containsAll(CollectionInterface $collection) { $iterator = $collection->getIterator(); foreach ($iterator as $element) { if (!$this->contains($element)) { return false; } } return true; }
/** * Adds a set of elements as long no duplicates are found * * @param CollectionInterface $collection * @return boolean */ public function addAll(CollectionInterface $collection = NULL) { $iterator = $collection->getIterator(); foreach ($iterator as $element) { if ($this->contains($element)) { throw new \InvalidArgumentException('Duplicate element are not allowed'); } } return parent::addAll($collection); }
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 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(); }
public function equals(CollectionInterface $collection) { if ($collection->count() !== $this->count()) { return false; } foreach ($collection as $key => $item) { if ($this->_collection[$key] !== $item) { return false; } } return true; }
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; }
/** * Retains only the elements in this collection that are contained in the specified collection (optional operation). * @param CollectionInterface $collection * @return boolean */ public function retainAll(CollectionInterface $collection) { $iterator = $this->iterator(); $changed = false; while ($iterator->hasNext()) { if (!$collection->contains($iterator->next())) { $iterator->remove(); $changed = true; } } return $changed; }
/** * {@inheritdoc} */ public static function fromConfig(CollectionInterface $config) : TypeInterface { $type = new self(); if (!$config->hasKey('inner')) { throw TypeDeclarationException::missingField('inner'); } if (self::identifiers()->contains($config->get('inner'))) { throw new RecursiveTypeDeclarationException(); } $innerConfig = $config->unset('inner')->unset('_types'); if ($config->hasKey('nullable')) { $type->nullable = true; $innerConfig = $innerConfig->unset('nullable'); } $type->type = $config->get('inner'); $type->inner = $config->get('_types')->build($config->get('inner'), $innerConfig); return $type; }
/** * {@inheritdoc} */ public static function fromConfig(CollectionInterface $config) : TypeInterface { $type = new self(); if ($config->hasKey('nullable')) { $type->nullable = true; } if ($config->hasKey('format')) { $type->format = $config->get('format'); } if ($config->hasKey('immutable')) { $type->immutable = (bool) $config->get('immutable'); } return $type; }
/** * 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()]); }
/** * Check if operation access is allowed * * @param string $access * @param \Rbac\CollectionInterface $collection * * @return bool */ public function isAllowed($access, CollectionInterface $collection) { return $collection->isAllowed($access); }
/** * Get number of ocurrences. * @return int */ public function count() { return $this->collection->count(); }
public function copyTo(CollectionInterface $collection) { $this->each(function ($item) use($collection) { $collection->push($item); }); }
/** * Add a clause to set all properties to be updated on the wished variable * * @param Str $variable * @param CollectionInterface $changeset * @param Query $query * * @return Query */ private function update(Str $variable, CollectionInterface $changeset, Query $query) : Query { return $query->set(sprintf('%s += {%s_props}', $variable, $variable))->withParameter((string) $variable->append('_props'), $changeset->toPrimitive()); }