10000 add AbstractDomainId in favor of DomainIdTrait (#387) · msgphp/msgphp@3004d3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3004d3f

Browse files
authored
add AbstractDomainId in favor of DomainIdTrait (#387)
1 parent 9fd9b68 commit 3004d3f

File tree

4 files changed

+175
-3
lines changed

4 files changed

+175
-3
lines changed

docs/ddd/collections.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Get the total no. of items in the full result set.
8787

8888
### `MsgPhp\Domain\GenericDomainCollection`
8989

90-
A first-class citizen domain collection compatible with any `iterable` value.
90+
A generic collection compatible with any `iterable` value.
9191

9292
#### Basic Example
9393

@@ -131,7 +131,7 @@ $firstTwoIntsPlussed = $firstTwoInts->map(function (int $value): int {
131131

132132
### `MsgPhp\Domain\GenericPaginatedDomainCollection`
133133

134-
A first-class citizen paginated domain collection compatible with any `iterable` value.
134+
A generic paginated collection compatible with any `iterable` value.
135135

136136
### Infrastructural
137137

docs/ddd/identifiers.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Returns the identifier its primitive string value.
2626

2727
### `MsgPhp\Domain\GenericDomainId`
2828

29-
A first-class citizen domain identifier compatible with any string or numeric value.
29+
A generic identifier compatible with any `string` or `numeric` value.
3030

3131
#### Basic Example
3232

@@ -55,6 +55,35 @@ $id->equals(new GenericDomainId('2')); // false
5555
$id->equals(new GenericDomainId()); // false
5656
```
5757

58+
### `MsgPhp\Domain\AbstractDomainId`
59+
60+
A decorating identifier to built custom/concrete identifiers upon.
61+
62+
#### Basic Example
63+
64+
```php
65+
<?php
66+
67+
use MsgPhp\Domain\AbstractDomainId;use MsgPhp\Domain\DomainId;
68+
69+
// SETUP
70+
71+
class MyDomainId extends AbstractDomainId
72+
{
73+
}
74+
75+
class MyOtherDomainId extends AbstractDomainId
76+
{
77+
}
78+
79+
$id = MyDomainId::fromValue(1);
80+
$otherId = MyOtherDomainId::fromValue(1);
81+
82+
// USAGE
83+
84+
$id->equals($otherId); // false
85+
```
86+
5887
### Infrastructural
5988

6089
- [Unique Identifier](../infrastructure/uid.md#domain-identifier) (UUID, ULID, etc.)

src/Domain/AbstractDomainId.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Domain;
6+
7+
/**
8+
* @author Roland Franssen <franssen.roland@gmail.com>
9+
*/
10+
abstract class AbstractDomainId implements DomainId
11+
{
12+
/** @var DomainId */
13+
private $id;
14+
15+
public function __construct(?DomainId $id = null)
16+
{
17+
$this->id = $id ?? new GenericDomainId();
18+
}
19+
20+
public function __toString(): string
21+
{
22+
return $this->id->toString();
23+
}
24+
25+
public static function fromValue($value): DomainId
26+
{
27+
if ($value instanceof static) {
28+
return $value;
29+
}
30+
if (null === $value || $value instanceof DomainId) {
31+
return new static($value);
32+
}
33+
34+
return new static(GenericDomainId::fromValue($value));
35+
}
36+
37+
public function isNil(): bool
38+
{
39+
return $this->id->isNil();
40+
}
41+
42+
public function equals($other): bool
43+
{
44+
return $other instanceof static && $this->id->equals($other->id);
45+
}
46+
47+
public function toString(): string
48+
{
49+
return $this->id->toString();
50+
}
51+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Domain\Tests;
6+
7+
use MsgPhp\Domain\AbstractDomainId;
8+
use MsgPhp\Domain\DomainId;
9+
use MsgPhp\Domain\GenericDomainId;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class AbstractDomainIdTest extends TestCase
16+
{
17+
public function testFromValue(): void
18+
{
19+
self::assertInstanceOf(TestAbstractDomainId::class, TestAbstractDomainId::fromValue(null));
20+
self::assertInstanceOf(TestSubAbstractDomainId::class, TestSubAbstractDomainId::fromValue(null));
21+
self::assertInstanceOf(TestAbstractDomainId::class, TestAbstractDomainId::fromValue($this->createMock(DomainId::class)));
22+
self::assertInstanceOf(TestSubAbstractDomainId::class, TestAbstractDomainId::fromValue(new TestSubAbstractDomainId()));
23+
self::assertInstanceOf(TestAbstractDomainId::class, TestAbstractDomainId::fromValue(new TestOtherAbstractDomainId()));
24+
self::assertSame($id = TestAbstractDomainId::fromValue(null), TestAbstractDomainId::fromValue($id));
25+
self::assertSame((array) new TestAbstractDomainId($id = $this->createMock(DomainId::class)), (array) TestAbstractDomainId::fromValue($id));
26+
self::assertSame((new TestAbstractDomainId())->isNil(), TestAbstractDomainId::fromValue(null)->isNil());
27+
}
28+
29+
public function testFromInvalidValue(): void
30+
{
31+
$this->expectException(\LogicException::class);
32+
33+
TestAbstractDomainId::fromValue(true);
34+
}
35+
36+
public function testIsNil(): void
37+
{
38+
$id = $this->createMock(DomainId::class);
39+
$id->expects(self::once())
40+
->method('isNil')
41+
->with()
42+
->willReturn(true)
43+
;
44+
45+
self::assertTrue((new TestAbstractDomainId($id))->isNil());
46+
}
47+
48+
public function testEquals(): void
49+
{
50+
$id = $this->createMock(DomainId::class);
51+
$id->expects(self::exactly(2))
52+
->method('equals')
53+
->with($id)
54+
->willReturn(true)
55+
;
56+
57+
self::assertFalse((new TestAbstractDomainId($id))->equals(''));
58+
self::assertFalse((new TestAbstractDomainId($id))->equals($id));
59+
self::assertTrue((new TestAbstractDomainId($id))->equals(new TestAbstractDomainId($id)));
60+
self::assertTrue((new TestAbstractDomainId($id))->equals(new TestSubAbstractDomainId($id)));
61+
self::assertFalse((new TestAbstractDomainId($id))->equals(new TestOtherAbstractDomainId($id)));
62+
}
63+
64+
public function testToString(): void
65+
{
66+
$id = $this->createMock(DomainId::class);
67+
$id->expects(self::once())
68+
->method('toString')
69+
->with()
70+
->willReturn('foo')
71+
;
72+
73+
self::assertSame('foo', (new TestAbstractDomainId($id))->toString());
74+
}
75+
76+
public function testSerialize(): void
77+
{
78+
self::assertSame('foo', (string) unserialize(serialize(new TestAbstractDomainId(new GenericDomainId('foo')))));
79+
}
80+
}
81+
82+
class TestAbstractDomainId extends AbstractDomainId
83+
{
84+
}
85+
86+
class TestSubAbstractDomainId extends TestAbstractDomainId
87+
{
88+
}
89+
90+
class TestOtherAbstractDomainId extends AbstractDomainId
91+
{
92+
}

0 commit comments

Comments
 (0)
0