mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
update deps & install rector
This commit is contained in:
@@ -4,25 +4,10 @@ namespace DesignPatterns\More\Repository\Domain;
|
||||
|
||||
class Post
|
||||
{
|
||||
/**
|
||||
* @var PostId
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var PostStatus
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $title;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
private PostId $id;
|
||||
private PostStatus $status;
|
||||
private string $title;
|
||||
private string $text;
|
||||
|
||||
public static function draft(PostId $id, string $title, string $text): Post
|
||||
{
|
||||
@@ -44,12 +29,6 @@ class Post
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostId $id
|
||||
* @param PostStatus $status
|
||||
* @param string $title
|
||||
* @param string $text
|
||||
*/
|
||||
private function __construct(PostId $id, PostStatus $status, string $title, string $text)
|
||||
{
|
||||
$this->id = $id;
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository\Domain;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -11,12 +13,9 @@ namespace DesignPatterns\More\Repository\Domain;
|
||||
*/
|
||||
class PostId
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
private int $id;
|
||||
|
||||
public static function fromInt(int $id)
|
||||
public static function fromInt(int $id): PostId
|
||||
{
|
||||
self::ensureIsValid($id);
|
||||
|
||||
@@ -36,7 +35,7 @@ class PostId
|
||||
private static function ensureIsValid(int $id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
throw new \InvalidArgumentException('Invalid PostId given');
|
||||
throw new InvalidArgumentException('Invalid PostId given');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository\Domain;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Like PostId, this is a value object which holds the value of the current status of a Post. It can be constructed
|
||||
* either from a string or int and is able to validate itself. An instance can then be converted back to int or string.
|
||||
@@ -14,20 +16,13 @@ class PostStatus
|
||||
const STATE_DRAFT = 'draft';
|
||||
const STATE_PUBLISHED = 'published';
|
||||
|
||||
private static $validStates = [
|
||||
private static array $validStates = [
|
||||
self::STATE_DRAFT_ID => self::STATE_DRAFT,
|
||||
self::STATE_PUBLISHED_ID => self::STATE_PUBLISHED,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private int $id;
|
||||
private string $name;
|
||||
|
||||
public static function fromInt(int $statusId)
|
||||
{
|
||||
@@ -39,8 +34,13 @@ class PostStatus
|
||||
public static function fromString(string $status)
|
||||
{
|
||||
self::ensureIsValidName($status);
|
||||
$state = array_search($status, self::$validStates);
|
||||
|
||||
return new self(array_search($status, self::$validStates), $status);
|
||||
if ($state === false) {
|
||||
throw new InvalidArgumentException('Invalid state given!');
|
||||
}
|
||||
|
||||
return new self($state, $status);
|
||||
}
|
||||
|
||||
private function __construct(int $id, string $name)
|
||||
@@ -66,7 +66,7 @@ class PostStatus
|
||||
private static function ensureIsValidId(int $status)
|
||||
{
|
||||
if (!in_array($status, array_keys(self::$validStates), true)) {
|
||||
throw new \InvalidArgumentException('Invalid status id given');
|
||||
throw new InvalidArgumentException('Invalid status id given');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ class PostStatus
|
||||
private static function ensureIsValidName(string $status)
|
||||
{
|
||||
if (!in_array($status, self::$validStates, true)) {
|
||||
throw new \InvalidArgumentException('Invalid status name given');
|
||||
throw new InvalidArgumentException('Invalid status name given');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user