| @@ 9-42 (lines=34) @@ | ||
| 6 | use PtrTn\Battlerite\Exception\InvalidQueryException; |
|
| 7 | use PtrTn\Battlerite\Query\CriterionInterface; |
|
| 8 | ||
| 9 | class EndDateCriterion implements CriterionInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var DateTime |
|
| 13 | */ |
|
| 14 | private $endDate; |
|
| 15 | ||
| 16 | public function __construct(DateTime $endDate) |
|
| 17 | { |
|
| 18 | $this->endDate = $endDate; |
|
| 19 | } |
|
| 20 | ||
| 21 | public function toArray(): array |
|
| 22 | { |
|
| 23 | return ['filter[createdAt-end]' => $this->endDate->format(DateTime::ISO8601)]; |
|
| 24 | } |
|
| 25 | ||
| 26 | public function checkCollisionWithCriteria(array $critera): void |
|
| 27 | { |
|
| 28 | foreach ($critera as $criterion) { |
|
| 29 | if ($criterion instanceof StartDateCriterion) { |
|
| 30 | if ($this->endDate < $criterion->getValue()) { |
|
| 31 | throw InvalidQueryException::criteriaCollision('End date must be later than start date'); |
|
| 32 | } |
|
| 33 | } |
|
| 34 | } |
|
| 35 | return; |
|
| 36 | } |
|
| 37 | ||
| 38 | public function getValue(): DateTime |
|
| 39 | { |
|
| 40 | return $this->endDate; |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||
| @@ 9-42 (lines=34) @@ | ||
| 6 | use PtrTn\Battlerite\Exception\InvalidQueryException; |
|
| 7 | use PtrTn\Battlerite\Query\CriterionInterface; |
|
| 8 | ||
| 9 | class StartDateCriterion implements CriterionInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var DateTime |
|
| 13 | */ |
|
| 14 | private $startDate; |
|
| 15 | ||
| 16 | public function __construct(DateTime $startDate) |
|
| 17 | { |
|
| 18 | $this->startDate = $startDate; |
|
| 19 | } |
|
| 20 | ||
| 21 | public function toArray(): array |
|
| 22 | { |
|
| 23 | return ['filter[createdAt-start]' => $this->startDate->format(DateTime::ISO8601)]; |
|
| 24 | } |
|
| 25 | ||
| 26 | public function checkCollisionWithCriteria(array $critera): void |
|
| 27 | { |
|
| 28 | foreach ($critera as $criterion) { |
|
| 29 | if ($criterion instanceof EndDateCriterion) { |
|
| 30 | if ($criterion->getValue() < $this->startDate) { |
|
| 31 | throw InvalidQueryException::criteriaCollision('End date must be later than start date'); |
|
| 32 | } |
|
| 33 | } |
|
| 34 | } |
|
| 35 | return; |
|
| 36 | } |
|
| 37 | ||
| 38 | public function getValue(): DateTime |
|
| 39 | { |
|
| 40 | return $this->startDate; |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||