|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +# PHP Comparable Interface & Convenience Trait |
| 6 | + |
| 7 | +A PHP library to allow for comparison of two objects with a comparison operator. |
| 8 | + |
| 9 | +This library allows you to compare two objects using a value within the objects rather than the entirety of the object (the default for PHP). |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +This library is available on Packagist using Composer. Run this command within your composer project directory: |
| 14 | + |
| 15 | +```bash |
| 16 | +$ composer require jimbo2150/php-comparable |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage (private value with convenience trait) |
| 20 | + |
| 21 | +The privately comparable interface requires a protected method, `getComparableValue()`, which does not publically expose the value to be compared: |
| 22 | + |
| 23 | +```php |
| 24 | +use Jimbo2150\PhpComparable\Interface\PrivatelyComparable; |
| 25 | +use Jimbo2150\PhpComparable\Trait\PrivatelyComparableTrait; |
| 26 | +use Jimbo2150\PhpComparable\Enum\Operator; |
| 27 | + |
| 28 | +class Person implements PrivatelyComparable { |
| 29 | + use PrivatelyComparableTrait; |
| 30 | + |
| 31 | + public function __construct(private string $name, private int $age) |
| 32 | + { |
| 33 | + assert($age > 0); |
| 34 | + } |
| 35 | + |
| 36 | + public function getComparableValue(): mixed |
| 37 | + { |
| 38 | + return $this->age; |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +$john = new Person('John', 29); |
| 44 | +$karen = new Person('Karen', 24); |
| 45 | + |
| 46 | +$john->compareTo($karen); // False, compares by equals (==) by default |
| 47 | +$karen->compareTo($john, Operator::from('<')); // True, comparing with less than |
| 48 | +``` |
| 49 | + |
| 50 | +## Usage (publicly-visible value with convenience trait) |
| 51 | + |
| 52 | +The publically comparable interface requires a public method `getComparableValue()` which exposes the value to be compared: |
| 53 | + |
| 54 | +```php |
| 55 | +use Jimbo2150\PhpComparable\Interface\PublicallyComparable; |
| 56 | +use Jimbo2150\PhpComparable\Trait\PublicallyComparableTrait; |
| 57 | + |
| 58 | +class GreenFood implements PublicallyComparable { |
| 59 | + use PublicallyComparableTrait; |
| 60 | + |
| 61 | + /** The value to compare */ |
| 62 | + private string $food = 'salad'; |
| 63 | + |
| 64 | + public function __construct(private string $type) |
| 65 | + { |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + public function getComparableValue(): mixed |
| 70 | + { |
| 71 | + return [$this->food, $this->type]; |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +class WheatFood implements PublicallyComparable { |
| 77 | + use PublicallyComparableTrait; |
| 78 | + |
| 79 | + /** The value to compare */ |
| 80 | + private $food = 'pasta'; |
| 81 | + |
| 82 | + public function __construct(private string $type) |
| 83 | + { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + public function getComparableValue(): mixed |
| 88 | + { |
| 89 | + return [$this->food, $this->type]; |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +$cobbSalad = new GreenFood('cobb'); |
| 95 | +$chefSalad = new GreenFood('chef'); |
| 96 | +$pasta = new WheatFood('spaghetti'); |
| 97 | +$pasta2 = new WheatFood('spaghetti'); |
| 98 | + |
| 99 | +$cobbSalad->compareTo($chefSalad); // false |
| 100 | +$pasta->compareTo($pasta2); // true |
| 101 | +``` |
| 102 | + |
| 103 | +## Usage (private value with custom comparison) |
| 104 | + |
| 105 | +You can also implement your own custom compare function by implementing both `compareTo()` and `compareFrom()` methods: |
| 106 | + |
| 107 | +```php |
| 108 | +use Jimbo2150\PhpComparable\Interface\PrivatelyComparable; |
| 109 | +use Jimbo2150\PhpComparable\Trait\PrivatelyComparableTrait; |
| 110 | + |
| 111 | +class Score implements PrivatelyComparable |
| 112 | +{ |
| 113 | + use PrivatelyComparableTrait; |
| 114 | + |
| 115 | + public const MIN_REQUIRED_SCORE = 1; |
| 116 | + |
| 117 | + public function __construct(private float $score) |
| 118 | + { |
| 119 | + } |
| 120 | + |
| 121 | + public function compareDiff(PrivatelyComparable|PublicallyComparable $other): bool|int |
| 122 | + { |
| 123 | + $leftValue = $this->getComparableValue(); |
| 124 | + $operator = Operator::GREATER_THAN_OR_EQUAL; |
| 125 | + $callback = fn ( |
| 126 | + mixed $rightValue, |
| 127 | + Operator $operator, |
| 128 | + ) => $operator->compare( |
| 129 | + $leftValue - $rightValue, |
| 130 | + self::MIN_REQUIRED_SCORE |
| 131 | + ); |
| 132 | + |
| 133 | + if ($other instanceof PublicallyComparable) { |
| 134 | + return $callback($other->getComparableValue(), $operator); |
| 135 | + } |
| 136 | + |
| 137 | + return $other->compareFrom($callback, $operator); |
| 138 | + } |
| 139 | + |
| 140 | + public function getComparableValue(): mixed |
| 141 | + { |
| 142 | + return $this->score; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +$score1 = new Score(2.4); |
| 147 | +$score2 = new Score(0.7); |
| 148 | +$score3 = new Score(0.2); |
| 149 | + |
| 150 | +$score1->compareDiff($score2); // True, score is >= 1 |
| 151 | +$score2->compareDiff($score3); // False, score is less than 1 |
| 152 | +``` |
0 commit comments