| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Sorting; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Sort comparison function for each of the possible SortSetOperation values. | 
					
						
							|  |  |  |  * Method names should be camelCase names for the SortSetOperation enum value. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class SortSetOperationComparisons | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public static function nameAsc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-26 22:19:03 +08:00
										 |  |  |         return strtolower($a->name) <=> strtolower($b->name); | 
					
						
							| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function nameDesc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-26 22:19:03 +08:00
										 |  |  |         return strtolower($b->name) <=> strtolower($a->name); | 
					
						
							| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function nameNumericAsc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $numRegex = '/^\d+(\.\d+)?/'; | 
					
						
							|  |  |  |         $aMatches = []; | 
					
						
							|  |  |  |         $bMatches = []; | 
					
						
							| 
									
										
										
										
											2025-02-25 00:58:59 +08:00
										 |  |  |         preg_match($numRegex, $a->name, $aMatches); | 
					
						
							|  |  |  |         preg_match($numRegex, $b->name, $bMatches); | 
					
						
							|  |  |  |         $aVal = floatval(($aMatches[0] ?? 0)); | 
					
						
							|  |  |  |         $bVal = floatval(($bMatches[0] ?? 0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $aVal <=> $bVal; | 
					
						
							| 
									
										
										
										
											2025-02-06 00:52:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function nameNumericDesc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return -(static::nameNumericAsc($a, $b)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function createdDateAsc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $a->created_at->unix() <=> $b->created_at->unix(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function createdDateDesc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $b->created_at->unix() <=> $a->created_at->unix(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function updatedDateAsc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $a->updated_at->unix() <=> $b->updated_at->unix(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function updatedDateDesc(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $b->updated_at->unix() <=> $a->updated_at->unix(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function chaptersFirst(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return ($b instanceof Chapter ? 1 : 0) - (($a instanceof Chapter) ? 1 : 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function chaptersLast(Entity $a, Entity $b): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return ($a instanceof Chapter ? 1 : 0) - (($b instanceof Chapter) ? 1 : 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |