mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
introduced PostId and PostStatus and changed naming from Storage to Persistence
This commit is contained in:
42
More/Repository/Domain/PostId.php
Normal file
42
More/Repository/Domain/PostId.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Repository\Domain;
|
||||
|
||||
/**
|
||||
* This is a perfect example of a value object that is identifiable by it's value alone and
|
||||
* is guaranteed to be valid each time an instance is created. Another important property of value objects
|
||||
* is immutability.
|
||||
*
|
||||
* Notice also the use of a named constructor (fromInt) which adds a little context when creating an instance.
|
||||
*/
|
||||
class PostId
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
public static function fromInt(int $id)
|
||||
{
|
||||
self::ensureIsValid($id);
|
||||
|
||||
return new self($id);
|
||||
}
|
||||
|
||||
private function __construct(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function toInt(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
private static function ensureIsValid(int $id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
throw new \InvalidArgumentException('Invalid PostId given');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user