コード例 #1
0
ファイル: Types.php プロジェクト: innmind/rest-server
 /**
  * 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);
 }
コード例 #2
0
ファイル: Map.php プロジェクト: versionable/common
 public function putAll(MapInterface $map)
 {
     foreach ($map->toArray() as $value) {
         $this->doCheckValid($value);
     }
     $this->elements = array_merge($this->elements, $map->toArray());
 }
コード例 #3
0
ファイル: DefaultFactory.php プロジェクト: innmind/http
 public function __construct(MapInterface $factories)
 {
     if ((string) $factories->keyType() !== 'string' || (string) $factories->valueType() !== HeaderFactoryInterface::class) {
         throw new InvalidArgumentException();
     }
     $this->factories = $factories;
 }
コード例 #4
0
 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;
 }
コード例 #5
0
ファイル: Locator.php プロジェクト: innmind/rest-server
 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);
 }
コード例 #6
0
ファイル: Formats.php プロジェクト: innmind/rest-client
 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();
 }
コード例 #7
0
 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;
 }
コード例 #8
0
ファイル: Headers.php プロジェクト: innmind/http
 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);
     });
 }
コード例 #9
0
ファイル: Directory.php プロジェクト: innmind/rest-server
 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;
 }
コード例 #10
0
 /**
  * {@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));
     });
 }
コード例 #11
0
ファイル: Alternates.php プロジェクト: innmind/crawler
 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();
         }
     });
 }
コード例 #12
0
ファイル: LanguagesParser.php プロジェクト: innmind/crawler
 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()));
 }
コード例 #13
0
ファイル: DateType.php プロジェクト: innmind/rest-server
 /**
  * {@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;
 }
コード例 #14
0
 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;
 }
コード例 #15
0
ファイル: LinkValue.php プロジェクト: innmind/http
 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));
 }
コード例 #16
0
ファイル: MediaType.php プロジェクト: innmind/filesystem
 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;
 }
コード例 #17
0
 /**
  * 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;
 }
コード例 #18
0
ファイル: MapType.php プロジェクト: innmind/rest-server
 /**
  * {@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;
 }
コード例 #19
0
ファイル: AcceptValue.php プロジェクト: innmind/http
 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));
 }
コード例 #20
0
ファイル: RemovePersister.php プロジェクト: innmind/neo4j-onm
 /**
  * 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;
 }
コード例 #21
0
ファイル: FindContentNode.php プロジェクト: innmind/crawler
 /**
  * @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);
 }
コード例 #22
0
ファイル: InsertPersister.php プロジェクト: innmind/neo4j-onm
 /**
  * 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;
 }
コード例 #23
0
ファイル: Lexer.php プロジェクト: NoUseFreak/calculator
 private function getParts($input)
 {
     preg_match_all($this->map->getRegex(), $input, $matches);
     return $matches[0];
 }
コード例 #24
0
ファイル: MapIterator.php プロジェクト: bazo/php-orientdb
 /**
  * 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);
 }
コード例 #25
0
ファイル: Map.php プロジェクト: Innmind/Immutable
 /**
  * {@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;
 }
コード例 #26
0
ファイル: UpdatePersister.php プロジェクト: innmind/neo4j-onm
 /**
  * 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);
     });
 }
コード例 #27
0
ファイル: HashMap.php プロジェクト: BuildrPHP/Collection
 /**
  * {@inheritDoc}
  */
 public function merge(MapInterface $map)
 {
     $self = $this;
     $map->each(function ($key, $value) use($self) {
         if (!$self->containsKey($key)) {
             $self->put($key, $value);
         }
     });
 }