Instantiation and Access
Instantiation
To get an instance of the Query
class you can use the static fromString
and fromArray
methods:
$query = Query::fromString('foo=bar&baz=quz');
$query = Query::fromArray(['foo' => 'bar', 'baz' => 'quz']);
You can also just instantiate it with new Query('foo=bar')
(also accepts array input), but using the static methods will look better in many situations:
$fooValue = Query::fromString('foo=bar&baz=quz')->get('foo');
vs.
$query = new Query('foo=bar&baz=quz');
$fooValue = $query->get('foo');
or
$fooValue = (new Query('foo=bar&baz=quz'))->get('foo');
Access
Accessing keys
get()
To get the value for a certain key, use the get()
method.
$baz = Query::fromString('foo=bar&baz=quz')->get('baz'); // string(3) "quz"
When the requested key is an array, it will return a new instance of the Query
class:
$fooBazValue = Query::fromString('foo[bar]=1&foo[baz]=2&foo[quz]=3')
->get('foo')
->get('baz');
// string(1) "2"
has()
When parsing variable query strings, you may want to check if a query contains a certain key:
$input1 = 'foo=bar&baz=quz';
$input2 = 'page[number]=3&page[size]=25';
Query::fromString($input1)->has('foo'); // bool(true)
Query::fromString($input2)->has('foo'); // bool(false)
first()
For simple indexed arrays you might want to get the first element:
$firstFooElement = Query::fromString('foo[]=1&foo[]=2&foo[]=3')
->first('foo');
// string(1) "1"
Can also be used without providing a key:
$firstFooElement = Query::fromString('foo[]=bar&foo[]=baz&foo[]=quz')
->get('foo')
->first();
// string(3) "bar"
last()
And of course there is also a last()
method:
$lastFooElement = Query::fromString('foo[]=1&foo[]=2&foo[]=3')
->last('foo');
// string(1) "3"
Like first()
it can also be called without a key:
$lastFooElement = Query::fromString('foo[]=bar&foo[]=baz&foo[]=quz')
->get('foo')
->last();
string(3) "quz"
isArray()
As the get()
method returns an instance of the Query
class when the requested key is an array and otherwise just the scalar value, there might be situations where you want to check if a certain key is an array:
$input1 = 'foo[]=1&foo[]=2';
$input2 = 'foo=bar&baz=quz';
Query::fromString($input1)->isArray('foo'); // bool(true)
Query::fromString($input2)->isArray('foo'); // bool(false)
isScalar()
You can also check if something is a scalar value (basically means not an array here).
$input1 = 'foo[]=1&foo[]=2';
$input2 = 'foo=bar&baz=quz';
Query::fromString($input1)->isScalar('foo'); // bool(false)
Query::fromString($input2)->isScalar('foo'); // bool(true)
Getting the Whole Query
As String => toString()
$queryString = Query::fromArray(['page' => ['number' => 3, 'size' => 25]])
->toString();
// string(36) "page%5Bnumber%5D=3&page%5Bsize%5D=25"
The toString()
method encodes characters as they should be encoded for the usage in URLs. As you can see also the bracket characters are encoded which makes it pretty unreadable for humans. If you just want to output a query string and have it in a more readable form, you can use the toStringWithUnencodedBrackets()
method:
$queryString = Query::fromArray(['page' => ['number' => 3, 'size' => 25]])
->toStringWithUnencodedBrackets();
string(28) "page[number]=3&page[size]=25"
The Query
class also has a __toString()
method, so you can also cast a Query
object to a string or pass it to a function as argument with type string
.
$queryString = (string) Query::fromArray(['page' => ['number' => 3, 'size' => 25]]);
string(36) "page%5Bnumber%5D=3&page%5Bsize%5D=25"
As Array => toArray()
$queryArray = Query::fromString('page[number]=5&page[size]=30')
->toArray();
// array(1) {
// ["page"]=>
// array(2) {
// ["number"]=>
// string(1) "5"
// ["size"]=>
// string(2) "30"
// }
// }