diff --git a/docs/5.0/components/data-path.md b/docs/5.0/components/data-path.md index 8c411ed4..d71fd49b 100644 --- a/docs/5.0/components/data-path.md +++ b/docs/5.0/components/data-path.md @@ -16,8 +16,8 @@ but also provide specific methods to work with Data URI paths. submitted string is normalized to be RFC3986 compliant.

+### The default constructor + +~~~php +The optional $separator argument was added in version 1.3.0

+

submitted string is normalized to be RFC3986 compliant.

If the submitted value is not valid a League\Uri\Components\Exception exception is thrown.

The `League\Uri\Components\Exception` extends PHP's SPL `InvalidArgumentException`. +### Query::withSeparator + +`Query::withSeparator` returns a new `Query` object with an alternate string separator. + +~~~php +withSeparator('|'); +$newQuery->__toString(); //return foo=bar|baz=toto +~~~ + +### Query::merge + +`Query::merge` returns a new `Query` object with its data merged. + +~~~php +merge('foo=jane&r=stone'); +$newQuery->__toString(); //return foo=jane&baz=toto&r=stone +// the 'foo' parameter was updated +// the 'r' parameter was added +~~~ + +

Values equal to null or the empty string are merge differently.

+ +~~~php + 'bar', 'baz' => 'toto']); +$newQuery = $query->merge('baz=&r'); +$newQuery->__toString(); //return foo=bar&baz=&r +// the 'r' parameter was added without any value +// the 'baz' parameter was updated to an empty string and its = sign remains +~~~ + +### Query::append + +`Query::append` returns a new `Query` object with its data append to it. + +~~~php +append('foo=baz'); +$newQuery->__toString(); //return foo=jane&foo=baz&john=doe +// a new foo parameter is added +~~~ + +### Query::ksort + +`Query::ksort` returns a `Query` object with its pairs sorted according to its keys or a user defined function. + +The single argument `sort` can be: + +One of PHP's sorting constant used by the [sort function](http://php.net/sort). **In this case the query parameters are sorted from low to high** like PHP's [ksort function](http://php.net/ksort) + +~~~php +ksort(SORT_STRING); +$newQuery->__toString(); //return baz=toto&foo=bar +~~~ + +A user-defined comparison function which must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second, like PHP's [uksort function](http://php.net/uksort) + +~~~php +ksort('strcmp'); +$newQuery->__toString(); //return baz=toto&foo=bar +~~~ + ## Query as a PHP data transport layer ### Query::extract @@ -108,6 +237,23 @@ $arr = Uri\extract_query($query_string); // $arr = ['foo.bar' => 'bar', 'foo_bar' => baz']; ~~~ +### Query::createFromParams + +

Query::createFromParams is available since version 1.3.0

+ +This named constructor takes any iterable construct and tries to recreate a `Query` object using internally `http_build_query`. This method takes 2 arguments: + +- `$params` : a iterable containing properties to transform into a query string; +- `$separator`: the separator to be used when creating the query string representation; + +~~~php + 'bar', 'filter' => ['status' => 'on', 'order' => 'desc']], '&'); +echo $query->getContent(Query::NO_ENCODING); //return 'foo=bar&filter[status]=on&filter[order]=desc'; +~~~ ### Query::getParams @@ -143,6 +289,44 @@ $query->getParam('gweta', 'now'); //return 'now' The method returns the value of a specific argument. If the argument does not exist it will return the value specified by the second argument which defaults to `null`. + +### Query::withoutParams + +

Query::withoutParams is available since version 1.3.0

+ +If you want to remove PHP's variable from the query string you can use the `Query::withoutParams` method as shown below + +~~~php +withoutParams(['foo']); +$new_query->getParam('foo'); //return null +echo $new_query->getContent(Query::NO_ENCODING); //return 'z=' +~~~ + +### Query::withoutNumericIndices + +

Query::withoutNumericIndices is available since version 1.3.0

