10000 rename DomainId::isEmpty() to isNil() (#384) · msgphp/msgphp@5f4d4cd · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f4d4cd

Browse files
authored
rename DomainId::isEmpty() to isNil() (#384)
1 parent c88013a commit 5f4d4cd

File tree

12 files changed

+54
-66
lines changed

12 files changed

+54
-66
lines changed

docs/ddd/identifiers.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Returns a factorized identifier from any primitive value. Using `null` might imp
1111

1212
---
1313

14-
### `isEmpty(): bool`
14+
### `isNil(): bool`
1515

16-
Tells if an identifier value is considered empty, thus has no known primitive value.
16+
Tells if an identifier value is nil, thus is considered empty/unknown.
1717

1818
---
1919

@@ -25,8 +25,7 @@ Tells if an identifier strictly equals another identifier.
2525

2626
### `toString(): string`
2727

28-
Returns the identifier its primitive string value. If the identifier is empty (see `isEmpty()`) an empty string should
29-
be returned.
28+
Returns the identifier its primitive string value.
3029

3130
## Implementations
3231

@@ -54,8 +53,8 @@ $emptyId = new MyDomainId();
5453

5554
// --- USAGE ---
5655

57-
$id->isEmpty(); // false
58-
$emptyId->isEmpty(); // true
56+
$id->isNil(); // false
57+
$emptyId->isNil(); // true
5958

6059
$id->toString(); // "1"
6160
$emptyId->toString(); // ""

src/Domain/DomainId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __toString(): string;
2121
*/
2222
public static function fromValue($value): self;
2323

24-
public function isEmpty(): bool;
24+
public function isNil(): bool;
2525

2626
/**
2727
* @param mixed $other

src/Domain/DomainIdTrait.php

Lines changed: 6 additions & 10 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@
99
*/
1010
trait DomainIdTrait
1111
{
12-
/** @var null|string */
12+
/** @var string */
1313
private $id;
1414

1515
public function __construct(?string $id = null)
1616
{
17-
if ('' === $id) {
18-
throw new \LogicException('A domain ID cannot be empty.');
19-
}
20-
21-
$this->id = $id;
17+
$this->id = $id ?? '';
2218
}
2319

2420
/**
2521
* @internal
2622
*/
2723
public function __toString(): string
2824
{
29-
return $this->id ?? '';
25+
return $this->id;
3026
}
3127

3228
/**
@@ -46,9 +42,9 @@ public static function fromValue($value): DomainId
4642
throw new \LogicException('Raw ID value must be of type string or number, got "'.\gettype($value).'".');
4743
}
4844

49-
public function isEmpty(): bool
45+
public function isNil(): bool
5046
{
51-
return null === $this->id;
47+
return '' === $this->id;
5248
}
5349

5450
/**
@@ -65,6 +61,6 @@ public function equals($other): bool
6561

6662
public function toString(): string
6763
{
68-
return $this->id ?? '';
64+
return $this->id;
6965
}
7066
}

src/Domain/Infrastructure/Doctrine/DomainIdType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ final public static function resolveValue($value, AbstractPlatform $platform)
106106
}
107107
}
108108

109-
return self::getType($type)->convertToPHPValue($value->isEmpty() ? null : $value->toString(), $platform);
109+
return self::getType($type)->convertToPHPValue($value->isNil() ? null : $value->toString(), $platform);
110110
}
111111

112112
return $value;
@@ -125,7 +125,7 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
125125
public function convertToDatabaseValue($value, AbstractPlatform $platform)
126126
{
127127
if ($value instanceof DomainId) {
128-
$value = $value->isEmpty() ? null : $value->toString();
128+
$value = $value->isNil() ? null : $value->toString();
129129
}
130130

131131
try {

src/Domain/Infrastructure/Uuid/DomainIdTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public static function fromValue($value): DomainId
4646
throw new \LogicException('Raw UUID value must be of type string, got "'.\gettype($value).'".');
4747
}
4848

49-
public function isEmpty(): bool
49+
public function isNil(): bool
5050
{
51-
return false;
51+
return $this->uuid->equals(Uuid::fromString(Uuid::NIL));
5252
}
5353

5454
/**

src/Domain/Tests/DomainEntityRepositoryTestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function testFindByFieldsWithPrimaryId(): void
195195

196196
static::flushEntities([$derivingEntity = Entities\TestDerivedEntity::create(['entity' => $entity]), $entity2]);
197197

198-
self::assertFalse($entity->getId()->isEmpty());
198+
self::assertFalse($entity->getId()->isNil());
199199
self::assertNotSame('IRRELEVANT', $entity->getId()->toString());
200200
$this->assertEntityEquals($derivingEntity, $repository->findByFields(['entity' => $entity->getId()]));
201201
}
@@ -291,13 +291,13 @@ public function testSaveUpdates(): void
291291
]);
292292

293293
if (!static::$supportsAutoGeneratedIds) {
294-
self::assertFalse($entity->getId()->isEmpty());
294+
self::assertFalse($entity->getId()->isNil());
295295
}
296296

297297
$repository->save($entity);
298298

299299
if (static::$supportsAutoGeneratedIds) {
300-
self::assertFalse($entity->getId()->isEmpty());
300+
self::assertFalse($entity->getId()->isNil());
301301
}
302302

303303
self::assertNull($entity->strField);
@@ -313,7 +313,7 @@ public function testSaveUpdates(): void
313313

314314
$fresh = $repository->find(Entities\BaseTestEntity::getPrimaryIds($entity));
315315

316-
self::assertFalse($fresh->getId()->isEmpty());
316+
self::assertFalse($fresh->getId()->isNil());
317317
self::assertSame('foo', $fresh->strField);
318318
self::assertSame(1, $fresh->intField);
319319
self::assertNull($fresh->floatField);

src/Domain/Tests/DomainIdTest.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ final class DomainIdTest extends TestCase
1616
{
1717
public function testFromValue(): void
1818
{
19+
self::assertInstanceOf(TestDomainId::class, TestDomainId::fromValue(null));
20+
self::assertInstanceOf(TestOtherDomainId::class, TestOtherDomainId::fromValue(''));
1921
self::assertSame((array) new TestDomainId(), (array) TestDomainId::fromValue(null));
2022
self::assertSame((array) new TestDomainId(null), (array) TestDomainId::fromValue(null));
23+
self::assertSame((array) new TestDomainId(''), (array) TestDomainId::fromValue(null));
2124
self::assertSame((array) new TestDomainId('foo'), (array) TestDomainId::fromValue('foo'));
2225
self::assertSame((array) new TestDomainId('1'), (array) TestDomainId::fromValue(1));
2326
self::assertSame((array) new TestDomainId(' '), (array) TestDomainId::fromValue(' '));
24-
self::assertNotSame(TestDomainId::fromValue('1'), TestDomainId::fromValue('1'));
25-
self::assertInstanceOf(TestOtherDomainId::class, TestOtherDomainId::fromValue(null));
2627
}
2728

2829
public function testFromInvalidValue(): void
@@ -32,37 +33,31 @@ public function testFromInvalidValue(): void
3233
TestDomainId::fromValue(true);
3334
}
3435

35-
public function testEmptyIdValue(): void
36+
public function testIsNil(): void
3637
{
37-
$this->expectException(\LogicException::class);
38-
39-
new TestDomainId('');
40-
}
41-
42-
public function testIsEmpty(): void
43-
{
44-
self::assertTrue((new TestDomainId())->isEmpty());
45-
self::assertTrue((new TestDomainId(null))->isEmpty());
46-
self::assertFalse((new TestDomainId('foo'))->isEmpty());
47-
self::assertFalse((new TestDomainId(' '))->isEmpty());
38+
self::assertTrue((new TestDomainId())->isNil());
39+
self::assertTrue((new TestDomainId(null))->isNil());
40+
self::assertTrue((new TestDomainId(''))->isNil());
41+
self::assertFalse((new TestDomainId(' '))->isNil());
42+
self::assertFalse((new TestDomainId('foo'))->isNil());
4843
}
4944

5045
public function testEquals(): void
5146
{
5247
$id = new TestDomainId('foo');
53-
$emptyId = new TestDomainId();
48+
$nilId = new TestDomainId();
5449

5550
self::assertTrue($id->equals($id));
5651
self::assertTrue($id->equals(new TestDomainId('foo')));
57-
self::assertFalse($id->equals($emptyId));
52+
self::assertFalse($id->equals($nilId));
5853
self::assertFalse($id->equals(new TestOtherDomainId('foo')));
5954
self::assertFalse($id->equals('foo'));
6055
self::assertFalse($id->equals(new \stdClass()));
61-
self::assertTrue($emptyId->equals($emptyId));
62-
self::assertTrue($emptyId->equals(new TestDomainId()));
63-
self::assertFalse($emptyId->equals(new TestOtherDomainId()));
64-
self::assertFalse($emptyId->equals(''));
65-
self::assertFalse($emptyId->equals(new \stdClass()));
56+
self::assertTrue($nilId->equals($nilId));
57+
self::assertTrue($nilId->equals(new TestDomainId()));
58+
self::assertFalse($nilId->equals(new TestOtherDomainId()));
59+
self::assertFalse($nilId->equals(''));
60+
self::assertFalse($nilId->equals(new \stdClass()));
6661
}
6762

6863
/**

src/Domain/Tests/Fixtures/Entities/BaseTestEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final public static function getPrimaryIds(self $entity, array &$primitives = []
3737
$ids[$field] = $id = method_exists($entity, $method = 'get'.ucfirst($field)) ? $entity->{$method}() : $entity->{$field};
3838

3939
if ($id instanceof DomainId) {
40-
$primitives[$field] = $id->isEmpty() ? null : $id->toString();
40+
$primitives[$field] = $id->isNil() ? null : $id->toString();
4141
} elseif ($id instanceof self) {
4242
$nestedPrimitiveIds = [];
4343
self::getPrimaryIds($id, $nestedPrimitiveIds);

src/Domain/Tests/Infrastructure/Uuid/DomainIdTest.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use MsgPhp\Domain\Tests\Fixtures\TestOtherDomainUuid;
99
use PHPUnit\Framework\TestCase;
1010
use Ramsey\Uuid\Uuid;
11-
use Ramsey\Uuid\UuidInterface;
1211

1312
/**
1413
* @internal
@@ -17,22 +16,21 @@ final class DomainIdTest extends TestCase
1716
{
1817
public function testFromValue(): void
1918
{
20-
$uuid = Uuid::fromString('00000000-0000-0000-0000-000000000000');
19+
$uuid = Uuid::fromString(Uuid::NIL);
2120

2221
self::assertInstanceOf(TestDomainUuid::class, TestDomainUuid::fromValue(null));
23-
self::assertInstanceOf(TestDomainUuid::class, TestDomainUuid::fromValue('00000000-0000-0000-0000-000000000000'));
22+
self::assertInstanceOf(TestOtherDomainUuid::class, TestOtherDomainUuid::fromValue(Uuid::NIL));
23+
self::assertSame((array) new TestDomainUuid($uuid), (array) TestDomainUuid::fromValue($uuid));
2424
self::assertNotSame((array) new TestDomainUuid(), (array) TestDomainUuid::fromValue(null));
2525
self::assertNotSame((array) new TestDomainUuid(null), (array) TestDomainUuid::fromValue(null));
2626
self::assertNotSame((array) new TestDomainUuid($uuid), (array) TestDomainUuid::fromValue(null));
27-
self::assertSame((array) new TestDomainUuid($uuid), (array) TestDomainUuid::fromValue($uuid));
28-
self::assertInstanceOf(TestOtherDomainUuid::class, TestOtherDomainUuid::fromValue(null));
2927
}
3028

3129
public function testFromValueWithInvalidUuid(): void
3230
{
3331
$this->expectException(\LogicException::class);
3432

35-
TestDomainUuid::fromValue('00000000-0000-0000-0000-00000000000');
33+
TestDomainUuid::fromValue('foo');
3634
}
3735

3836
public function testFromInvalidValue(): void
@@ -42,21 +40,21 @@ public function testFromInvalidValue(): void
4240
TestDomainUuid::fromValue(true);
4341
}
4442

45-
public function testIsEmpty(): void
43+
public function testIsNil(): void
4644
{
47-
self::assertFalse((new TestDomainUuid())->isEmpty());
48-
self::assertFalse((new TestDomainUuid($this->createMock(UuidInterface::class)))->isEmpty());
45+
self::assertFalse((new TestDomainUuid())->isNil());
46+
self::assertTrue((new TestDomainUuid(Uuid::fromString(Uuid::NIL)))->isNil());
4947
}
5048

5149
public function testEquals(): void
5250
{
53-
$id = new TestDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'));
51+
$id = new TestDomainUuid(Uuid::fromString(Uuid::NIL));
5452

5553
self::assertTrue($id->equals($id));
56-
self::assertTrue($id->equals(new TestDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
54+
self::assertTrue($id->equals(new TestDomainUuid(Uuid::fromString(Uuid::NIL))));
5755
self::assertFalse($id->equals(new TestDomainUuid()));
58-
self::assertFalse($id->equals(new TestOtherDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
59-
self::assertFalse($id->equals('00000000-0000-0000-0000-000000000000'));
56+
self::assertFalse($id->equals(new TestOtherDomainUuid(Uuid::fromString(Uuid::NIL))));
57+
self::assertFalse($id->equals(Uuid::NIL));
6058
self::assertFalse($id->equals(new \stdClass()));
6159
}
6260

src/Eav/Tests/Command/CreateAttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testCreate(): void
2828

2929
self::assertSame('bar', $event->context['foo'] ?? null);
3030
self::assertInstanceOf(AttributeId::class, $event->context['id'] ?? null);
31-
self::assertTrue($event->context['id']->isEmpty());
31+
self::assertTrue($event->context['id']->isNil());
3232
self::assertCount(1, $repository->findAll());
3333
self::assertSame($event->attribute, $repository->find($event->attribute->getId()));
3434
}
@@ -46,7 +46,7 @@ public function testCreateWithId(): void
4646
$repository = self::createAttributeRepository();
4747

4848
self::assertSame($id, $event->context['id'] ?? null);
49-
self::assertFalse($event->attribute->getId()->isEmpty());
49+
self::assertFalse($event->attribute->getId()->isNil());
5050
self::assertTrue($repository->exists($event->attribute->getId()));
5151
}
5252
}

src/User/Infrastructure/Security/UserIdentity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function __construct(User $user, ?string $originUsername = null, array $r
3434
{
3535
$this->id = $user->getId();
3636

37-
if ($this->id->isEmpty()) {
38-
throw new \LogicException('The user ID cannot be empty.');
37+
if ($this->id->isNil()) {
38+
throw new \LogicException('The user ID cannot be nil.');
3939
}
4040

4141
$credential = $user->getCredential();

src/User/Tests/Command/CreateUserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testCreate(): void
3535

3636
self::assertSame('bar', $event->context['foo'] ?? null);
3737
self::assertInstanceOf(UserId::class, $event->context['id'] ?? null);
38-
self::assertTrue($event->context['id']->isEmpty());
38+
self::assertTrue($event->context['id']->isNil());
3939
self::assertCount(1, $repository->findAll());
4040
self::assertSame($user, $repository->find($user->getId()));
4141
self::assertSame($user, $repository->findByUsername('user@localhost'));
@@ -58,7 +58,7 @@ public function testCreateWithId(): void
5858
$repository = self::createUserRepository();
5959

6060
self::assertSame($id, $event->context['id'] ?? null);
61-
self::assertFalse($event->user->getId()->isEmpty());
61+
self::assertFalse($event->user->getId()->isNil());
6262
self::assertTrue($repository->exists($event->user->getId()));
6363
self::assertTrue($repository->usernameExists('user@localhost'));
6464
}

0 commit comments

Comments
 (0)
0