3 namespace BookStack\Sorting;
6 use Illuminate\Support\Str;
8 enum SortRuleOperation: string
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';
22 * Provide a translated label string for this option.
24 public function getLabel(): string
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');
36 $label = trans('settings.sort_rule_op_' . $key) . ' ' . $label;
40 public function getSortFunction(): callable
42 $camelValue = Str::camel($this->value);
43 return SortSetOperationComparisons::$camelValue(...);
47 * @return SortRuleOperation[]
49 public static function allExcluding(array $operations): array
51 $all = SortRuleOperation::cases();
52 $filtered = array_filter($all, function (SortRuleOperation $operation) use ($operations) {
53 return !in_array($operation, $operations);
55 return array_values($filtered);
59 * Create a set of operations from a string sequence representation.
60 * (values seperated by commas).
61 * @return SortRuleOperation[]
63 public static function fromSequence(string $sequence): array
65 $strOptions = explode(',', $sequence);
66 $options = array_map(fn ($val) => SortRuleOperation::tryFrom($val), $strOptions);
67 return array_filter($options);