@@ -12,18 +12,42 @@ class Uri implements Stringable
1212 public function __construct (mixed $ value )
1313 {
1414 if (\is_array ($ value )) {
15- if (!isset ($ value ['base ' ])) {
15+ if (!\is_string ($ value ['base ' ])) {
1616 throw new InvalidArgumentException (
1717 'If you want to build an URI from an array, use the schema: [ base => string, ?parameters => [string => mixed] '
1818 );
1919 }
2020
21+ // Check if parameters is a valid string[]
2122 $ parameters = $ value ['parameters ' ] ?? [];
22- $ value = str_replace (array_keys ($ parameters ), $ parameters , (string ) $ value ['base ' ]);
23- } elseif (!\is_string ($ value )) {
23+ if (!\is_array ($ parameters )) {
24+ throw new InvalidArgumentException ('value parameters must be an array. ' );
25+ }
26+
27+ $ search = [];
28+ $ replace = [];
29+ foreach ($ parameters as $ name => $ parameter ) {
30+ if (!\is_string ($ name ) || !\is_string ($ parameter )) {
31+ 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 {
2449 throw new InvalidArgumentException ('$value must be a string or an array. ' );
2550 }
26- $ this ->uri = $ value ;
2751 }
2852
2953 public function __toString (): string
0 commit comments