]> BookStack Code Mirror - bookstack/blob - app/Search/SearchOptionSet.php
467dc9f5ae1d7ff4dac84f56ff6d33d94274d157
[bookstack] / app / Search / SearchOptionSet.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use BookStack\Search\Options\SearchOption;
6
7 class SearchOptionSet
8 {
9     /**
10      * @var SearchOption[]
11      */
12     protected array $options = [];
13
14     public function __construct(array $options = [])
15     {
16         $this->options = $options;
17     }
18
19     public function toValueArray(): array
20     {
21         return array_map(fn(SearchOption $option) => $option->value, $this->options);
22     }
23
24     public function toValueMap(): array
25     {
26         $map = [];
27         foreach ($this->options as $index => $option) {
28             $key = $option->getKey() ?? $index;
29             $map[$key] = $option->value;
30         }
31         return $map;
32     }
33
34     public function merge(SearchOptionSet $set): self
35     {
36         return new self(array_merge($this->options, $set->options));
37     }
38
39     public function filterEmpty(): self
40     {
41         $filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
42         return new self($filteredOptions);
43     }
44
45     /**
46      * @param class-string<SearchOption> $class
47      */
48     public static function fromValueArray(array $values, string $class): self
49     {
50         $options = array_map(fn($val) => new $class($val), $values);
51         return new self($options);
52     }
53
54     /**
55      * @return SearchOption[]
56      */
57     public function all(): array
58     {
59         return $this->options;
60     }
61
62     /**
63      * @return SearchOption[]
64      */
65     public function negated(): array
66     {
67         return array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
68     }
69 }