namespace BookStack\Search;
+use BookStack\Search\Options\SearchOption;
+
+/**
+ * @template T of SearchOption
+ */
class SearchOptionSet
{
/**
- * @var SearchOption[]
+ * @var T[]
*/
- public array $options = [];
+ protected array $options = [];
public function __construct(array $options = [])
{
public function toValueMap(): array
{
$map = [];
- foreach ($this->options as $key => $option) {
+ foreach ($this->options as $index => $option) {
+ $key = $option->getKey() ?? $index;
$map[$key] = $option->value;
}
return $map;
public function filterEmpty(): self
{
- $filteredOptions = array_filter($this->options, fn (SearchOption $option) => !empty($option->value));
+ $filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
return new self($filteredOptions);
}
- public static function fromValueArray(array $values): self
+ /**
+ * @param class-string<SearchOption> $class
+ */
+ public static function fromValueArray(array $values, string $class): self
{
- $options = array_map(fn($val) => new SearchOption($val), $values);
+ $options = array_map(fn($val) => new $class($val), $values);
return new self($options);
}
- public static function fromMapArray(array $values): self
+ /**
+ * @return T[]
+ */
+ public function all(): array
{
- $options = [];
- foreach ($values as $key => $value) {
- $options[$key] = new SearchOption($value);
- }
- return new self($options);
+ return $this->options;
+ }
+
+ /**
+ * @return self<T>
+ */
+ public function negated(): self
+ {
+ $values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
+ return new self($values);
+ }
+
+ /**
+ * @return self<T>
+ */
+ public function nonNegated(): self
+ {
+ $values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
+ return new self($values);
}
}