3 namespace BookStack\Sorting;
5 enum SortSetOperation: string
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';
18 * Provide a translated label string for this option.
20 public function getLabel(): string
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');
32 $label = trans('settings.sort_set_op_' . $key) . ' ' . $label;
37 * @return SortSetOperation[]
39 public static function allExcluding(array $operations): array
41 $all = SortSetOperation::cases();
42 $filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
43 return !in_array($operation, $operations);
45 return array_values($filtered);
49 * Create a set of operations from a string sequence representation.
50 * (values seperated by commas).
51 * @return SortSetOperation[]
53 public static function fromSequence(string $sequence): array
55 $strOptions = explode(',', $sequence);
56 $options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
57 return array_filter($options);