$parameters
$parameters :
Represents parameters with string values, where each parameter can have one or multiple values (like URL query parameters). For basic functions it can be used as an array, but also provides add() methods to add subsequent values to a key. So it really is just an array with multiple values per key. The class also comes with a few convenience functions to convert to and from other parameter representations (like query strings or HTTP headers).
Concept, interface and usage are partly inspired by .NET's NameValueCollection.
TODO: Implement Iterator or IteratorAggregate interface to allow use of Parameter objects in foreach(). Or maybe just extend ArrayIterator.
__construct(mixed $parameters = null, boolean $java = false)
Optionally takes a URL query string or an array of initial parameters to populate the object with.
In the case of an array this is just a convenience over creating an empty object and setting the parameters manually with ->setAll($parameters).
| mixed | $parameters | Either a URL query string or an array of parameters to initialize the object with. |
| boolean | $java | If $parameters is a string and this is True, the query string will be interpreted as one originating from Java instead of PHP. |
offsetSet(string $name, mixed $value)
Sets a parameter with one or multiple values.
| string | $name | The parameter name. |
| mixed | $value | A string or an array of strings. Nested arrays are not allowed. All other types are cast to strings. |
if no name is given or the value is a nested array.
getArray() : array<mixed,mixed>
Returns a reference to the object's internal array used to store all parameters. This can be used to sort the parameters from the outside.
TODO: Provide access to relevant functions instead. Just handing out the array is kind of dangerous and also breaks all encapsulation.
An array of all parameters. All values are either strings or arrays of strings.
setAll(array<mixed,mixed> $parameters)
Set all given parameters. Previous values of the given parameters will be replaced. Unmentioned parameters will retain their values.
| array<mixed,mixed> | $parameters | An array of parameters. The keys are parameter names, the values are like those you would pass to offsetSet(). |
set(string $name, mixed $value)
Alias for $parameters['name'] = 'value' notation, because offset indexing cannot be chained with method calls.
| string | $name | The parameter name. |
| mixed | $value | A string or an array of strings. Nested arrays are not allowed. All other types are cast to strings. |
if no name is given or the value is a nested array.
add(string $name, mixed $value)
Adds a parameter value. As opposed to offsetSet() existing values will not be replaced.
| string | $name | The parameter name. |
| mixed | $value | A string or an array of strings. Nested arrays are not allowed. All other types are cast to strings. |
if no name is given or the value is a nested array.
toPhpQueryString() : string
Returns a URL query string based on the set parameters for use in a request to a PHP server. That is, PHP's built-in functions will correctly recreate the structure of this object's internal array.
Note however, that PHP will replace all non-letter, non-digit characters in parameter names with underscores, when retrieving GET parameters. WARNING: If the parameter object contains the empty string as one of its keys, the corresponding key-value pair(s) will be part of the query string. However, PHP's built-in functions will not be able retrieve those values (this includes $_GET and parse_str()). However, if you create a new Parameters object from such a query string, the empty parameter names will be correctly reconstructed.
The query string.
toJavaQueryString() : string
Returns a URL query string based on the set parameters for use in a request to a Java server. The difference to a PHP query string is that Java does not need and cannot deal with square brackets in parameter names. Hence, parameters with multiple names will simply be repeated.
The query string.
sanitizeValue(mixed $value) : mixed
Makes sure that the given value is not a nested array. It also converts all non-strings to strings. Lastly, single-element arrays are converted to string values.
| mixed | $value | The value to be sanitized. |
The sanitized value.