]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortRuleOperation.php
Deps: Updated composer/npm packages, fixed test namespace
[bookstack] / app / Sorting / SortRuleOperation.php
1 <?php
2
3 namespace BookStack\Sorting;
4
5 use Closure;
6 use Illuminate\Support\Str;
7
8 enum SortRuleOperation: string
9 {
10     case NameAsc = 'name_asc';
11     case NameDesc = 'name_desc';
12     case NameNumericAsc = 'name_numeric_asc';
13     case NameNumericDesc = 'name_numeric_desc';
14     case CreatedDateAsc = 'created_date_asc';
15     case CreatedDateDesc = 'created_date_desc';
16     case UpdateDateAsc = 'updated_date_asc';
17     case UpdateDateDesc = 'updated_date_desc';
18     case ChaptersFirst = 'chapters_first';
19     case ChaptersLast = 'chapters_last';
20
21     /**
22      * Provide a translated label string for this option.
23      */
24     public function getLabel(): string
25     {
26         $key = $this->value;
27         $label = '';
28         if (str_ends_with($key, '_asc')) {
29             $key = substr($key, 0, -4);
30             $label = trans('settings.sort_rule_op_asc');
31         } elseif (str_ends_with($key, '_desc')) {
32             $key = substr($key, 0, -5);
33             $label = trans('settings.sort_rule_op_desc');
34         }
35
36         $label = trans('settings.sort_rule_op_' . $key) . ' ' . $label;
37         return trim($label);
38     }
39
40     public function getSortFunction(): callable
41     {
42         $camelValue = Str::camel($this->value);
43         return SortSetOperationComparisons::$camelValue(...);
44     }
45
46     /**
47      * @return SortRuleOperation[]
48      */
49     public static function allExcluding(array $operations): array
50     {
51         $all = SortRuleOperation::cases();
52         $filtered = array_filter($all, function (SortRuleOperation $operation) use ($operations) {
53             return !in_array($operation, $operations);
54         });
55         return array_values($filtered);
56     }
57
58     /**
59      * Create a set of operations from a string sequence representation.
60      * (values seperated by commas).
61      * @return SortRuleOperation[]
62      */
63     public static function fromSequence(string $sequence): array
64     {
65         $strOptions = explode(',', $sequence);
66         $options = array_map(fn ($val) => SortRuleOperation::tryFrom($val), $strOptions);
67         return array_filter($options);
68     }
69 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.