Skip to content

Commit

Permalink
chore: use native php enum type (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas authored Oct 3, 2023
1 parent 23a2319 commit 9c90282
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private VideoCreator $creator) {}
public function __invoke(CreateVideoCommand $command): void
{
$id = new VideoId($command->id());
$type = new VideoType($command->type());
$type = VideoType::from($command->type());
$title = new VideoTitle($command->title());
$url = new VideoUrl($command->url());
$courseId = new CourseId($command->courseId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __invoke(Video $video): VideoResponse
{
return new VideoResponse(
$video->id()->value(),
$video->type()->value(),
$video->type()->value,
$video->title()->value(),
$video->url()->value(),
$video->courseId()->value()
Expand Down
2 changes: 1 addition & 1 deletion src/Mooc/Videos/Domain/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function create(
$video->record(
new VideoCreatedDomainEvent(
$id->value(),
$type->value(),
$type->value,
$title->value(),
$url->value(),
$courseId->value()
Expand Down
18 changes: 3 additions & 15 deletions src/Mooc/Videos/Domain/VideoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@

namespace CodelyTv\Mooc\Videos\Domain;

use CodelyTv\Shared\Domain\ValueObject\Enum;
use InvalidArgumentException;

/**
* @method static VideoType screencast()
* @method static VideoType interview()
*/
final class VideoType extends Enum
enum VideoType: string
{
public const SCREENCAST = 'screencast';
public const INTERVIEW = 'interview';

protected function throwExceptionForInvalidValue($value): never
{
throw new InvalidArgumentException(sprintf('The <%s> value is not a valid video type', $value));
}
case SCREENCAST = 'screencast';
case INTERVIEW = 'interview';
}
4 changes: 2 additions & 2 deletions src/Shared/Domain/Criteria/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function fromValues(array $values): self
{
return new self(
new FilterField($values['field']),
new FilterOperator($values['operator']),
FilterOperator::from($values['operator']),
new FilterValue($values['value'])
);
}
Expand All @@ -38,6 +38,6 @@ public function value(): FilterValue

public function serialize(): string
{
return sprintf('%s.%s.%s', $this->field->value(), $this->operator->value(), $this->value->value());
return sprintf('%s.%s.%s', $this->field->value(), $this->operator->value, $this->value->value());
}
}
30 changes: 8 additions & 22 deletions src/Shared/Domain/Criteria/FilterOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,17 @@

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Shared\Domain\ValueObject\Enum;
use InvalidArgumentException;

/**
* @method static FilterOperator gt()
* @method static FilterOperator lt()
* @method static FilterOperator like()
*/
final class FilterOperator extends Enum
enum FilterOperator: string
{
public const EQUAL = '=';
public const NOT_EQUAL = '!=';
public const GT = '>';
public const LT = '<';
public const CONTAINS = 'CONTAINS';
public const NOT_CONTAINS = 'NOT_CONTAINS';
private static array $containing = [self::CONTAINS, self::NOT_CONTAINS];
case EQUAL = '=';
case NOT_EQUAL = '!=';
case GT = '>';
case LT = '<';
case CONTAINS = 'CONTAINS';
case NOT_CONTAINS = 'NOT_CONTAINS';

public function isContaining(): bool
{
return in_array($this->value(), self::$containing, true);
}

protected function throwExceptionForInvalidValue($value): never
{
throw new InvalidArgumentException(sprintf('The filter <%s> is invalid', $value));
return in_array($this->value, [self::CONTAINS->value, self::NOT_CONTAINS->value], true);
}
}
8 changes: 4 additions & 4 deletions src/Shared/Domain/Criteria/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public function __construct(private OrderBy $orderBy, private OrderType $orderTy

public static function createDesc(OrderBy $orderBy): self
{
return new self($orderBy, OrderType::desc());
return new self($orderBy, OrderType::DESC);
}

public static function fromValues(?string $orderBy, ?string $order): self
{
return $orderBy === null ? self::none() : new self(new OrderBy($orderBy), new OrderType($order));
return $orderBy === null ? self::none() : new self(new OrderBy($orderBy), OrderType::from($order));
}

public static function none(): self
{
return new self(new OrderBy(''), OrderType::none());
return new self(new OrderBy(''), OrderType::NONE);
}

public function orderBy(): OrderBy
Expand All @@ -40,6 +40,6 @@ public function isNone(): bool

public function serialize(): string
{
return sprintf('%s.%s', $this->orderBy->value(), $this->orderType->value());
return sprintf('%s.%s', $this->orderBy->value(), $this->orderType->value);
}
}
23 changes: 5 additions & 18 deletions src/Shared/Domain/Criteria/OrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@

namespace CodelyTv\Shared\Domain\Criteria;

use CodelyTv\Shared\Domain\ValueObject\Enum;
use InvalidArgumentException;

/**
* @method static OrderType asc()
* @method static OrderType desc()
* @method static OrderType none()
*/
final class OrderType extends Enum
enum OrderType: string
{
public const ASC = 'asc';
public const DESC = 'desc';
public const NONE = 'none';
case ASC = 'asc';
case DESC = 'desc';
case NONE = 'none';

public function isNone(): bool
{
return $this->equals(self::none());
}

protected function throwExceptionForInvalidValue($value): never
{
throw new InvalidArgumentException($value);
return $this->value === self::NONE->value;
}
}
83 changes: 0 additions & 83 deletions src/Shared/Domain/ValueObject/Enum.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function buildComparison(): callable
? $this->hydrate($field, $filter->value()->value())
: $filter->value()->value();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CodelyTv\Shared\Domain\Criteria\Filter;
use CodelyTv\Shared\Domain\Criteria\FilterOperator;
use Exception;

final class ElasticQueryGenerator
{
Expand Down Expand Up @@ -38,17 +37,16 @@ public function __invoke(array $query, Filter $filter): array

private function typeFor(FilterOperator $operator): string
{
return in_array($operator->value(), self::$mustNotFields, true) ? self::MUST_NOT_TYPE : self::MUST_TYPE;
return in_array($operator->value, self::$mustNotFields, true) ? self::MUST_NOT_TYPE : self::MUST_TYPE;
}

private function termLevelFor(FilterOperator $operator): string
{
return match ($operator->value()) {
return match ($operator) {
FilterOperator::EQUAL => self::TERM_TERM,
FilterOperator::NOT_EQUAL => '!=',
FilterOperator::GT, FilterOperator::LT => self::TERM_RANGE,
FilterOperator::CONTAINS, FilterOperator::NOT_CONTAINS => self::TERM_WILDCARD,
default => throw new Exception("Unexpected match value {$operator->value()}"),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function formatSort(Criteria $criteria): array
return [
'sort' => [
$order->orderBy()->value() => [
'order' => $order->orderType()->value(),
'order' => $order->orderType()->value,
],
],
];
Expand Down
16 changes: 15 additions & 1 deletion tests/Shared/Domain/Criteria/FilterMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CodelyTv\Shared\Domain\Criteria\FilterField;
use CodelyTv\Shared\Domain\Criteria\FilterOperator;
use CodelyTv\Shared\Domain\Criteria\FilterValue;
use CodelyTv\Tests\Shared\Domain\RandomElementPicker;

final class FilterMother
{
Expand All @@ -18,7 +19,7 @@ public static function create(
): Filter {
return new Filter(
$field ?? FilterFieldMother::create(),
$operator ?? FilterOperator::random(),
$operator ?? self::randomOperator(),
$value ?? FilterValueMother::create()
);
}
Expand All @@ -28,4 +29,17 @@ public static function fromValues(array $values): Filter
{
return Filter::fromValues($values);
}


private static function randomOperator(): FilterOperator
{
return RandomElementPicker::from(
FilterOperator::EQUAL,
FilterOperator::NOT_EQUAL,
FilterOperator::GT,
FilterOperator::LT,
FilterOperator::CONTAINS,
FilterOperator::NOT_CONTAINS
);
}
}
8 changes: 7 additions & 1 deletion tests/Shared/Domain/Criteria/OrderMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
use CodelyTv\Shared\Domain\Criteria\Order;
use CodelyTv\Shared\Domain\Criteria\OrderBy;
use CodelyTv\Shared\Domain\Criteria\OrderType;
use CodelyTv\Tests\Shared\Domain\RandomElementPicker;

final class OrderMother
{
public static function create(?OrderBy $orderBy = null, ?OrderType $orderType = null): Order
{
return new Order($orderBy ?? OrderByMother::create(), $orderType ?? OrderType::random());
return new Order($orderBy ?? OrderByMother::create(), $orderType ?? self::randomOrderType());
}

public static function none(): Order
{
return Order::none();
}

private static function randomOrderType(): Order
{
return RandomElementPicker::from(OrderType::ASC, OrderType::DESC, OrderType::NONE);
}
}

0 comments on commit 9c90282

Please sign in to comment.