Skip to content

Commit 29cb02d

Browse files
authored
Allow to correctly compare other values (#89)
1 parent b67305e commit 29cb02d

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

src/Value.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Munus\Collection\Traversable;
1111
use Munus\Control\Option;
1212
use Munus\Control\TryTo;
13+
use Munus\Value\Comparable;
1314
use Munus\Value\Comparator;
1415

1516
/**
@@ -21,7 +22,7 @@
2122
*
2223
* @template T
2324
*/
24-
abstract class Value
25+
abstract class Value implements Comparable
2526
{
2627
/**
2728
* Checks, if the underlying value is absent.

src/Value/Comparable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Munus\Value;
6+
7+
interface Comparable
8+
{
9+
public function equals(self $other): bool;
10+
}

src/Value/Comparator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Munus\Value;
66

77
use Munus\Tuple;
8-
use Munus\Value;
98

109
final class Comparator
1110
{
@@ -15,11 +14,11 @@ final class Comparator
1514
*/
1615
public static function equals($a, $b): bool
1716
{
18-
if ($a instanceof Value) {
17+
if ($a instanceof Comparable) {
1918
return $a->equals($b);
2019
}
2120

22-
if ($b instanceof Value) {
21+
if ($b instanceof Comparable) {
2322
return $b->equals($a);
2423
}
2524

tests/Stub/Event.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Munus\Tests\Stub;
6+
7+
use Munus\Value\Comparable;
8+
9+
final class Event implements Comparable
10+
{
11+
public function __construct(public string $id, public string $name)
12+
{
13+
}
14+
15+
public function equals(Comparable $other): bool
16+
{
17+
return self::class === $other::class && $this->name === $other->name;
18+
}
19+
}

tests/Value/ComparatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Munus\Tests\Value;
66

77
use Munus\Collection\GenericList;
8+
use Munus\Tests\Stub\Event;
89
use Munus\Value\Comparator;
910
use PHPUnit\Framework\TestCase;
1011

@@ -29,4 +30,14 @@ public function testCompareObjects(): void
2930
self::assertTrue(Comparator::equals($object3, $object1->append(2)));
3031
self::assertFalse(Comparator::equals($object1, $object3));
3132
}
33+
34+
public function testCompareComparableObject(): void
35+
{
36+
$event = new Event('123', 'event');
37+
38+
self::assertTrue(Comparator::equals($event, new Event('456', 'event')));
39+
self::assertTrue(Comparator::equals($event, new Event('789', 'event')));
40+
self::assertFalse(Comparator::equals($event, new Event('123', 'event1')));
41+
self::assertFalse(Comparator::equals($event, new Event('456', 'event1')));
42+
}
3243
}

0 commit comments

Comments
 (0)