3 namespace BookStack\Sorting;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Entity;
9 * Sort comparison function for each of the possible SortSetOperation values.
10 * Method names should be camelCase names for the SortSetOperation enum value.
12 class SortSetOperationComparisons
14 public static function nameAsc(Entity $a, Entity $b): int
16 return $a->name <=> $b->name;
19 public static function nameDesc(Entity $a, Entity $b): int
21 return $b->name <=> $a->name;
24 public static function nameNumericAsc(Entity $a, Entity $b): int
26 $numRegex = '/^\d+(\.\d+)?/';
29 preg_match($numRegex, $a->name, $aMatches);
30 preg_match($numRegex, $b->name, $bMatches);
31 $aVal = floatval(($aMatches[0] ?? 0));
32 $bVal = floatval(($bMatches[0] ?? 0));
34 return $aVal <=> $bVal;
37 public static function nameNumericDesc(Entity $a, Entity $b): int
39 return -(static::nameNumericAsc($a, $b));
42 public static function createdDateAsc(Entity $a, Entity $b): int
44 return $a->created_at->unix() <=> $b->created_at->unix();
47 public static function createdDateDesc(Entity $a, Entity $b): int
49 return $b->created_at->unix() <=> $a->created_at->unix();
52 public static function updatedDateAsc(Entity $a, Entity $b): int
54 return $a->updated_at->unix() <=> $b->updated_at->unix();
57 public static function updatedDateDesc(Entity $a, Entity $b): int
59 return $b->updated_at->unix() <=> $a->updated_at->unix();
62 public static function chaptersFirst(Entity $a, Entity $b): int
64 return ($b instanceof Chapter ? 1 : 0) - (($a instanceof Chapter) ? 1 : 0);
67 public static function chaptersLast(Entity $a, Entity $b): int
69 return ($a instanceof Chapter ? 1 : 0) - (($b instanceof Chapter) ? 1 : 0);