]> BookStack Code Mirror - bookstack/blob - app/Util/SimpleListOptions.php
Fixed phpstan static usage warning, updated ci flows
[bookstack] / app / Util / SimpleListOptions.php
1 <?php
2
3 namespace BookStack\Util;
4
5 use Illuminate\Http\Request;
6
7 /**
8  * Handled options commonly used for item lists within the system, providing a standard
9  * model for handling and validating sort, order and search options.
10  */
11 class SimpleListOptions
12 {
13     protected string $typeKey;
14     protected string $sort;
15     protected string $order;
16     protected string $search;
17     protected array $sortOptions = [];
18
19     public function __construct(string $typeKey, string $sort, string $order, string $search = '')
20     {
21         $this->typeKey = $typeKey;
22         $this->sort = $sort;
23         $this->order = $order;
24         $this->search = $search;
25     }
26
27     /**
28      * Create a new instance from the given request.
29      * Takes the item type (plural) that's used as a key for storing sort preferences.
30      */
31     public static function fromRequest(Request $request, string $typeKey, bool $sortDescDefault = false): self
32     {
33         $search = $request->get('search', '');
34         $sort = setting()->getForCurrentUser($typeKey . '_sort', '');
35         $order = setting()->getForCurrentUser($typeKey . '_sort_order', $sortDescDefault ? 'desc' : 'asc');
36
37         return new self($typeKey, $sort, $order, $search);
38     }
39
40     /**
41      * Configure the valid sort options for this set of list options.
42      * Provided sort options must be an array, keyed by search properties
43      * with values being user-visible option labels.
44      * Returns current options for easy fluent usage during creation.
45      */
46     public function withSortOptions(array $sortOptions): self
47     {
48         $this->sortOptions = array_merge($this->sortOptions, $sortOptions);
49
50         return $this;
51     }
52
53     /**
54      * Get the current order option.
55      */
56     public function getOrder(): string
57     {
58         return strtolower($this->order) === 'desc' ? 'desc' : 'asc';
59     }
60
61     /**
62      * Get the current sort option.
63      */
64     public function getSort(): string
65     {
66         $default = array_key_first($this->sortOptions) ?? 'name';
67         $sort = $this->sort ?: $default;
68
69         if (empty($this->sortOptions) || array_key_exists($sort, $this->sortOptions)) {
70             return $sort;
71         }
72
73         return $default;
74     }
75
76     /**
77      * Get the set search term.
78      */
79     public function getSearch(): string
80     {
81         return $this->search;
82     }
83
84     /**
85      * Get the data to append for pagination.
86      */
87     public function getPaginationAppends(): array
88     {
89         return ['search' => $this->search];
90     }
91
92     /**
93      * Get the data required by the sort control view.
94      */
95     public function getSortControlData(): array
96     {
97         return [
98             'options' => $this->sortOptions,
99             'order' => $this->getOrder(),
100             'sort' => $this->getSort(),
101             'type' => $this->typeKey,
102         ];
103     }
104 }