4545 * @property-read string $basePath
4646 * @property-read string $baseUrl
4747 * @property-read string $relativeUrl
48+ * @property-read array $queryParameters
4849 */
4950class Url extends Nette \Object
5051{
@@ -75,8 +76,8 @@ class Url extends Nette\Object
7576 /** @var string */
7677 private $ path = '' ;
7778
78- /** @var string */
79- private $ query = '' ;
79+ /** @var array */
80+ private $ query = array () ;
8081
8182 /** @var string */
8283 private $ fragment = '' ;
@@ -101,7 +102,7 @@ public function __construct($url = NULL)
101102 $ this ->user = isset ($ p ['user ' ]) ? rawurldecode ($ p ['user ' ]) : '' ;
102103 $ this ->pass = isset ($ p ['pass ' ]) ? rawurldecode ($ p ['pass ' ]) : '' ;
103104 $ this ->setPath (isset ($ p ['path ' ]) ? $ p ['path ' ] : '' );
104- $ this ->query = isset ($ p ['query ' ]) ? self :: unescape ( $ p ['query ' ], ' %&;=+ ' ) : '' ;
105+ $ this ->setQuery ( isset ($ p ['query ' ]) ? $ p ['query ' ] : array ()) ;
105106 $ this ->fragment = isset ($ p ['fragment ' ]) ? rawurldecode ($ p ['fragment ' ]) : '' ;
106107
107108 } elseif ($ url instanceof self) {
@@ -254,7 +255,7 @@ public function getPath()
254255 */
255256 public function setQuery ($ value )
256257 {
257- $ this ->query = ( string ) ( is_array ($ value ) ? http_build_query ( $ value, '' , ' & ' ) : $ value );
258+ $ this ->query = is_array ($ value ) ? $ value : self :: parseQuery ( $ value );
258259 return $ this ;
259260 }
260261
@@ -266,8 +267,9 @@ public function setQuery($value)
266267 */
267268 public function appendQuery ($ value )
268269 {
269- $ value = (string ) (is_array ($ value ) ? http_build_query ($ value , '' , '& ' ) : $ value );
270- $ this ->query .= ($ this ->query === '' || $ value === '' ) ? $ value : '& ' . $ value ;
270+ $ this ->query = is_array ($ value )
271+ ? $ value + $ this ->query
272+ : self ::parseQuery ($ this ->getQuery () . '& ' . $ value );
271273 return $ this ;
272274 }
273275
@@ -277,6 +279,18 @@ public function appendQuery($value)
277279 * @return string
278280 */
279281 public function getQuery ()
282+ {
283+ if (PHP_VERSION < 50400 ) {
284+ return str_replace ('+ ' , '%20 ' , http_build_query ($ this ->query , '' , '& ' ));
285+ }
286+ return http_build_query ($ this ->query , '' , '& ' , PHP_QUERY_RFC3986 );
287+ }
288+
289+
290+ /**
291+ * @return array
292+ */
293+ public function getQueryParameters ()
280294 {
281295 return $ this ->query ;
282296 }
@@ -289,8 +303,7 @@ public function getQuery()
289303 */
290304 public function getQueryParameter ($ name , $ default = NULL )
291305 {
292- parse_str ($ this ->query , $ params );
293- return isset ($ params [$ name ]) ? $ params [$ name ] : $ default ;
306+ return isset ($ this ->query [$ name ]) ? $ this ->query [$ name ] : $ default ;
294307 }
295308
296309
@@ -301,13 +314,7 @@ public function getQueryParameter($name, $default = NULL)
301314 */
302315 public function setQueryParameter ($ name , $ value )
303316 {
304- parse_str ($ this ->query , $ params );
305- if ($ value === NULL ) {
306- unset($ params [$ name ]);
307- } else {
308- $ params [$ name ] = $ value ;
309- }
310- $ this ->setQuery ($ params );
317+ $ this ->query [$ name ] = $ value ;
311318 return $ this ;
312319 }
313320
@@ -341,7 +348,7 @@ public function getFragment()
341348 public function getAbsoluteUrl ()
342349 {
343350 return $ this ->getHostUrl () . $ this ->path
344- . ($ this ->query === '' ? '' : ' ? ' . $ this ->query )
351+ . ($ this ->query ? '? ' . $ this ->getQuery () : '' )
345352 . ($ this ->fragment === '' ? '' : '# ' . $ this ->fragment );
346353 }
347354
@@ -414,9 +421,9 @@ public function getRelativeUrl()
414421 public function isEqual ($ url )
415422 {
416423 $ url = new self ($ url );
417- parse_str ( $ url ->query , $ query ) ;
424+ $ query = $ url ->query ;
418425 sort ($ query );
419- parse_str ( $ this ->query , $ query2 ) ;
426+ $ query2 = $ this ->query ;
420427 sort ($ query2 );
421428 $ http = in_array ($ this ->scheme , array ('http ' , 'https ' ), TRUE );
422429 return $ url ->scheme === $ this ->scheme
@@ -442,7 +449,6 @@ function($m) { return rawurlencode($m[0]); },
442449 self ::unescape ($ this ->path , '%/ ' )
443450 );
444451 $ this ->host = strtolower ($ this ->host );
445- $ this ->query = self ::unescape (strtr ($ this ->query , '+ ' , ' ' ), '%&;=+ ' );
446452 return $ this ;
447453 }
448454
@@ -477,4 +483,18 @@ function($m) { return '%25' . strtoupper($m[1]); },
477483 return rawurldecode ($ s );
478484 }
479485
486+
487+ /**
488+ * Parses query string.
489+ * @return array
490+ */
491+ public static function parseQuery ($ s )
492+ {
493+ parse_str ($ s , $ res );
494+ if (get_magic_quotes_gpc ()) { // for PHP 5.3
495+ $ res = Helpers::stripSlashes ($ res );
496+ }
497+ return $ res ;
498+ }
499+
480500}
0 commit comments