3 namespace BookStack\Search;
5 use BookStack\Search\Options\SearchOption;
8 * @template T of SearchOption
15 protected array $options = [];
20 public function __construct(array $options = [])
22 $this->options = $options;
25 public function toValueArray(): array
27 return array_map(fn(SearchOption $option) => $option->value, $this->options);
30 public function toValueMap(): array
33 foreach ($this->options as $index => $option) {
34 $key = $option->getKey() ?? $index;
35 $map[$key] = $option->value;
40 public function merge(SearchOptionSet $set): self
42 return new self(array_merge($this->options, $set->options));
45 public function filterEmpty(): self
47 $filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
48 return new self($filteredOptions);
52 * @param class-string<SearchOption> $class
54 public static function fromValueArray(array $values, string $class): self
56 $options = array_map(fn($val) => new $class($val), $values);
57 return new self($options);
63 public function all(): array
65 return $this->options;
71 public function negated(): self
73 $values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
74 return new self($values);
80 public function nonNegated(): self
82 $values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
83 return new self($values);