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