Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
13 / 18
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PossibleRoutePrefixes
72.22% covered (warning)
72.22%
13 / 18
20.00% covered (danger)
20.00%
1 / 5
16.62
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromNative
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 toNative
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRouteRequirements
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 __toString
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
1<?php
2namespace Apie\Common\ValueObjects;
3
4use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface;
5use Apie\Core\ValueObjects\Utils;
6use Stringable;
7
8final 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}