| Total Complexity | 9 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | class Uri implements Stringable |
||
| 9 | { |
||
| 10 | private readonly string $uri; |
||
| 11 | |||
| 12 | 15 | public function __construct(mixed $value) |
|
| 13 | { |
||
| 14 | 15 | if (\is_array($value)) { |
|
| 15 | 4 | if (!\is_string($value['base'])) { |
|
| 16 | 1 | throw new InvalidArgumentException( |
|
| 17 | 1 | 'If you want to build an URI from an array, use the schema: [ base => string, ?parameters => [string => mixed]' |
|
| 18 | 1 | ); |
|
| 19 | } |
||
| 20 | |||
| 21 | 3 | // Check if parameters is a valid string[] |
|
| 22 | 3 | $parameters = $value['parameters'] ?? []; |
|
| 23 | 11 | if (!\is_array($parameters)) { |
|
| 24 | 4 | throw new InvalidArgumentException('value parameters must be an array.'); |
|
| 25 | } |
||
| 26 | 10 | ||
| 27 | $search = []; |
||
| 28 | $replace = []; |
||
| 29 | 10 | foreach ($parameters as $name => $parameter) { |
|
| 30 | if (!\is_string($name) || !\is_string($parameter)) { |
||
| 31 | 10 | throw new InvalidArgumentException(\sprintf( |
|
| 32 | 'Invalid parameter given {name: %s, parameter: %s}', |
||
| 33 | var_export($name, true), |
||
| 34 | var_export($parameter, true) |
||
| 35 | )); |
||
| 36 | } |
||
| 37 | $search[] = $name; |
||
| 38 | $replace[] = $parameter; |
||
| 39 | } |
||
| 40 | |||
| 41 | $this->uri = str_replace( |
||
|
|
|||
| 42 | $search, |
||
| 43 | $replace, |
||
| 44 | (string) $value['base'] |
||
| 45 | ); |
||
| 46 | } elseif (\is_string($value)) { |
||
| 47 | $this->uri = $value; |
||
| 48 | } else { |
||
| 49 | throw new InvalidArgumentException('$value must be a string or an array.'); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public function __toString(): string |
||
| 56 | } |
||
| 57 | } |
||
| 58 |