Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.83% |
23 / 24 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| PossibleRoutePrefixes | |
95.83% |
23 / 24 |
|
83.33% |
5 / 6 |
16 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRandom | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| fromNative | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| toNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRouteRequirements | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| __toString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\FakeMethod; |
| 5 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 6 | use Apie\Core\ValueObjects\Utils; |
| 7 | use Faker\Generator; |
| 8 | use Stringable; |
| 9 | |
| 10 | #[FakeMethod('createRandom')] |
| 11 | final class PossibleRoutePrefixes implements ValueObjectInterface, Stringable |
| 12 | { |
| 13 | /** |
| 14 | * @param array<int, string> $internal |
| 15 | */ |
| 16 | private function __construct( |
| 17 | private array $internal |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | public static function createRandom(Generator $faker): static |
| 22 | { |
| 23 | $val = []; |
| 24 | if ($faker->boolean()) { |
| 25 | $val[] = 'api'; |
| 26 | } |
| 27 | if ($faker->boolean()) { |
| 28 | $val[] = 'cms'; |
| 29 | } |
| 30 | |
| 31 | return static::fromNative($val); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return static |
| 36 | */ |
| 37 | public static function fromNative(mixed $input): self |
| 38 | { |
| 39 | if ($input instanceof ValueObjectInterface) { |
| 40 | $input = $input->toNative(); |
| 41 | } |
| 42 | if (is_string($input)) { |
| 43 | return new PossibleRoutePrefixes([$input]); |
| 44 | } |
| 45 | return new PossibleRoutePrefixes(Utils::toArray($input)); |
| 46 | } |
| 47 | /** |
| 48 | * @return array<string|int, mixed> |
| 49 | */ |
| 50 | public function toNative(): array |
| 51 | { |
| 52 | return $this->internal; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return array<string, string> |
| 57 | */ |
| 58 | public function getRouteRequirements(): array |
| 59 | { |
| 60 | return match (count($this->internal)) { |
| 61 | 0 => [], |
| 62 | 1 => [], |
| 63 | // TODO; quote regular expression? |
| 64 | default => ['prefix' => implode('|', $this->internal)], |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | public function __toString(): string |
| 69 | { |
| 70 | switch (count($this->internal)) { |
| 71 | case 0: |
| 72 | return '/'; |
| 73 | case 1: |
| 74 | return '/' . reset($this->internal) . '/'; |
| 75 | default: |
| 76 | return '/{prefix}/'; |
| 77 | } |
| 78 | } |
| 79 | } |