public function __construct(MapInterface $factories) { if ((string) $factories->keyType() !== 'string' || (string) $factories->valueType() !== HeaderFactoryInterface::class) { throw new InvalidArgumentException(); } $this->factories = $factories; }
/** * 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 __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(); }
/** * {@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(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; }
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); }); }
/** * {@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('inner'); $type->inner = $types->build($config->get('inner'), $config->remove('inner')); return $type; }
/** * {@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; }
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; }
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 $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)); }
/** * @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); }
/** * {@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; }