| 
									
										
										
										
											2025-01-30 01:34:07 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Sorting; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  | use Closure; | 
					
						
							|  |  |  | use Illuminate\Support\Str; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  | enum SortRuleOperation: string | 
					
						
							| 
									
										
										
										
											2025-01-30 01:34:07 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     case NameAsc = 'name_asc'; | 
					
						
							|  |  |  |     case NameDesc = 'name_desc'; | 
					
						
							|  |  |  |     case NameNumericAsc = 'name_numeric_asc'; | 
					
						
							| 
									
										
										
										
											2025-02-25 00:58:59 +08:00
										 |  |  |     case NameNumericDesc = 'name_numeric_desc'; | 
					
						
							| 
									
										
										
										
											2025-01-30 01:34:07 +08:00
										 |  |  |     case CreatedDateAsc = 'created_date_asc'; | 
					
						
							|  |  |  |     case CreatedDateDesc = 'created_date_desc'; | 
					
						
							|  |  |  |     case UpdateDateAsc = 'updated_date_asc'; | 
					
						
							|  |  |  |     case UpdateDateDesc = 'updated_date_desc'; | 
					
						
							|  |  |  |     case ChaptersFirst = 'chapters_first'; | 
					
						
							|  |  |  |     case ChaptersLast = 'chapters_last'; | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Provide a translated label string for this option. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getLabel(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $key = $this->value; | 
					
						
							|  |  |  |         $label = ''; | 
					
						
							|  |  |  |         if (str_ends_with($key, '_asc')) { | 
					
						
							|  |  |  |             $key = substr($key, 0, -4); | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |             $label = trans('settings.sort_rule_op_asc'); | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |         } elseif (str_ends_with($key, '_desc')) { | 
					
						
							|  |  |  |             $key = substr($key, 0, -5); | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |             $label = trans('settings.sort_rule_op_desc'); | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         $label = trans('settings.sort_rule_op_' . $key) . ' ' . $label; | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |         return trim($label); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  |     public function getSortFunction(): callable | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $camelValue = Str::camel($this->value); | 
					
						
							|  |  |  |         return SortSetOperationComparisons::$camelValue(...); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |      * @return SortRuleOperation[] | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-02-04 23:14:22 +08:00
										 |  |  |     public static function allExcluding(array $operations): array | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         $all = SortRuleOperation::cases(); | 
					
						
							|  |  |  |         $filtered = array_filter($all, function (SortRuleOperation $operation) use ($operations) { | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  |             return !in_array($operation, $operations); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         return array_values($filtered); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a set of operations from a string sequence representation. | 
					
						
							|  |  |  |      * (values seperated by commas). | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |      * @return SortRuleOperation[] | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function fromSequence(string $sequence): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $strOptions = explode(',', $sequence); | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         $options = array_map(fn ($val) => SortRuleOperation::tryFrom($val), $strOptions); | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  |         return array_filter($options); | 
					
						
							| 
									
										
										
										
											2025-02-04 00:48:57 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-30 01:34:07 +08:00
										 |  |  | } |