8000 remove type hint from DomainId::equals() by ro0NL · Pull Request #383 · msgphp/msgphp · GitHub
[go: up one dir, main page]

Skip to content

remove type hint from DomainId::equals() #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
remove type hint from DomainId::equals()
  • Loading branch information
ro0NL committed Mar 14, 2020
commit 701550777dc130c4e8cfd9d08a65718d58184a1b
12 changes: 6 additions & 6 deletions docs/ddd/identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Tells if an identifier value is considered empty, thus has no known primitive va

---

### `equals(DomainId $id): bool`
### `equals($other): bool`

Tells if an identifier strictly equals another identifier.

Expand All @@ -32,7 +32,7 @@ be returned.

### `MsgPhp\Domain\DomainIdTrait`

A first-class citizen domain identifier trait compatible with any `scalar` value.
A first-class citizen domain identifier trait compatible with any string or numeric value.

#### Basic Example

Expand All @@ -57,12 +57,12 @@ $emptyId = new MyDomainId();
$id->isEmpty(); // false
$emptyId->isEmpty(); // true

$id->equals(new MyDomainId('1')); // true
$emptyId->equals(new MyDomainId()); // false
$emptyId->equals($emptyId); // true

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

$id->equals(new MyDomainId('1')); // true
$id->equals('1'); // false
$id->equals(new MyDomainId('2')); // false
```

### `MsgPhp\Domain\Infrastructure\Uuid\DomainIdTrait`
Expand Down
5 changes: 4 additions & 1 deletion src/Domain/DomainId.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public static function fromValue($value): self;

public function isEmpty(): bool;

public function equals(self $id): bool;
/**
* @param mixed $other
*/
public function equals($other): bool;

public function toString(): string;
}
22 changes: 12 additions & 10 deletions src/Domain/DomainIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,31 @@ public function __toString(): string
*/
public static function fromValue($value): DomainId
{
if (null !== $value && !\is_string($value)) {
$value = (string) $value;
if (null === $value || \is_string($value)) {
return new static($value);
}
if (is_numeric($value)) {
return new static((string) $value);
}

return new static($value);
throw new \LogicException('Raw ID value must be of type string or number, got "'.\gettype($value).'".');
}

public function isEmpty(): bool
{
return null === $this->id;
}

public function equals(DomainId $id): bool
/**
* @param mixed $other
*/
public function equals($other): bool
{
if ($id === $this) {
return true;
}

if (null === $this->id || $id->isEmpty() || static::class !== \get_class($id)) {
if (!$other instanceof self || static::class !== \get_class($other)) {
return false;
}

return $this->id === $id->toString();
return $this->id === $other->id;
}

public function toString(): string
Expand Down
22 changes: 12 additions & 10 deletions src/Domain/Infrastructure/Uuid/DomainIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,31 @@ public function __toString(): string
*/
public static function fromValue($value): DomainId
{
if (null !== $value && !$value instanceof UuidInterface) {
$value = Uuid::fromString((string) $value);
if (null === $value || $value instanceof UuidInterface) {
return new static($value);
}
if (\is_string($value)) {
return new static(Uuid::fromString($value));
}

return new static($value);
throw new \LogicException('Raw UUID value must be of type string, got "'.\gettype($value).'".');
}

public function isEmpty(): bool
{
return false;
}

public function equals(DomainId $id): bool
/**
* @param mixed $other
*/
public function equals($other): bool
{
if ($id === $this) {
return true;
}

if (static::class !== \get_class($id)) {
if (!$other instanceof self || static::class !== \get_class($other)) {
return false;
}

return $this->uuid->equals(Uuid::fromString($id->toString()));
return $this->uuid->equals($other->uuid);
}

public function toString(): string
Expand Down
13 changes: 12 additions & 1 deletion src/Domain/Tests/DomainIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public function testFromValue(): void
self::assertInstanceOf(TestOtherDomainId::class, TestOtherDomainId::fromValue(null));
}

public function testFromInvalidValue(): void
{
$this->expectException(\LogicException::class);

TestDomainId::fromValue(true);
}

public function testEmptyIdValue(): void
{
$this->expectException(\LogicException::class);
Expand All @@ -49,9 +56,13 @@ public function testEquals(): void
self::assertTrue($id->equals(new TestDomainId('foo')));
self::assertFalse($id->equals($emptyId));
self::assertFalse($id->equals(new TestOtherDomainId('foo')));
self::assertFalse($id->equals('foo'));
self::assertFalse($id->equals(new \stdClass()));
self::assertTrue($emptyId->equals($emptyId));
self::assertFalse($emptyId->equals(new TestDomainId()));
self::assertTrue($emptyId->equals(new TestDomainId()));
self::assertFalse($emptyId->equals(new TestOtherDomainId()));
self::assertFalse($emptyId->equals(''));
self::assertFalse($emptyId->equals(new \stdClass()));
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Domain/Tests/Infrastructure/Uuid/DomainIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MsgPhp\Domain\Tests\Fixtures\TestDomainUuid;
use MsgPhp\Domain\Tests\Fixtures\TestOtherDomainUuid;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

Expand All @@ -31,11 +30,18 @@ public function testFromValue(): void

public function testFromValueWithInvalidUuid(): void
{
$this->expectException(InvalidUuidStringException::class);
$this->expectException(\LogicException::class);

TestDomainUuid::fromValue('00000000-0000-0000-0000-00000000000');
}

public function testFromInvalidValue(): void
{
$this->expectException(\LogicException::class);

TestDomainUuid::fromValue(true);
}

public function testIsEmpty(): void
{
self::assertFalse((new TestDomainUuid())->isEmpty());
Expand All @@ -50,6 +56,8 @@ public function testEquals(): void
self::assertTrue($id->equals(new TestDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
self::assertFalse($id->equals(new TestDomainUuid()));
self::assertFalse($id->equals(new TestOtherDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
self::assertFalse($id->equals('00000000-0000-0000-0000-000000000000'));
self::assertFalse($id->equals(new \stdClass()));
}

public function testToString(): void
Expand Down
0