]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortSetOperation.php
Sorting: Finished main sort set CRUD work
[bookstack] / app / Sorting / SortSetOperation.php
1 <?php
2
3 namespace BookStack\Sorting;
4
5 enum SortSetOperation: string
6 {
7     case NameAsc = 'name_asc';
8     case NameDesc = 'name_desc';
9     case NameNumericAsc = 'name_numeric_asc';
10     case CreatedDateAsc = 'created_date_asc';
11     case CreatedDateDesc = 'created_date_desc';
12     case UpdateDateAsc = 'updated_date_asc';
13     case UpdateDateDesc = 'updated_date_desc';
14     case ChaptersFirst = 'chapters_first';
15     case ChaptersLast = 'chapters_last';
16
17     /**
18      * Provide a translated label string for this option.
19      */
20     public function getLabel(): string
21     {
22         $key = $this->value;
23         $label = '';
24         if (str_ends_with($key, '_asc')) {
25             $key = substr($key, 0, -4);
26             $label = trans('settings.sort_set_op_asc');
27         } elseif (str_ends_with($key, '_desc')) {
28             $key = substr($key, 0, -5);
29             $label = trans('settings.sort_set_op_desc');
30         }
31
32         $label = trans('settings.sort_set_op_' . $key) . ' ' . $label;
33         return trim($label);
34     }
35
36     /**
37      * @return SortSetOperation[]
38      */
39     public static function allExcluding(array $operations): array
40     {
41         $all = SortSetOperation::cases();
42         $filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
43             return !in_array($operation, $operations);
44         });
45         return array_values($filtered);
46     }
47
48     /**
49      * Create a set of operations from a string sequence representation.
50      * (values seperated by commas).
51      * @return SortSetOperation[]
52      */
53     public static function fromSequence(string $sequence): array
54     {
55         $strOptions = explode(',', $sequence);
56         $options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
57         return array_filter($options);
58     }
59 }