mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-12 09:54:09 +02:00
update deps & install rector
This commit is contained in:
@@ -2,21 +2,16 @@
|
||||
|
||||
namespace DesignPatterns\More\EAV;
|
||||
|
||||
use SplObjectStorage;
|
||||
|
||||
class Attribute
|
||||
{
|
||||
/**
|
||||
* @var \SplObjectStorage
|
||||
*/
|
||||
private $values;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private SplObjectStorage $values;
|
||||
private string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->values = new \SplObjectStorage();
|
||||
$this->values = new SplObjectStorage();
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
@@ -25,10 +20,7 @@ class Attribute
|
||||
$this->values->attach($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \SplObjectStorage
|
||||
*/
|
||||
public function getValues(): \SplObjectStorage
|
||||
public function getValues(): SplObjectStorage
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ class Entity
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
@@ -22,6 +22,7 @@ class Entity
|
||||
*/
|
||||
public function __construct(string $name, $values)
|
||||
{
|
||||
/** @var SplObjectStorage<Value,Value> values */
|
||||
$this->values = new SplObjectStorage();
|
||||
$this->name = $name;
|
||||
|
||||
|
@@ -4,15 +4,8 @@ namespace DesignPatterns\More\EAV;
|
||||
|
||||
class Value
|
||||
{
|
||||
/**
|
||||
* @var Attribute
|
||||
*/
|
||||
private $attribute;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private Attribute $attribute;
|
||||
private string $name;
|
||||
|
||||
public function __construct(Attribute $attribute, string $name)
|
||||
{
|
||||
|
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,17 +2,12 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository;
|
||||
|
||||
use OutOfBoundsException;
|
||||
|
||||
class InMemoryPersistence implements Persistence
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lastId = 0;
|
||||
private array $data = [];
|
||||
private int $lastId = 0;
|
||||
|
||||
public function generateId(): int
|
||||
{
|
||||
@@ -29,7 +24,7 @@ class InMemoryPersistence implements Persistence
|
||||
public function retrieve(int $id): array
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
throw new OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
return $this->data[$id];
|
||||
@@ -38,7 +33,7 @@ class InMemoryPersistence implements Persistence
|
||||
public function delete(int $id)
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
throw new OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
unset($this->data[$id]);
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository;
|
||||
|
||||
use OutOfBoundsException;
|
||||
use DesignPatterns\More\Repository\Domain\Post;
|
||||
use DesignPatterns\More\Repository\Domain\PostId;
|
||||
|
||||
@@ -16,10 +17,7 @@ use DesignPatterns\More\Repository\Domain\PostId;
|
||||
*/
|
||||
class PostRepository
|
||||
{
|
||||
/**
|
||||
* @var Persistence
|
||||
*/
|
||||
private $persistence;
|
||||
private Persistence $persistence;
|
||||
|
||||
public function __construct(Persistence $persistence)
|
||||
{
|
||||
@@ -35,8 +33,8 @@ class PostRepository
|
||||
{
|
||||
try {
|
||||
$arrayData = $this->persistence->retrieve($id->toInt());
|
||||
} catch (\OutOfBoundsException $e) {
|
||||
throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id->toInt()), 0, $e);
|
||||
} catch (OutOfBoundsException $e) {
|
||||
throw new OutOfBoundsException(sprintf('Post with id %d does not exist', $id->toInt()), 0, $e);
|
||||
}
|
||||
|
||||
return Post::fromState($arrayData);
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository\Tests;
|
||||
|
||||
use OutOfBoundsException;
|
||||
use DesignPatterns\More\Repository\Domain\PostId;
|
||||
use DesignPatterns\More\Repository\Domain\PostStatus;
|
||||
use DesignPatterns\More\Repository\InMemoryPersistence;
|
||||
@@ -11,10 +12,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PostRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var PostRepository
|
||||
*/
|
||||
private $repository;
|
||||
private PostRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -28,7 +26,7 @@ class PostRepositoryTest extends TestCase
|
||||
|
||||
public function testThrowsExceptionWhenTryingToFindPostWhichDoesNotExist()
|
||||
{
|
||||
$this->expectException(\OutOfBoundsException::class);
|
||||
$this->expectException(OutOfBoundsException::class);
|
||||
$this->expectExceptionMessage('Post with id 42 does not exist');
|
||||
|
||||
$this->repository->findById(PostId::fromInt(42));
|
||||
|
@@ -2,17 +2,20 @@
|
||||
|
||||
namespace DesignPatterns\More\ServiceLocator;
|
||||
|
||||
use OutOfRangeException;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ServiceLocator
|
||||
{
|
||||
/**
|
||||
* @var string[][]
|
||||
*/
|
||||
private $services = [];
|
||||
private array $services = [];
|
||||
|
||||
/**
|
||||
* @var Service[]
|
||||
*/
|
||||
private $instantiated = [];
|
||||
private array $instantiated = [];
|
||||
|
||||
public function addInstance(string $class, Service $service)
|
||||
{
|
||||
@@ -51,11 +54,11 @@ class ServiceLocator
|
||||
$object = new $class($args[0], $args[1], $args[2]);
|
||||
break;
|
||||
default:
|
||||
throw new \OutOfRangeException('Too many arguments given');
|
||||
throw new OutOfRangeException('Too many arguments given');
|
||||
}
|
||||
|
||||
if (!$object instanceof Service) {
|
||||
throw new \InvalidArgumentException('Could not register service: is no instance of Service');
|
||||
throw new InvalidArgumentException('Could not register service: is no instance of Service');
|
||||
}
|
||||
|
||||
$this->instantiated[$class] = $object;
|
||||
|
@@ -8,10 +8,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ServiceLocatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ServiceLocator
|
||||
*/
|
||||
private $serviceLocator;
|
||||
private ServiceLocator $serviceLocator;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
|
Reference in New Issue
Block a user