]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/SearchOptions.php
Laravel 7.x Shift (#3011)
[bookstack] / app / Entities / Tools / SearchOptions.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use Illuminate\Http\Request;
6
7 class SearchOptions
8 {
9     /**
10      * @var array
11      */
12     public $searches = [];
13
14     /**
15      * @var array
16      */
17     public $exacts = [];
18
19     /**
20      * @var array
21      */
22     public $tags = [];
23
24     /**
25      * @var array
26      */
27     public $filters = [];
28
29     /**
30      * Create a new instance from a search string.
31      */
32     public static function fromString(string $search): self
33     {
34         $decoded = static::decode($search);
35         $instance = new static();
36         foreach ($decoded as $type => $value) {
37             $instance->$type = $value;
38         }
39
40         return $instance;
41     }
42
43     /**
44      * Create a new instance from a request.
45      * Will look for a classic string term and use that
46      * Otherwise we'll use the details from an advanced search form.
47      */
48     public static function fromRequest(Request $request): self
49     {
50         if (!$request->has('search') && !$request->has('term')) {
51             return static::fromString('');
52         }
53
54         if ($request->has('term')) {
55             return static::fromString($request->get('term'));
56         }
57
58         $instance = new static();
59         $inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']);
60         $instance->searches = explode(' ', $inputs['search'] ?? []);
61         $instance->exacts = array_filter($inputs['exact'] ?? []);
62         $instance->tags = array_filter($inputs['tags'] ?? []);
63         foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) {
64             if (empty($filterVal)) {
65                 continue;
66             }
67             $instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal;
68         }
69         if (isset($inputs['types']) && count($inputs['types']) < 4) {
70             $instance->filters['type'] = implode('|', $inputs['types']);
71         }
72
73         return $instance;
74     }
75
76     /**
77      * Decode a search string into an array of terms.
78      */
79     protected static function decode(string $searchString): array
80     {
81         $terms = [
82             'searches' => [],
83             'exacts'   => [],
84             'tags'     => [],
85             'filters'  => [],
86         ];
87
88         $patterns = [
89             'exacts'  => '/"(.*?)"/',
90             'tags'    => '/\[(.*?)\]/',
91             'filters' => '/\{(.*?)\}/',
92         ];
93
94         // Parse special terms
95         foreach ($patterns as $termType => $pattern) {
96             $matches = [];
97             preg_match_all($pattern, $searchString, $matches);
98             if (count($matches) > 0) {
99                 $terms[$termType] = $matches[1];
100                 $searchString = preg_replace($pattern, '', $searchString);
101             }
102         }
103
104         // Parse standard terms
105         foreach (explode(' ', trim($searchString)) as $searchTerm) {
106             if ($searchTerm !== '') {
107                 $terms['searches'][] = $searchTerm;
108             }
109         }
110
111         // Split filter values out
112         $splitFilters = [];
113         foreach ($terms['filters'] as $filter) {
114             $explodedFilter = explode(':', $filter, 2);
115             $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
116         }
117         $terms['filters'] = $splitFilters;
118
119         return $terms;
120     }
121
122     /**
123      * Encode this instance to a search string.
124      */
125     public function toString(): string
126     {
127         $string = implode(' ', $this->searches ?? []);
128
129         foreach ($this->exacts as $term) {
130             $string .= ' "' . $term . '"';
131         }
132
133         foreach ($this->tags as $term) {
134             $string .= " [{$term}]";
135         }
136
137         foreach ($this->filters as $filterName => $filterVal) {
138             $string .= ' {' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}';
139         }
140
141         return $string;
142     }
143 }