Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| IdType | |
92.86% |
13 / 14 |
|
66.67% |
2 / 3 |
12.05 | |
0.00% |
0 / 1 |
| tryFromName | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
| toConstructorArgument | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| toConstructorBody | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Maker\Enums; |
| 3 | |
| 4 | use Apie\CommonValueObjects\Email; |
| 5 | use Apie\Core\Identifiers\AutoIncrementInteger; |
| 6 | use Apie\Core\Identifiers\CamelCaseSlug; |
| 7 | use Apie\Core\Identifiers\Identifier; |
| 8 | use Apie\Core\Identifiers\PascalCaseSlug; |
| 9 | use Apie\Core\Identifiers\Ulid; |
| 10 | use Apie\Core\Identifiers\UuidV4; |
| 11 | use Nette\PhpGenerator\Parameter; |
| 12 | use Nette\PhpGenerator\PromotedParameter; |
| 13 | |
| 14 | enum IdType: string |
| 15 | { |
| 16 | case Uuid = UuidV4::class; |
| 17 | case Identifier = Identifier::class; |
| 18 | case UppercaseSlug = PascalCaseSlug::class; |
| 19 | case LowercaseSlug = CamelCaseSlug::class; |
| 20 | case Email = Email::class; |
| 21 | case Integer = AutoIncrementInteger::class; |
| 22 | case Ulid = Ulid::class; |
| 23 | |
| 24 | public static function tryFromName(string $name): ?static |
| 25 | { |
| 26 | foreach (self::cases() as $case) { |
| 27 | if ($case->name === $name || $case->value === $name) { |
| 28 | return $case; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | public function toConstructorArgument(string $type): PromotedParameter|Parameter|null |
| 36 | { |
| 37 | return match($this) { |
| 38 | self::Uuid, self::Ulid => (new Parameter('id'))->setType('?' . $type)->setDefaultValue(null), |
| 39 | self::UppercaseSlug, self::LowercaseSlug, self::Identifier, self::Email => (new PromotedParameter('id'))->setType($type), |
| 40 | default => null, |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | public function toConstructorBody(string $type): string |
| 45 | { |
| 46 | return match($this) { |
| 47 | self::Integer => '$this->id = new ' . $type . '(null);', |
| 48 | self::Uuid, self::Ulid => '$this->id = $id ?? ' . $type . '::createRandom();', |
| 49 | default => '', |
| 50 | }; |
| 51 | } |
| 52 | } |