Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ComparisonResult | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
14 | |
100.00% |
1 / 1 |
| fromComparisonResult | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| toComparisonResult | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| reverse | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| isComparable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Ergonomic utilities for PHP enums, including names, values, lookups, and option maps. |
| 7 | * |
| 8 | * This file is part of fast-forward/enum project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/enum |
| 14 | * @see https://github.com/php-fast-forward/enum/issues |
| 15 | * @see https://php-fast-forward.github.io/enum/ |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Enum\Sort; |
| 20 | |
| 21 | use FastForward\Enum\ReversibleInterface; |
| 22 | use FastForward\Enum\Trait\Comparable; |
| 23 | use FastForward\Enum\Trait\HasNameLookup; |
| 24 | use FastForward\Enum\Trait\HasNameMap; |
| 25 | use FastForward\Enum\Trait\HasNames; |
| 26 | |
| 27 | enum ComparisonResult implements ReversibleInterface |
| 28 | { |
| 29 | use Comparable; |
| 30 | use HasNameLookup; |
| 31 | use HasNameMap; |
| 32 | use HasNames; |
| 33 | |
| 34 | case LeftGreater; |
| 35 | case RightGreater; |
| 36 | case Equal; |
| 37 | case Incomparable; |
| 38 | |
| 39 | /** |
| 40 | * @param int $result |
| 41 | * |
| 42 | * @return self |
| 43 | */ |
| 44 | public static function fromComparisonResult(int $result): self |
| 45 | { |
| 46 | return match (true) { |
| 47 | $result > 0 => self::LeftGreater, |
| 48 | $result < 0 => self::RightGreater, |
| 49 | default => self::Equal, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return int |
| 55 | */ |
| 56 | public function toComparisonResult(): int |
| 57 | { |
| 58 | return match ($this) { |
| 59 | self::LeftGreater => 1, |
| 60 | self::RightGreater => -1, |
| 61 | self::Equal => 0, |
| 62 | // Mirrors PHP's legacy comparator fallback for values that are not logically comparable. |
| 63 | self::Incomparable => 1, |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return static |
| 69 | */ |
| 70 | public function reverse(): static |
| 71 | { |
| 72 | return match ($this) { |
| 73 | self::LeftGreater => self::RightGreater, |
| 74 | self::RightGreater => self::LeftGreater, |
| 75 | default => $this, |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return bool |
| 81 | */ |
| 82 | public function isComparable(): bool |
| 83 | { |
| 84 | return ! $this->is(self::Incomparable); |
| 85 | } |
| 86 | } |