private function extractProperties($object, MapInterface $properties) : CollectionInterface { $refl = new ReflectionObject($object); $data = new Collection([]); $properties->foreach(function (string $name, Property $property) use(&$data, $refl) { $data = $data->set($name, $property->type()->forDatabase($refl->extract([$name])->get($name))); }); return $data; }
public function __construct(MapInterface $alternates) { $this->attributes = new Attributes('alternates', $alternates); $alternates->foreach(function (string $language, AttributeInterface $alternate) { if (!$alternate instanceof Alternate) { throw new InvalidArgumentException(); } }); }
private function merge(MapInterface $left, MapInterface $right) : MapInterface { $map = $left; $right->foreach(function (string $var, SequenceInterface $data) use(&$map, $left) { if (!$map->contains($var)) { $map = $map->put($var, $data); return; } $map = $map->put($var, new Sequence($data->get(0)->merge($left->get($var)->get(0)), $data->get(1)->merge($left->get($var)->get(1)))); }); return $map; }
/** * Translate a raw dbal result into formated data usable for entity factories * * @param ResultInterface $result * @param MapInterface<string, EntityInterface> $variables Association between query variables and entity definitions * * @return MapInterface<string, CollectionInterface> */ public function translate(ResultInterface $result, MapInterface $variables) : MapInterface { $mapped = new Map('string', CollectionInterface::class); $variables->foreach(function (string $variable, EntityInterface $meta) use(&$mapped, $result) { $forVariable = $result->rows()->filter(function (RowInterface $row) use($variable) { return $row->column() === $variable; }); if ($forVariable->count() === 0) { return; } $translator = $this->translators->get(get_class($meta)); $mapped = $mapped->put($variable, $translator->translate($variable, $meta, $result)); }); return $mapped; }
/** * Build the query to update all entities at once * * @param MapInterface<IdentityInterface, CollectionInterface> $changesets * @param MapInterface<IdentityInterface, object> $entities * * @return QueryInterface */ private function queryFor(MapInterface $changesets, MapInterface $entities) : QueryInterface { $query = new Query(); $this->variables = new Map(Str::class, CollectionInterface::class); $changesets->foreach(function (IdentityInterface $identity, CollectionInterface $changeset) use(&$query, $entities) { $entity = $entities->get($identity); $meta = $this->metadatas->get(get_class($entity)); if ($meta instanceof Aggregate) { $query = $this->matchAggregate($identity, $entity, $meta, $changeset, $query); } else { if ($meta instanceof Relationship) { $query = $this->matchRelationship($identity, $entity, $meta, $changeset, $query); } } }); $this->variables->foreach(function (Str $variable, CollectionInterface $changeset) use(&$query) { $query = $this->update($variable, $changeset, $query); }); $this->variables = null; return $query; }
/** * Build the collection of properties to be injected in the query * * @param MapInterface<string, Property> $properties * @param Str $name * * @return CollectionInterface */ private function buildProperties(MapInterface $properties, Str $name) : CollectionInterface { $data = new Collection([]); $name = $name->prepend('{')->append('}.'); $properties->foreach(function (string $property) use(&$data, $name) { $data = $data->set($property, (string) $name->append($property)); }); return $data; }
/** * {@inheritdoc} */ public function merge(MapInterface $map) : MapInterface { if (!$this->keyType()->equals($map->keyType()) || !$this->valueType()->equals($map->valueType())) { throw new InvalidArgumentException('The 2 maps does not reference the same types'); } $newMap = clone $this; $map->foreach(function ($key, $value) use(&$newMap) { $newMap = $newMap->put($key, $value); }); return $newMap; }