Skip to content

Commit 9c90282

Browse files
authored
chore: use native php enum type (#357)
1 parent 23a2319 commit 9c90282

14 files changed

Lines changed: 51 additions & 155 deletions

File tree

src/Mooc/Videos/Application/Create/CreateVideoCommandHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(private VideoCreator $creator) {}
1818
public function __invoke(CreateVideoCommand $command): void
1919
{
2020
$id = new VideoId($command->id());
21-
$type = new VideoType($command->type());
21+
$type = VideoType::from($command->type());
2222
$title = new VideoTitle($command->title());
2323
$url = new VideoUrl($command->url());
2424
$courseId = new CourseId($command->courseId());

src/Mooc/Videos/Application/Find/VideoResponseConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __invoke(Video $video): VideoResponse
1212
{
1313
return new VideoResponse(
1414
$video->id()->value(),
15-
$video->type()->value(),
15+
$video->type()->value,
1616
$video->title()->value(),
1717
$video->url()->value(),
1818
$video->courseId()->value()

src/Mooc/Videos/Domain/Video.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function create(
3030
$video->record(
3131
new VideoCreatedDomainEvent(
3232
$id->value(),
33-
$type->value(),
33+
$type->value,
3434
$title->value(),
3535
$url->value(),
3636
$courseId->value()

src/Mooc/Videos/Domain/VideoType.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,8 @@
44

55
namespace CodelyTv\Mooc\Videos\Domain;
66

7-
use CodelyTv\Shared\Domain\ValueObject\Enum;
8-
use InvalidArgumentException;
9-
10-
/**
11-
* @method static VideoType screencast()
12-
* @method static VideoType interview()
13-
*/
14-
final class VideoType extends Enum
7+
enum VideoType: string
158
{
16-
public const SCREENCAST = 'screencast';
17-
public const INTERVIEW = 'interview';
18-
19-
protected function throwExceptionForInvalidValue($value): never
20-
{
21-
throw new InvalidArgumentException(sprintf('The <%s> value is not a valid video type', $value));
22-
}
9+
case SCREENCAST = 'screencast';
10+
case INTERVIEW = 'interview';
2311
}

src/Shared/Domain/Criteria/Filter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function fromValues(array $values): self
1616
{
1717
return new self(
1818
new FilterField($values['field']),
19-
new FilterOperator($values['operator']),
19+
FilterOperator::from($values['operator']),
2020
new FilterValue($values['value'])
2121
);
2222
}
@@ -38,6 +38,6 @@ public function value(): FilterValue
3838

3939
public function serialize(): string
4040
{
41-
return sprintf('%s.%s.%s', $this->field->value(), $this->operator->value(), $this->value->value());
41+
return sprintf('%s.%s.%s', $this->field->value(), $this->operator->value, $this->value->value());
4242
}
4343
}

src/Shared/Domain/Criteria/FilterOperator.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,17 @@
44

55
namespace CodelyTv\Shared\Domain\Criteria;
66

7-
use CodelyTv\Shared\Domain\ValueObject\Enum;
8-
use InvalidArgumentException;
9-
10-
/**
11-
* @method static FilterOperator gt()
12-
* @method static FilterOperator lt()
13-
* @method static FilterOperator like()
14-
*/
15-
final class FilterOperator extends Enum
7+
enum FilterOperator: string
168
{
17-
public const EQUAL = '=';
18-
public const NOT_EQUAL = '!=';
19-
public const GT = '>';
20-
public const LT = '<';
21-
public const CONTAINS = 'CONTAINS';
22-
public const NOT_CONTAINS = 'NOT_CONTAINS';
23-
private static array $containing = [self::CONTAINS, self::NOT_CONTAINS];
9+
case EQUAL = '=';
10+
case NOT_EQUAL = '!=';
11+
case GT = '>';
12+
case LT = '<';
13+
case CONTAINS = 'CONTAINS';
14+
case NOT_CONTAINS = 'NOT_CONTAINS';
2415

2516
public function isContaining(): bool
2617
{
27-
return in_array($this->value(), self::$containing, true);
28-
}
29-
30-
protected function throwExceptionForInvalidValue($value): never
31-
{
32-
throw new InvalidArgumentException(sprintf('The filter <%s> is invalid', $value));
18+
return in_array($this->value, [self::CONTAINS->value, self::NOT_CONTAINS->value], true);
3319
}
3420
}

src/Shared/Domain/Criteria/Order.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ public function __construct(private OrderBy $orderBy, private OrderType $orderTy
1010

1111
public static function createDesc(OrderBy $orderBy): self
1212
{
13-
return new self($orderBy, OrderType::desc());
13+
return new self($orderBy, OrderType::DESC);
1414
}
1515

1616
public static function fromValues(?string $orderBy, ?string $order): self
1717
{
18-
return $orderBy === null ? self::none() : new self(new OrderBy($orderBy), new OrderType($order));
18+
return $orderBy === null ? self::none() : new self(new OrderBy($orderBy), OrderType::from($order));
1919
}
2020

2121
public static function none(): self
2222
{
23-
return new self(new OrderBy(''), OrderType::none());
23+
return new self(new OrderBy(''), OrderType::NONE);
2424
}
2525

2626
public function orderBy(): OrderBy
@@ -40,6 +40,6 @@ public function isNone(): bool
4040

4141
public function serialize(): string
4242
{
43-
return sprintf('%s.%s', $this->orderBy->value(), $this->orderType->value());
43+
return sprintf('%s.%s', $this->orderBy->value(), $this->orderType->value);
4444
}
4545
}

src/Shared/Domain/Criteria/OrderType.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,14 @@
44

55
namespace CodelyTv\Shared\Domain\Criteria;
66

7-
use CodelyTv\Shared\Domain\ValueObject\Enum;
8-
use InvalidArgumentException;
9-
10-
/**
11-
* @method static OrderType asc()
12-
* @method static OrderType desc()
13-
* @method static OrderType none()
14-
*/
15-
final class OrderType extends Enum
7+
enum OrderType: string
168
{
17-
public const ASC = 'asc';
18-
public const DESC = 'desc';
19-
public const NONE = 'none';
9+
case ASC = 'asc';
10+
case DESC = 'desc';
11+
case NONE = 'none';
2012

2113
public function isNone(): bool
2214
{
23-
return $this->equals(self::none());
24-
}
25-
26-
protected function throwExceptionForInvalidValue($value): never
27-
{
28-
throw new InvalidArgumentException($value);
15+
return $this->value === self::NONE->value;
2916
}
3017
}

src/Shared/Domain/ValueObject/Enum.php

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/Shared/Infrastructure/Persistence/Doctrine/DoctrineCriteriaConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private function buildComparison(): callable
6060
? $this->hydrate($field, $filter->value()->value())
6161
: $filter->value()->value();
6262

63-
return new Comparison($field, $filter->operator()->value(), $value);
63+
return new Comparison($field, $filter->operator()->value, $value);
6464
};
6565
}
6666

0 commit comments

Comments
 (0)