/** * Build a new type instance of the wished type * * @param string $type * @param MapInterface<scalar, variable> $config * * @return TypeInterface */ public function build(string $type, MapInterface $config) : TypeInterface { if ((string) $config->keyType() !== 'scalar' || (string) $config->valueType() !== 'variable') { throw new InvalidArgumentException(); } return call_user_func([$this->types->get($type), 'fromConfig'], $config, $this); }
public function putAll(MapInterface $map) { foreach ($map->toArray() as $value) { $this->doCheckValid($value); } $this->elements = array_merge($this->elements, $map->toArray()); }
public function __construct(MapInterface $factories) { if ((string) $factories->keyType() !== 'string' || (string) $factories->valueType() !== HeaderFactoryInterface::class) { throw new InvalidArgumentException(); } $this->factories = $factories; }
private function addProperties(Query $query, string $name, MapInterface $mapping) : Query { if ($mapping->contains($name)) { $query = $query->withProperties($mapping->get($name)->get(0)->toPrimitive())->withParameters($mapping->get($name)->get(1)->toPrimitive()); } return $query; }
public function __construct(MapInterface $directories) { if ((string) $directories->keyType() !== 'string' || (string) $directories->valueType() !== Directory::class) { throw new InvalidArgumentException(); } $this->directories = $directories; $this->cache = new Map('string', HttpResource::class); }
public function __construct(MapInterface $formats) { if ((string) $formats->keyType() !== 'string' || (string) $formats->valueType() !== Format::class || $formats->size() === 0) { throw new InvalidArgumentException(); } $this->formats = $formats; $this->negotiator = new Negotiator(); }
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 $headers) { if ((string) $headers->keyType() !== 'string' || (string) $headers->valueType() !== HeaderInterface::class) { throw new InvalidArgumentException(); } $this->headers = $headers->map(function (string $name, HeaderInterface $header) { return new Pair((string) (new Str($name))->toLower(), $header); }); }
public function __construct(string $name, MapInterface $children, MapInterface $definitions) { if ((string) $children->keyType() !== 'string' || (string) $children->valueType() !== self::class || (string) $definitions->keyType() !== 'string' || (string) $definitions->valueType() !== HttpResource::class) { throw new InvalidArgumentException(); } $this->name = $name; $this->children = $children; $this->definitions = $definitions; }
/** * {@inheritdoc} */ public function build(ServerRequestInterface $request, Reference $from, MapInterface $tos) : MapInterface { if ((string) $tos->keyType() !== Reference::class || (string) $tos->valueType() !== MapInterface::class) { throw new InvalidArgumentException(); } return $this->builders->reduce(new Map('string', HeaderInterface::class), function (MapInterface $carry, LinkBuilderInterface $builder) use($request, $from, $tos) : MapInterface { return $carry->merge($builder->build($request, $from, $tos)); }); }
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(); } }); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Content-Language') || !$response->headers()->get('Content-Language') instanceof ContentLanguage) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $response->headers()->get('Content-Language')->values()->reduce(new Set('string'), function (Set $carry, ContentLanguageValue $language) : Set { return $carry->add((string) $language); }), $this->clock->now()->elapsedSince($start)->milliseconds())); }
/** * {@inheritdoc} */ public static function fromConfig(MapInterface $config, Types $types) : TypeInterface { if ((string) $config->keyType() !== 'scalar' || (string) $config->valueType() !== 'variable') { throw new InvalidArgumentException(); } $type = new self(); if ($config->contains('format')) { $type->format = (string) $config->get('format'); } return $type; }
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; }
public function __construct(UrlInterface $url, string $rel, MapInterface $parameters) { if (empty($rel) || (string) $parameters->keyType() !== 'string' || (string) $parameters->valueType() !== ParameterInterface::class) { throw new InvalidArgumentException(); } $this->url = $url; $this->rel = $rel; $this->parameters = $parameters; $parameters = $parameters->values()->join(';'); $parameters = $parameters->length() > 0 ? $parameters->prepend(';') : $parameters; $link = (new Str('<%s>; rel="%s"'))->sprintf((string) $url, $rel); parent::__construct((string) $link->append((string) $parameters)); }
public function __construct(string $topLevel, string $subType, string $suffix, MapInterface $parameters) { if ((string) $parameters->keyType() !== 'string' || (string) $parameters->valueType() !== ParameterInterface::class) { throw new InvalidArgumentException(); } if (!self::topLevels()->contains($topLevel)) { throw new InvalidTopLevelTypeException(); } $this->topLevel = $topLevel; $this->subType = $subType; $this->suffix = $suffix; $this->parameters = $parameters; }
/** * 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; }
/** * {@inheritdoc} */ public static function fromConfig(MapInterface $config, Types $types) : TypeInterface { if ((string) $config->keyType() !== 'scalar' || (string) $config->valueType() !== 'variable') { throw new InvalidArgumentException(); } $type = new self(); $type->innerKey = $config->get('key'); $type->innerValue = $config->get('inner'); $type->inner = $types->build($config->get('inner'), $config->remove('inner')->remove('key')); $type->key = $types->build($config->get('key'), $config->remove('inner')->remove('key')); return $type; }
public function __construct(string $type, string $subType, MapInterface $parameters) { $media = (new Str('%s/%s'))->sprintf($type, $subType); if (!$media->match('~^\\*/\\*$~') && !$media->match('~^[\\w\\-.]+/\\*$~') && !$media->match('~^[\\w\\-.]+/[\\w\\-.]+$~')) { throw new InvalidArgumentException(); } if ((string) $parameters->keyType() !== 'string' || (string) $parameters->valueType() !== ParameterInterface::class) { throw new InvalidArgumentException(); } $this->type = $type; $this->subType = $subType; $this->parameters = $parameters; $parameters = $parameters->values()->join(';'); $parameters = $parameters->length() > 0 ? $parameters->prepend(';') : $parameters; parent::__construct((string) $media->append((string) $parameters)); }
/** * Build the query to delete all entities at once * * @param MapInterface<IdentityInterface, object> $entities * * @return QueryInterface */ private function queryFor(MapInterface $entities) : QueryInterface { $query = new Query(); $this->variables = new Set('string'); $partitions = $entities->partition(function (IdentityInterface $identity, $entity) { $meta = $this->metadatas->get(get_class($entity)); return $meta instanceof Relationship; }); $partitions->get(true)->foreach(function (IdentityInterface $identity, $entity) use(&$query) { $query = $this->matchRelationship($identity, $entity, $query); }); $partitions->get(false)->foreach(function (IdentityInterface $identity, $entity) use(&$query) { $query = $this->matchAggregate($identity, $entity, $query); }); $this->variables->foreach(function (string $variable) use(&$query) { $query = $query->delete($variable); }); $this->variables = null; return $query; }
/** * @param MapInterface<int, NodeInterface> $nodes */ public function __invoke(MapInterface $nodes) : NodeInterface { if ((string) $nodes->keyType() !== 'int' || (string) $nodes->valueType() !== NodeInterface::class) { throw new InvalidArgumentException(); } if ($nodes->size() === 1) { if (!$nodes->current()->hasChildren()) { return $nodes->current(); } try { return $this($nodes->current()->children()); } catch (ContentTooDispersedException $e) { return $nodes->current(); } } $dispersion = $nodes->reduce([], function (array $dispersion, int $position, NodeInterface $node) : array { $text = (new Text())($node); $text = new Str($text); $dispersion[$position] = $text->wordCount(); return $dispersion; }); $quantile = new Quantile($dispersion); $lookup = []; //select qartiles that have more words than the average nodes for ($i = 1; $i < 5; $i++) { $diff = $quantile->quartile($i)->value() - $quantile->quartile($i - 1)->value(); if ($diff >= $quantile->mean()) { $lookup[] = $i; } } if (empty($lookup)) { throw new ContentTooDispersedException(); } //select the minimum amount of words that needs to be in nodes $min = $quantile->quartile(min($lookup))->value(); $nodes = $nodes->filter(function (int $position) use($min, $dispersion) : bool { return $dispersion[$position] >= $min; }); return $this($nodes); }
/** * 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; }
private function getParts($input) { preg_match_all($this->map->getRegex(), $input, $matches); return $matches[0]; }
/** * Iterator Constructor. * * @param MapInterface $map The map to iterate */ public function __construct(MapInterface $map) { $this->map = $map; $this->keys = $map->keys(); $this->key = reset($this->keys); }
/** * {@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; }
/** * Build a collection with only the elements that are properties * * @param CollectionInterface $changeset * @param MapInterface<string, Property> $properties * * @return CollectionInterface */ private function buildProperties(CollectionInterface $changeset, MapInterface $properties) : CollectionInterface { return $changeset->filter(function ($data, string $property) use($properties) { return $properties->contains($property); }); }
/** * {@inheritDoc} */ public function merge(MapInterface $map) { $self = $this; $map->each(function ($key, $value) use($self) { if (!$self->containsKey($key)) { $self->put($key, $value); } }); }