thephpleague / uri-src

URI manipulation Library
https://uri.thephpleague.com
MIT License
27 stars 8 forks source link

Query::createFromPairs does not work with key value array #106

Closed sebui closed 1 year ago

sebui commented 1 year ago

Bug Report

Information Description
Package league/uri-components
Version ^2
PHP version >=7
OS Platform Linux

Summary

The example on the documentation does not work (https://uri.thephpleague.com/components/2.0/query). It is not clear what kind of array we should pass to the function. [key => value, key2 => value2 ...] or [[key, value], [key, value]] The first form is more convenient, but does not work.

Standalone code, or other way to reproduce the problem

<?php
require_once 'vendor/autoload.php';

use League\Uri\Components\Query;

$query =  Query::createFromPairs([
    'foo' => 'bar',
    'p' => 'yolo',
    'z' => ''
]);
echo $query; 

Expected result

//display 'foo=bar&p=yolo&z='

Actual result

PHP Fatal error:  Uncaught TypeError: League\Uri\QueryString::buildPair(): Argument #1 ($pair) must be of type array, string given, called in /tmp/testuri/vendor/league/uri-components/src/QueryString.php on line 287 and defined in /tmp/testuri/vendor/league/uri-components/src/QueryString.php:308
Stack trace:
#0 /tmp/testuri/vendor/league/uri-components/src/QueryString.php(287): League\Uri\QueryString::buildPair()
#1 /tmp/testuri/vendor/league/uri-components/src/Components/Query.php(127): League\Uri\QueryString::build()
#2 /tmp/testuri/main.php(7): League\Uri\Components\Query::createFromPairs()
#3 {main}
  thrown in /tmp/testuri/vendor/league/uri-components/src/QueryString.php on line 308
nyamsprod commented 1 year ago

Correct there's a mistake in the documentation it should have been instead:

<?php
require_once 'vendor/autoload.php';

use League\Uri\Components\Query;

$query =  Query::createFromPairs([
    ['foo', 'bar'],
    ['p', 'yolo'],
    ['z', ''],
]);
echo $query; 

I will try to fix it ASAP or you can already submit a PR with the fix.

nyamsprod commented 1 year ago

docs have been fixed