3 namespace BookStack\Sorting;
6 use Illuminate\Support\Str;
8 enum SortSetOperation: string
10 case NameAsc = 'name_asc';
11 case NameDesc = 'name_desc';
12 case NameNumericAsc = 'name_numeric_asc';
13 case CreatedDateAsc = 'created_date_asc';
14 case CreatedDateDesc = 'created_date_desc';
15 case UpdateDateAsc = 'updated_date_asc';
16 case UpdateDateDesc = 'updated_date_desc';
17 case ChaptersFirst = 'chapters_first';
18 case ChaptersLast = 'chapters_last';
21 * Provide a translated label string for this option.
23 public function getLabel(): string
27 if (str_ends_with($key, '_asc')) {
28 $key = substr($key, 0, -4);
29 $label = trans('settings.sort_set_op_asc');
30 } elseif (str_ends_with($key, '_desc')) {
31 $key = substr($key, 0, -5);
32 $label = trans('settings.sort_set_op_desc');
35 $label = trans('settings.sort_set_op_' . $key) . ' ' . $label;
39 public function getSortFunction(): callable
41 $camelValue = Str::camel($this->value);
42 return SortSetOperationComparisons::$camelValue(...);
46 * @return SortSetOperation[]
48 public static function allExcluding(array $operations): array
50 $all = SortSetOperation::cases();
51 $filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
52 return !in_array($operation, $operations);
54 return array_values($filtered);
58 * Create a set of operations from a string sequence representation.
59 * (values seperated by commas).
60 * @return SortSetOperation[]
62 public static function fromSequence(string $sequence): array
64 $strOptions = explode(',', $sequence);
65 $options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
66 return array_filter($options);