3 namespace BookStack\Sorting;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Entity;
10 * Sort comparison function for each of the possible SortSetOperation values.
11 * Method names should be camelCase names for the SortSetOperation enum value.
13 class SortSetOperationComparisons
15 public static function nameAsc(Entity $a, Entity $b): int
17 return strtolower(ASCII::to_transliterate($a->name, null)) <=> strtolower(ASCII::to_transliterate($b->name, null));
20 public static function nameDesc(Entity $a, Entity $b): int
22 return strtolower(ASCII::to_transliterate($b->name, null)) <=> strtolower(ASCII::to_transliterate($a->name, null));
25 public static function nameNumericAsc(Entity $a, Entity $b): int
27 $numRegex = '/^\d+(\.\d+)?/';
30 preg_match($numRegex, $a->name, $aMatches);
31 preg_match($numRegex, $b->name, $bMatches);
32 $aVal = floatval(($aMatches[0] ?? 0));
33 $bVal = floatval(($bMatches[0] ?? 0));
35 return $aVal <=> $bVal;
38 public static function nameNumericDesc(Entity $a, Entity $b): int
40 return -(static::nameNumericAsc($a, $b));
43 public static function createdDateAsc(Entity $a, Entity $b): int
45 return $a->created_at->unix() <=> $b->created_at->unix();
48 public static function createdDateDesc(Entity $a, Entity $b): int
50 return $b->created_at->unix() <=> $a->created_at->unix();
53 public static function updatedDateAsc(Entity $a, Entity $b): int
55 return $a->updated_at->unix() <=> $b->updated_at->unix();
58 public static function updatedDateDesc(Entity $a, Entity $b): int
60 return $b->updated_at->unix() <=> $a->updated_at->unix();
63 public static function chaptersFirst(Entity $a, Entity $b): int
65 return ($b instanceof Chapter ? 1 : 0) - (($a instanceof Chapter) ? 1 : 0);
68 public static function chaptersLast(Entity $a, Entity $b): int
70 return ($a instanceof Chapter ? 1 : 0) - (($b instanceof Chapter) ? 1 : 0);