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.
11 * TODO - Test to cover each SortSetOperation enum value is covered.
13 class SortSetOperationComparisons
15 public static function nameAsc(Entity $a, Entity $b): int
17 return $a->name <=> $b->name;
20 public static function nameDesc(Entity $a, Entity $b): int
22 return $b->name <=> $a->name;
25 public static function nameNumericAsc(Entity $a, Entity $b): int
27 $numRegex = '/^\d+(\.\d+)?/';
30 preg_match($numRegex, $a, $aMatches);
31 preg_match($numRegex, $b, $bMatches);
32 return ($aMatches[0] ?? 0) <=> ($bMatches[0] ?? 0);
35 public static function nameNumericDesc(Entity $a, Entity $b): int
37 return -(static::nameNumericAsc($a, $b));
40 public static function createdDateAsc(Entity $a, Entity $b): int
42 return $a->created_at->unix() <=> $b->created_at->unix();
45 public static function createdDateDesc(Entity $a, Entity $b): int
47 return $b->created_at->unix() <=> $a->created_at->unix();
50 public static function updatedDateAsc(Entity $a, Entity $b): int
52 return $a->updated_at->unix() <=> $b->updated_at->unix();
55 public static function updatedDateDesc(Entity $a, Entity $b): int
57 return $b->updated_at->unix() <=> $a->updated_at->unix();
60 public static function chaptersFirst(Entity $a, Entity $b): int
62 return ($b instanceof Chapter ? 1 : 0) - (($a instanceof Chapter) ? 1 : 0);
65 public static function chaptersLast(Entity $a, Entity $b): int
67 return ($a instanceof Chapter ? 1 : 0) - (($b instanceof Chapter) ? 1 : 0);