QueryString
extends Collection
in package
Query string object to handle managing query string parameters and aggregating those parameters together as a string.
Table of Contents
Constants
- BLANK = "_guzzle_blank_"
- FORM_URLENCODED = 'application/x-www-form-urlencoded'
- RFC_3986 = 'RFC 3986'
Properties
- $aggregator : QueryAggregatorInterface
- $data : array<string|int, mixed>
- $fieldSeparator : string
- $urlEncode : bool
- $valueSeparator : string
- $defaultAggregator : array<string|int, mixed>
Methods
- __construct() : mixed
- __toString() : string
- Convert the query string parameters to a query string string
- add() : Collection
- Add a value to a key. If a key of the same name has already been added, the key value will be converted into an array and the new value will be pushed to the end of the array.
- clear() : Collection
- Removes all key value pairs
- count() : mixed
- encodeValue() : string
- URL encodes a value based on the url encoding type of the query string object
- filter() : Collection
- Iterates over each key value pair in the collection passing them to the Closure. If the Closure function returns true, the current value from input is returned into the result Collection. The Closure must accept three parameters: (string) $key, (string) $value and return Boolean TRUE or FALSE for each value.
- fromConfig() : self
- Create a new collection from an array, validate the keys, and add default values where missing
- fromString() : self
- Parse a query string into a QueryString object
- get() : mixed|null
- Get a specific key value.
- getAll() : array<string|int, mixed>
- Get all or a subset of matching key value pairs
- getFieldSeparator() : string
- Get the query string field separator
- getIterator() : mixed
- getKeys() : array<string|int, mixed>
- Get all keys in the collection
- getPath() : mixed|null
- Gets a value from the collection using an array path (e.g. foo/baz/bar would retrieve bar from two nested arrays) Allows for wildcard searches which recursively combine matches up to the level at which the wildcard occurs. This can be useful for accepting any key of a sub-array and combining matching keys from each diverging path.
- getUrlEncoding() : bool|string
- Returns the type of URL encoding used by the query string
- getValueSeparator() : string
- Get the query string value separator
- hasKey() : bool
- Returns whether or not the specified key is present.
- hasValue() : mixed
- Checks if any keys contains a certain value
- inject() : string
- Inject configuration settings into an input string
- isUrlEncoding() : bool
- Returns true or false if using URL encoding
- keySearch() : bool|string
- Case insensitive search the keys in the collection
- map() : Collection
- Returns a Collection containing all the elements of the collection after applying the callback function to each one. The Closure should accept three parameters: (string) $key, (string) $value, (array) $context and return a modified value
- merge() : Collection
- Add and merge in a Collection or array of key value pair data.
- offsetExists() : mixed
- offsetGet() : mixed
- offsetSet() : mixed
- offsetUnset() : mixed
- overwriteWith() : self
- Over write key value pairs in this collection with all of the data from an array or collection.
- remove() : Collection
- Remove a specific key value pair
- replace() : Collection
- Replace the data of the object with the value of an array
- set() : Collection
- Set a key value pair
- setAggregator() : self
- Provide a function for combining multi-valued query string parameters into a single or multiple fields
- setFieldSeparator() : self
- Set the query string separator
- setPath() : self
- Set a value into a nested array key. Keys will be created as needed to set the value.
- setValueSeparator() : self
- Set the query string value separator
- toArray() : array<string|int, mixed>
- Get the array representation of an object
- urlEncode() : array<string|int, mixed>
- Returns an array of url encoded field names and values
- useUrlEncoding() : self
- Set whether or not field names and values should be rawurlencoded
- prepareData() : array<string|int, mixed>
- Url encode parameter data and convert nested query strings into a flattened hash.
- convertKvp() : string
- Converts a key value pair that can contain strings, nulls, false, or arrays into a single string.
Constants
BLANK
public
string
BLANK
= "_guzzle_blank_"
Constant used to create blank query string values (e.g. ?foo)
FORM_URLENCODED
public
string
FORM_URLENCODED
= 'application/x-www-form-urlencoded'
Used to encode with urlencode
RFC_3986
public
string
RFC_3986
= 'RFC 3986'
Used to URL encode with rawurlencode
Properties
$aggregator
protected
QueryAggregatorInterface
$aggregator
$data
protected
array<string|int, mixed>
$data
Data associated with the object.
$fieldSeparator
protected
string
$fieldSeparator
= '&'
The query string field separator (e.g. '&')
$urlEncode
protected
bool
$urlEncode
= 'RFC 3986'
URL encode fields and values
$valueSeparator
protected
string
$valueSeparator
= '='
The query string value separator (e.g. '=')
$defaultAggregator
private
static array<string|int, mixed>
$defaultAggregator
= null
Cached PHP aggregator
Methods
__construct()
public
__construct([array<string|int, mixed> $data = array() ]) : mixed
Parameters
- $data : array<string|int, mixed> = array()
-
Associative array of data to set
__toString()
Convert the query string parameters to a query string string
public
__toString() : string
Tags
Return values
stringadd()
Add a value to a key. If a key of the same name has already been added, the key value will be converted into an array and the new value will be pushed to the end of the array.
public
add(string $key, mixed $value) : Collection
Parameters
- $key : string
-
Key to add
- $value : mixed
-
Value to add to the key
Return values
Collection —Returns a reference to the object.
clear()
Removes all key value pairs
public
clear() : Collection
Return values
Collectioncount()
public
count() : mixed
Attributes
- #[ReturnTypeWillChange]
encodeValue()
URL encodes a value based on the url encoding type of the query string object
public
encodeValue(string $value) : string
Parameters
- $value : string
-
Value to encode
Return values
stringfilter()
Iterates over each key value pair in the collection passing them to the Closure. If the Closure function returns true, the current value from input is returned into the result Collection. The Closure must accept three parameters: (string) $key, (string) $value and return Boolean TRUE or FALSE for each value.
public
filter(Closure $closure[, bool $static = true ]) : Collection
Parameters
- $closure : Closure
-
Closure evaluation function
- $static : bool = true
-
Set to TRUE to use the same class as the return rather than returning a Collection
Return values
CollectionfromConfig()
Create a new collection from an array, validate the keys, and add default values where missing
public
static fromConfig([array<string|int, mixed> $config = array() ][, array<string|int, mixed> $defaults = array() ][, array<string|int, mixed> $required = array() ]) : self
Parameters
- $config : array<string|int, mixed> = array()
-
Configuration values to apply.
- $defaults : array<string|int, mixed> = array()
-
Default parameters
- $required : array<string|int, mixed> = array()
-
Required parameter names
Tags
Return values
selffromString()
Parse a query string into a QueryString object
public
static fromString(string $query) : self
Parameters
- $query : string
-
Query string to parse
Return values
selfget()
Get a specific key value.
public
get(string $key) : mixed|null
Parameters
- $key : string
-
Key to retrieve.
Return values
mixed|null —Value of the key or NULL
getAll()
Get all or a subset of matching key value pairs
public
getAll([array<string|int, mixed> $keys = null ]) : array<string|int, mixed>
Parameters
- $keys : array<string|int, mixed> = null
-
Pass an array of keys to retrieve only a subset of key value pairs
Return values
array<string|int, mixed> —Returns an array of all matching key value pairs
getFieldSeparator()
Get the query string field separator
public
getFieldSeparator() : string
Return values
stringgetIterator()
public
getIterator() : mixed
Attributes
- #[ReturnTypeWillChange]
getKeys()
Get all keys in the collection
public
getKeys() : array<string|int, mixed>
Return values
array<string|int, mixed>getPath()
Gets a value from the collection using an array path (e.g. foo/baz/bar would retrieve bar from two nested arrays) Allows for wildcard searches which recursively combine matches up to the level at which the wildcard occurs. This can be useful for accepting any key of a sub-array and combining matching keys from each diverging path.
public
getPath(string $path[, string $separator = '/' ][, mixed $data = null ]) : mixed|null
Parameters
- $path : string
-
Path to traverse and retrieve a value from
- $separator : string = '/'
-
Character used to add depth to the search
- $data : mixed = null
-
Optional data to descend into (used when wildcards are encountered)
Return values
mixed|nullgetUrlEncoding()
Returns the type of URL encoding used by the query string
public
getUrlEncoding() : bool|string
One of: false, "RFC 3986", or "application/x-www-form-urlencoded"
Return values
bool|stringgetValueSeparator()
Get the query string value separator
public
getValueSeparator() : string
Return values
stringhasKey()
Returns whether or not the specified key is present.
public
hasKey(string $key) : bool
Parameters
- $key : string
-
The key for which to check the existence.
Return values
boolhasValue()
Checks if any keys contains a certain value
public
hasValue(string $value) : mixed
Parameters
- $value : string
-
Value to search for
Return values
mixed —Returns the key if the value was found FALSE if the value was not found.
inject()
Inject configuration settings into an input string
public
inject(string $input) : string
Parameters
- $input : string
-
Input to inject
Return values
stringisUrlEncoding()
Returns true or false if using URL encoding
public
isUrlEncoding() : bool
Return values
boolkeySearch()
Case insensitive search the keys in the collection
public
keySearch(string $key) : bool|string
Parameters
- $key : string
-
Key to search for
Return values
bool|string —Returns false if not found, otherwise returns the key
map()
Returns a Collection containing all the elements of the collection after applying the callback function to each one. The Closure should accept three parameters: (string) $key, (string) $value, (array) $context and return a modified value
public
map(Closure $closure[, array<string|int, mixed> $context = array() ][, bool $static = true ]) : Collection
Parameters
- $closure : Closure
-
Closure to apply
- $context : array<string|int, mixed> = array()
-
Context to pass to the closure
- $static : bool = true
-
Set to TRUE to use the same class as the return rather than returning a Collection
Return values
Collectionmerge()
Add and merge in a Collection or array of key value pair data.
public
merge(Collection|array<string|int, mixed> $data) : Collection
Parameters
- $data : Collection|array<string|int, mixed>
-
Associative array of key value pair data
Return values
Collection —Returns a reference to the object.
offsetExists()
public
offsetExists(mixed $offset) : mixed
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
offsetGet()
public
offsetGet(mixed $offset) : mixed
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
offsetSet()
public
offsetSet(mixed $offset, mixed $value) : mixed
Parameters
- $offset : mixed
- $value : mixed
Attributes
- #[ReturnTypeWillChange]
offsetUnset()
public
offsetUnset(mixed $offset) : mixed
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
overwriteWith()
Over write key value pairs in this collection with all of the data from an array or collection.
public
overwriteWith(array<string|int, mixed>|Traversable $data) : self
Parameters
- $data : array<string|int, mixed>|Traversable
-
Values to override over this config
Return values
selfremove()
Remove a specific key value pair
public
remove(string $key) : Collection
Parameters
- $key : string
-
A key to remove
Return values
Collectionreplace()
Replace the data of the object with the value of an array
public
replace(array<string|int, mixed> $data) : Collection
Parameters
- $data : array<string|int, mixed>
-
Associative array of data
Return values
Collection —Returns a reference to the object
set()
Set a key value pair
public
set(string $key, mixed $value) : Collection
Parameters
- $key : string
-
Key to set
- $value : mixed
-
Value to set
Return values
Collection —Returns a reference to the object
setAggregator()
Provide a function for combining multi-valued query string parameters into a single or multiple fields
public
setAggregator([null|QueryAggregatorInterface $aggregator = null ]) : self
Parameters
- $aggregator : null|QueryAggregatorInterface = null
-
Pass in a QueryAggregatorInterface object to handle converting deeply nested query string variables into a flattened array. Pass null to use the default PHP style aggregator. For legacy reasons, this function accepts a callable that must accepts a $key, $value, and query object.
Tags
Return values
selfsetFieldSeparator()
Set the query string separator
public
setFieldSeparator(string $separator) : self
Parameters
- $separator : string
-
The query string separator that will separate fields
Return values
selfsetPath()
Set a value into a nested array key. Keys will be created as needed to set the value.
public
setPath(string $path, mixed $value) : self
Parameters
- $path : string
-
Path to set
- $value : mixed
-
Value to set at the key
Tags
Return values
selfsetValueSeparator()
Set the query string value separator
public
setValueSeparator(string $separator) : self
Parameters
- $separator : string
-
The query string separator that will separate values from fields
Return values
selftoArray()
Get the array representation of an object
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>urlEncode()
Returns an array of url encoded field names and values
public
urlEncode() : array<string|int, mixed>
Return values
array<string|int, mixed>useUrlEncoding()
Set whether or not field names and values should be rawurlencoded
public
useUrlEncoding(bool|string $encode) : self
Parameters
- $encode : bool|string
-
Set to TRUE to use RFC 3986 encoding (rawurlencode), false to disable encoding, or form_urlencoding to use application/x-www-form-urlencoded encoding (urlencode)
Return values
selfprepareData()
Url encode parameter data and convert nested query strings into a flattened hash.
protected
prepareData(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
The data to encode
Return values
array<string|int, mixed> —Returns an array of encoded values and keys
convertKvp()
Converts a key value pair that can contain strings, nulls, false, or arrays into a single string.
private
convertKvp(string $name, mixed $value) : string
Parameters
- $name : string
-
Name of the field
- $value : mixed
-
Value of the field