Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replaces the generic BasicEntityId with the more specific UuidE…
…ntityId
- Loading branch information
Showing
16 changed files
with
177 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Matiux/DDDStarterPack/Aggregate/Domain/UuidEntityId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DDDStarterPack\Aggregate\Domain; | ||
|
||
use Exception; | ||
use InvalidArgumentException; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* @extends BasicEntityId<string> | ||
*/ | ||
class UuidEntityId extends BasicEntityId | ||
{ | ||
public const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/'; | ||
|
||
public function __toString(): string | ||
{ | ||
return !$this->id ? '' : $this->id; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* | ||
* @return static | ||
*/ | ||
public static function create(): static | ||
{ | ||
return new static(Uuid::uuid4()->toString()); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return static | ||
*/ | ||
public static function createFrom($id): static | ||
{ | ||
if (!$id || !self::isValidUuid($id)) { | ||
throw new InvalidArgumentException(sprintf('Invalid ID: %s', $id)); | ||
} | ||
|
||
return new static($id); | ||
} | ||
|
||
public static function isValidUuid(string $uuid): bool | ||
{ | ||
if (1 === preg_match(self::UUID_PATTERN, $uuid)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 0 additions & 89 deletions
89
tests/Unit/DDDStarterPack/Aggregate/Domain/BasicEntityIdTest.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.