+ +If your query string is created with `http_build_query` or the `Query::createFromParams` named constructor chances are that numeric indices have been added by the method. The `Query::withoutNumericIndices` removes any numeric index found in the query string as shown below: + +~~~php +getContent(Query::NO_ENCODING); //return 'foo[0]=bar&foo[1]=baz' +$new_query = $query->withoutNumericIndices(); +echo $new_query->getContent(Query::NO_ENCODING); //return 'foo[]=bar&foo[]=baz' +//of note both objects returns the same PHP's variables but differs regarding the pairs +$query->getParams(); //return ['foo' => ['bar', 'baz']] +$new_query->getParams(); //return ['foo' => ['bar', 'baz']] +~~~ + ## Query as a collection of key/value pairs ~~~php @@ -150,13 +334,14 @@ The method returns the value of a specific argument. If the argument does not ex public static Query::parse(string $query_string [, string $separator = '&' [, int $enc_type = self::RFC3986_ENCODING]]): array public static Query::build(array $pairs [, string $separator = '&' [, int $enc_type = self::RFC3986_ENCODING]]): string -public static Query::createFromPairs(iterable $pairs): self +public static Query::createFromPairs(iterable $pairs[, string $separator = '&']): self public Query::getIterator(): ArrayIterator public Query::count(): int public Query::getPairs(): array public Query::getPair(string $offset, $default = null): mixed public Query::hasPair(string $offset): bool public Query::keys([mixed $value = null]): array +public Query::withoutPairs(array $offsets): self ~~~ This class mainly represents the query string as a collection of key/value pairs. @@ -258,6 +443,10 @@ Returns a new `Query` object from an `array` or a `Traversable` object. * `$pairs` : The submitted data must be an `array` or a `Traversable` key/value structure similar to the result of [Query::parse](#parsing-the-query-string-into-an-array). +* `$separator` : The query string separator used for string representation. By default equals to `&`; + +

$separator is availiabe since version 1.3.0.

+ #### Examples ~~~php @@ -372,79 +561,6 @@ $query->keys('gweta'); //return []; By default, the method returns all the keys, but if you supply a value, only the keys whose value equals the value are returned. -## Manipulating the query - -~~~php -merge('foo=jane&r=stone'); -$newQuery->__toString(); //return foo=jane&baz=toto&r=stone -// the 'foo' parameter was updated -// the 'r' parameter was added -~~~ - -

Values equal to null or the empty string are merge differently.

- -~~~php - 'bar', 'baz' => 'toto']); -$newQuery = $query->merge('baz=&r'); -$newQuery->__toString(); //return foo=bar&baz=&r -// the 'r' parameter was added without any value -// the 'baz' parameter was updated to an empty string and its = sign remains -~~~ - -### Query::append - -`Query::append` returns a new `Query` object with its data append to it. - -~~~php -append('foo=baz'); -$newQuery->__toString(); //return foo=jane&foo=baz&john=doe -// a new foo parameter is added -~~~ - ### Query::withoutPairs `Query::withoutPairs` returns a new `Query` object with deleted pairs according to their keys. @@ -459,35 +575,4 @@ use League\Uri\Components\Query; $query = new Query('foo=bar&p=y+olo&z='); $newQuery = $query->withoutPairs(['foo', 'p']); echo $newQuery; //displays 'z=' -~~~ - -### Query::ksort - -`Query::ksort` returns a `Query` object with its pairs sorted according to its keys or a user defined function. - -The single argument `sort` can be: - -One of PHP's sorting constant used by the [sort function](http://php.net/sort). **In this case the query parameters are sorted from low to high** like PHP's [ksort function](http://php.net/ksort) - -~~~php -ksort(SORT_STRING); -$newQuery->__toString(); //return baz=toto&foo=bar -~~~ - -A user-defined comparison function which must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second, like PHP's [uksort function](http://php.net/uksort) - -~~~php -ksort('strcmp'); -$newQuery->__toString(); //return baz=toto&foo=bar ~~~ \ No newline at end of file