@@ -59,18 +59,8 @@ public static function fromGlobals()
5959 */
6060 public static function fromParsed (array $ parsed )
6161 {
62- $ uriKeys = ['fragment ' , 'host ' , 'path ' , 'port ' , 'query ' , 'scheme ' , 'userInfo ' ];
63- $ parsed = \array_intersect_key (self ::parsedPartsPrep ($ parsed ), \array_flip ($ uriKeys ));
64- $ parsed = \array_filter ($ parsed , static function ($ val ) {
65- return \in_array ($ val , [null , '' ], true ) === false ;
66- });
6762 $ uri = new BdkUri ();
68- foreach ($ parsed as $ key => $ value ) {
69- $ method = 'with ' . \ucfirst ($ key );
70- /** @var BdkUri */
71- $ uri = \call_user_func_array ([$ uri , $ method ], (array ) $ value );
72- }
73- return $ uri ;
63+ return self ::withParsedValues ($ uri , $ parsed );
7464 }
7565
7666 /**
@@ -136,29 +126,57 @@ public static function resolve(UriInterface $base, UriInterface $rel)
136126 return $ base ;
137127 }
138128 if ($ rel ->getScheme () !== '' ) {
139- // rel specified scheme... return rel (with path cleaned up)
140- return $ rel ->withPath (self ::pathRemoveDots ($ rel ->getPath ()));
129+ // rel specified scheme
130+ // return rel (with path cleaned up)
131+ return $ rel
132+ ->withPath (self ::pathRemoveDots ($ rel ->getPath ()));
141133 }
134+ $ targetValues = array (
135+ 'fragment ' => $ rel ->getFragment (),
136+ );
142137 if ($ rel ->getAuthority () !== '' ) {
143- // rel specified "authority"..
138+ // rel specified "authority"
144139 // return base's scheme, rel's everything else (with path cleaned up)
145- return $ rel
146- ->withScheme ($ base ->getScheme ())
147- ->withPath (self ::pathRemoveDots ($ rel ->getPath ()));
140+ $ targetValues ['userInfo ' ] = $ rel ->getUserInfo ();
141+ $ targetValues ['host ' ] = $ rel ->getHost ();
142+ $ targetValues ['port ' ] = $ rel ->getPort ();
143+ $ targetValues ['path ' ] = self ::pathRemoveDots ($ rel ->getPath ());
144+ $ targetValues ['query ' ] = $ rel ->getQuery ();
145+ } elseif ($ rel ->getPath () !== '' ) {
146+ // rel specified path
147+ // return base with resolved path (cleaned up), rel's query & fragment
148+ $ targetValues ['path ' ] = self ::pathRemoveDots (self ::resolveTargetPath ($ base , $ rel ));
149+ $ targetValues ['query ' ] = $ rel ->getQuery ();
150+ } elseif ($ rel ->getQuery () !== '' ) {
151+ // rel specified query
152+ $ targetValues ['query ' ] = $ rel ->getQuery ();
148153 }
149- if ($ rel ->getPath () === '' ) {
150- $ targetQuery = $ rel ->getQuery () !== ''
151- ? $ rel ->getQuery ()
152- : $ base ->getQuery ();
153- return $ base
154- ->withQuery ($ targetQuery )
155- ->withFragment ($ rel ->getFragment ());
154+ return self ::withParsedValues ($ base , $ targetValues );
155+ }
156+
157+ /**
158+ * Apply component values to a Uri
159+ *
160+ * @param UriInterface $uri UriInterface instance
161+ * @param array $parsed Component values
162+ *
163+ * @return UriInterface
164+ *
165+ * @since x3.4
166+ */
167+ public static function withParsedValues (UriInterface $ uri , array $ values ): UriInterface
168+ {
169+ $ uriKeys = ['fragment ' , 'host ' , 'path ' , 'port ' , 'query ' , 'scheme ' , 'userInfo ' ];
170+ $ values = \array_intersect_key (self ::parsedPartsPrep ($ values ), \array_flip ($ uriKeys ));
171+ foreach ($ values as $ key => $ value ) {
172+ $ method = 'with ' . \ucfirst ($ key );
173+ // using call_user_func_array... some methods (withUserInfo) accept multiple arguments
174+ $ args = $ value === null
175+ ? array (null )
176+ : (array ) $ value ;
177+ $ uri = \call_user_func_array ([$ uri , $ method ], $ args );
156178 }
157- $ targetPath = self ::resolveTargetPath ($ base , $ rel );
158- return $ base
159- ->withPath (self ::pathRemoveDots ($ targetPath ))
160- ->withQuery ($ rel ->getQuery ())
161- ->withFragment ($ rel ->getFragment ());
179+ return $ uri ;
162180 }
163181
164182 /**
@@ -187,6 +205,7 @@ private static function computePort(UriInterface $uri)
187205 private static function parsedPartsPrep (array $ parsed )
188206 {
189207 $ map = array (
208+ 'passwd ' => 'pass ' ,
190209 'password ' => 'pass ' ,
191210 'username ' => 'user ' ,
192211 );
@@ -358,16 +377,16 @@ private static function uriInterfaceToParts(UriInterface $url)
358377 }
359378
360379 /**
361- * Get host and port from `$_SERVER` vals
380+ * Get host and port from `$_SERVER` values
362381 *
363- * @return array{host:string|null ,port:int|null} host & port
382+ * @return array{host:string,port:int|null} host & port
364383 *
365384 * @SuppressWarnings(PHPMD.Superglobals)
366385 */
367386 private static function hostPortFromGlobals ()
368387 {
369388 $ hostPort = array (
370- 'host ' => null ,
389+ 'host ' => '' ,
371390 'port ' => null ,
372391 );
373392 if (isset ($ _SERVER ['HTTP_HOST ' ])) {
0 commit comments