Skip to content

Commit 39d12ec

Browse files
committed
- Completed updates
- Added tests
1 parent 2609c19 commit 39d12ec

File tree

7 files changed

+488
-1
lines changed

7 files changed

+488
-1
lines changed

src/Enum/Operator.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,59 @@ enum Operator: string
1616
case LESS_THAN_OR_EQUAL = '<=';
1717
case GREATER_THAN_OR_EQUAL = '>=';
1818
case SPACESHIP = '<=>';
19+
20+
public function compare(mixed $left, mixed $right): bool|int
21+
{
22+
return self::{'compare_'.$this->name}($left, $right);
23+
}
24+
25+
private static function compare_EQUAL(mixed $left, mixed $right): bool
26+
{
27+
return $left == $right;
28+
}
29+
30+
private static function compare_IDENTICAL(mixed $left, mixed $right): bool
31+
{
32+
return $left === $right;
33+
}
34+
35+
private static function compare_NOT_EQUAL(mixed $left, mixed $right): bool
36+
{
37+
return $left != $right;
38+
}
39+
40+
private static function compare_NOT_EQUAL_DIAMOND(mixed $left, mixed $right): bool
41+
{
42+
return self::compare_NOT_EQUAL($left, $right);
43+
}
44+
45+
private static function compare_NOT_IDENTICAL(mixed $left, mixed $right): bool
46+
{
47+
return $left !== $right;
48+
}
49+
50+
private static function compare_LESS_THAN(mixed $left, mixed $right): bool
51+
{
52+
return $left < $right;
53+
}
54+
55+
private static function compare_GREATER_THAN(mixed $left, mixed $right): bool
56+
{
57+
return $left > $right;
58+
}
59+
60+
private static function compare_LESS_THAN_OR_EQUAL(mixed $left, mixed $right): bool
61+
{
62+
return $left <= $right;
63+
}
64+
65+
private static function compare_GREATER_THAN_OR_EQUAL(mixed $left, mixed $right): bool
66+
{
67+
return $left >= $right;
68+
}
69+
70+
private static function compare_SPACESHIP(mixed $left, mixed $right): int
71+
{
72+
return $left <=> $right;
73+
}
1974
}

src/Interface/Comparable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88

99
interface Comparable
1010
{
11-
public function compareTo(self $other, Operator $operator = Operator::EQUAL);
11+
public function compareTo(Comparable $other, Operator $operator = Operator::EQUAL): bool|int;
12+
13+
public function compareFrom(mixed $leftValue, Operator $operator = Operator::EQUAL): bool|int;
1214
}

src/Trait/ComparableTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jimbo2150\PhpComparable\Trait;
6+
7+
use Jimbo2150\PhpComparable\Enum\Operator;
8+
use Jimbo2150\PhpComparable\Interface\Comparable;
9+
10+
trait ComparableTrait
11+
{
12+
public function compareTo(Comparable $other, Operator $operator = Operator::EQUAL): bool|int
13+
{
14+
return $other->compareFrom($this->getComparableValue(), $operator);
15+
}
16+
17+
public function compareFrom(mixed $leftValue, Operator $operator = Operator::EQUAL): bool|int
18+
{
19+
return $operator->compare($leftValue, $this->getComparableValue());
20+
}
21+
22+
abstract protected function getComparableValue(): mixed;
23+
}

tests/Mocks/Comparable/From.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jimbo2150\PhpComparable\Tests\Mocks\Comparable;
6+
7+
use Jimbo2150\PhpComparable\Interface\Comparable;
8+
use Jimbo2150\PhpComparable\Trait\ComparableTrait;
9+
10+
final class From implements Comparable
11+
{
12+
use ComparableTrait;
13+
14+
public function __construct(private mixed $value)
15+
{
16+
}
17+
18+
protected function getComparableValue(): mixed
19+
{
20+
return $this->value;
21+
}
22+
}

tests/Mocks/Comparable/To.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jimbo2150\PhpComparable\Tests\Mocks\Comparable;
6+
7+
use Jimbo2150\PhpComparable\Interface\Comparable;
8+
use Jimbo2150\PhpComparable\Trait\ComparableTrait;
9+
10+
final class To implements Comparable
11+
{
12+
use ComparableTrait;
13+
14+
public function __construct(private mixed $value)
15+
{
16+
}
17+
18+
protected function getComparableValue(): mixed
19+
{
20+
return $this->value;
21+
}
22+
}

0 commit comments

Comments
 (0)