Skip to content
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

Add specification pattern example #42

Merged
merged 5 commits into from
Oct 1, 2018
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
4 changes: 4 additions & 0 deletions src/Context/Video/Module/Video/Domain/VideoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace CodelyTv\Context\Video\Module\Video\Domain;

use CodelyTv\Shared\Domain\Criteria\Criteria;

interface VideoRepository
{
public function save(Video $video): void;

public function search(VideoId $id): ?Video;

public function searchByCriteria(Criteria $criteria): Videos;
}
15 changes: 15 additions & 0 deletions src/Context/Video/Module/Video/Domain/Videos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Context\Video\Module\Video\Domain;

use CodelyTv\Types\Collection;

final class Videos extends Collection
{
protected function type(): string
{
return Video::class;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace CodelyTv\Context\Video\Module\Video\Infrastructure\Persistence;

use CodelyTv\Context\Video\Module\Video\Domain\Video;
use CodelyTv\Context\Video\Module\Video\Domain\VideoId;
use CodelyTv\Context\Video\Module\Video\Domain\VideoRepository;
use CodelyTv\Context\Video\Module\Video\Domain\Videos;
use CodelyTv\Infrastructure\Doctrine\DoctrineCriteriaConverter;
use CodelyTv\Infrastructure\Doctrine\Repository;
use CodelyTv\Shared\Domain\Criteria\Criteria;

final class VideoRepositoryMySql extends Repository implements VideoRepository
{
private static $criteriaToDoctrineFields = [
'id' => 'id',
'type' => 'type',
'title' => 'title',
'url' => 'url',
'course_id' => 'courseId',
];

public function save(Video $video): void
{
$this->persist($video);
Expand All @@ -20,4 +31,12 @@ public function search(VideoId $id): ?Video
{
return $this->repository(Video::class)->find($id);
}

public function searchByCriteria(Criteria $criteria): Videos
{
$doctrineCriteria = DoctrineCriteriaConverter::convert($criteria, self::$criteriaToDoctrineFields);
$videos = $this->repository(Video::class)->matching($doctrineCriteria)->toArray();

return new Videos($videos);
}
}
114 changes: 114 additions & 0 deletions src/Infrastructure/Doctrine/DoctrineCriteriaConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace CodelyTv\Infrastructure\Doctrine;

use CodelyTv\Shared\Domain\Criteria\Criteria;
use CodelyTv\Shared\Domain\Criteria\Filter;
use CodelyTv\Shared\Domain\Criteria\FilterField;
use CodelyTv\Shared\Domain\Criteria\OrderBy;
use Doctrine\Common\Collections\Criteria as DoctrineCriteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\CompositeExpression;

final class DoctrineCriteriaConverter
{
private $criteria;
private $criteriaToDoctrineFields;
private $hydrators;

public function __construct(Criteria $criteria, array $criteriaToDoctrineFields = [], array $hydrators = [])
{
$this->criteria = $criteria;
$this->criteriaToDoctrineFields = $criteriaToDoctrineFields;
$this->hydrators = $hydrators;
}

public static function convert(Criteria $criteria, array $criteriaToDoctrineFields = [], array $hydrators = [])
{
$converter = new self($criteria, $criteriaToDoctrineFields, $hydrators);

return $converter->convertToDoctrineCriteria();
}

public static function convertToCount(
Criteria $criteria,
array $criteriaToDoctrineFields = [],
array $hydrators = []
) {
$converter = new self($criteria, $criteriaToDoctrineFields, $hydrators);

return $converter->convertToDoctrineCriteriaToCount();
}

private function convertToDoctrineCriteria()
{
return new DoctrineCriteria(
$this->buildExpression($this->criteria),
$this->formatOrder($this->criteria),
$this->criteria->offset(),
$this->criteria->limit()
);
}

private function convertToDoctrineCriteriaToCount()
{
return new DoctrineCriteria($this->buildExpression($this->criteria), $this->formatOrder($this->criteria));
}

private function buildExpression(Criteria $criteria)
{
if ($criteria->hasFilters()) {
return new CompositeExpression(
CompositeExpression::TYPE_AND,
array_map($this->buildComparison(), $criteria->plainFilters())
);
}

return null;
}

private function buildComparison()
{
return function (Filter $filter) {
$field = $this->mapFieldValue($filter->field());
$value = $this->existsHydratorFor($field) ?
$this->hydrate($field, $filter->value()->value()) :
$filter->value()->value();

return new Comparison($field, $filter->operator()->value(), $value);
};
}

private function mapFieldValue(FilterField $field)
{
return array_key_exists($field->value(), $this->criteriaToDoctrineFields) ?
$this->criteriaToDoctrineFields[$field->value()] :
$field->value();
}

private function formatOrder(Criteria $criteria)
{
if (!$criteria->hasOrder()) {
return null;
}

return [$this->mapOrderBy($criteria->order()->orderBy()) => $criteria->order()->orderType()];
}

private function mapOrderBy(OrderBy $field)
{
return array_key_exists($field->value(), $this->criteriaToDoctrineFields) ?
$this->criteriaToDoctrineFields[$field->value()] :
$field->value();
}

private function existsHydratorFor($field)
{
return array_key_exists($field, $this->hydrators);
}

private function hydrate($field, $value)
{
return $this->hydrators[$field]($value);
}
}
56 changes: 56 additions & 0 deletions src/Shared/Domain/Criteria/Criteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Shared\Domain\Criteria;

final class Criteria
{
private $filters;
private $order;
private $offset;
private $limit;

public function __construct(Filters $filters, ?Order $order, ?int $offset, ?int $limit)
{
$this->filters = $filters;
$this->order = $order;
$this->offset = $offset;
$this->limit = $limit;
}

public function hasFilters(): bool
{
return $this->filters->count() > 0;
}

public function hasOrder(): bool
{
return null !== $this->order;
}

public function plainFilters(): array
{
return $this->filters->filters();
}

public function filters(): Filters
{
return $this->filters;
}

public function order(): ?Order
{
return $this->order;
}

public function offset(): ?int
{
return $this->offset;
}

public function limit(): ?int
{
return $this->limit;
}
}
41 changes: 41 additions & 0 deletions src/Shared/Domain/Criteria/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace CodelyTv\Shared\Domain\Criteria;

final class Filter
{
private $field;
private $operator;
private $value;

public function __construct(FilterField $field, FilterOperator $operator, FilterValue $value)
{
$this->field = $field;
$this->operator = $operator;
$this->value = $value;
}

public static function fromValues(array $values): self
{
return new self(
new FilterField($values['field']),
new FilterOperator($values['operator']),
new FilterValue($values['value'])
);
}

public function field(): FilterField
{
return $this->field;
}

public function operator(): FilterOperator
{
return $this->operator;
}

public function value(): FilterValue
{
return $this->value;
}
}
9 changes: 9 additions & 0 deletions src/Shared/Domain/Criteria/FilterField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Types\ValueObject\StringValueObject;

final class FilterField extends StringValueObject
{
}
29 changes: 29 additions & 0 deletions src/Shared/Domain/Criteria/FilterOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Types\ValueObject\Enum;
use InvalidArgumentException;

/**
* @method static FilterOperator gt()
* @method static FilterOperator lt()
* @method static FilterOperator like()
*/
final class FilterOperator extends Enum
{
const EQUAL = '=';
const GT = '>';
const LT = '<';
const CONTAINS = 'CONTAINS';

public static function equal(): self
{
return new self('=');
}

protected function throwExceptionForInvalidValue($value)
{
throw new InvalidArgumentException(sprintf('The filter <%s> is invalid', $value));
}
}
9 changes: 9 additions & 0 deletions src/Shared/Domain/Criteria/FilterValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Types\ValueObject\StringValueObject;

final class FilterValue extends StringValueObject
{
}
35 changes: 35 additions & 0 deletions src/Shared/Domain/Criteria/Filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Types\Collection;

final class Filters extends Collection
{
protected function type(): string
{
return Filter::class;
}

public static function fromValues(array $values): self
{
return new self(array_map(self::filterBuilder(), $values));
}

public function add(Filter $filter): self
{
return new self(array_merge($this->items(), [$filter]));
}

public function filters(): array
{
return $this->items();
}

private static function filterBuilder(): callable
{
return function (array $values) {
return Filter::fromValues($values);
};
}
}
Loading