minCount = $minCount; $this->maxCount = $maxCount; $this->limit = $limit; $this->targetCount = $targetCount; $this->type = $type; } /** * Build a range query from named fields (the only factory). * * Required keys: minCount, maxCount (int). Optional keys: limit (int), * targetCount (int|null), type (string|null). A missing required key or a * value of any other type throws InvalidArgumentException rather than * being coerced, so a transposed or garbage argument fails at the call. * * @param array $fields * @return self */ public static function fromArray(array $fields): self { $targetCount = array_key_exists('targetCount', $fields) && $fields['targetCount'] !== null ? self::intField($fields, 'targetCount') : null; $type = null; if (array_key_exists('type', $fields) && $fields['type'] !== null) { if (!is_string($fields['type'])) { throw new InvalidArgumentException('NGramCountRangeQuery field must be string or null: type'); } $type = $fields['type']; } return new self( self::intField($fields, 'minCount'), self::intField($fields, 'maxCount'), array_key_exists('limit', $fields) ? self::intField($fields, 'limit') : ABJ_404_Solution_NGramCacheRepository::CACHE_LOAD_LIMIT, $targetCount, $type ); } /** @param array $fields */ private static function intField(array $fields, string $name): int { if (!array_key_exists($name, $fields)) { throw new InvalidArgumentException('NGramCountRangeQuery is missing required field: ' . $name); } if (!is_int($fields[$name])) { throw new InvalidArgumentException('NGramCountRangeQuery field must be int: ' . $name); } return $fields[$name]; } public function minCount(): int { return $this->minCount; } public function maxCount(): int { return $this->maxCount; } public function limit(): int { return $this->limit; } public function type(): ?string { return $this->type; } /** * The ngram_count the two-range read orders around: the target clamped * into [minCount, maxCount], or the integer midpoint when no target was * given. */ public function orderTarget(): int { if ($this->targetCount !== null) { return max($this->minCount, min($this->maxCount, $this->targetCount)); } return (int)(($this->minCount + $this->maxCount) / 2); } }