Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
15 / 18 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| PossibleRoutePrefixes | |
83.33% |
15 / 18 |
|
60.00% |
3 / 5 |
13.78 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| toNative | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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\ValueObjects\Interfaces\ValueObjectInterface; |
| 5 | use Apie\Core\ValueObjects\Utils; |
| 6 | use Stringable; |
| 7 | |
| 8 | final class PossibleRoutePrefixes implements ValueObjectInterface, Stringable |
| 9 | { |
| 10 | /** |
| 11 | * @param array<int, string> $internal |
| 12 | */ |
| 13 | private function __construct( |
| 14 | private array $internal |
| 15 | ) { |
| 16 | } |
| 17 | /** |
| 18 | * @return static |
| 19 | */ |
| 20 | public static function fromNative(mixed $input): self |
| 21 | { |
| 22 | if ($input instanceof ValueObjectInterface) { |
| 23 | $input = $input->toNative(); |
| 24 | } |
| 25 | if (is_string($input)) { |
| 26 | return new PossibleRoutePrefixes([$input]); |
| 27 | } |
| 28 | return new PossibleRoutePrefixes(Utils::toArray($input)); |
| 29 | } |
| 30 | /** |
| 31 | * @return array<string|int, mixed> |
| 32 | */ |
| 33 | public function toNative(): array |
| 34 | { |
| 35 | return $this->internal; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return array<string, string> |
| 40 | */ |
| 41 | public function getRouteRequirements(): array |
| 42 | { |
| 43 | return match (count($this->internal)) { |
| 44 | 0 => [], |
| 45 | 1 => [], |
| 46 | // TODO; quote regular expression? |
| 47 | default => ['prefix' => implode('|', $this->internal)], |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | public function __toString(): string |
| 52 | { |
| 53 | switch (count($this->internal)) { |
| 54 | case 0: |
| 55 | return '/'; |
| 56 | case 1: |
| 57 | return '/' . reset($this->internal) . '/'; |
| 58 | default: |
| 59 | return '/{prefix}/'; |
| 60 | } |
| 61 | } |
| 62 | } |