Upstream spatie/url state of the touched files before 10490a890c
This commit is contained in:
commit
53eebaebee
2 files changed
+189
No files matched your search
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Spatie\Url;
|
||||||
|
|
||||||
|
class QueryParameterBag implements \Stringable
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected array $parameters = [],
|
||||||
|
) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromString(string $query = ''): static
|
||||||
|
{
|
||||||
|
if ($query === '') {
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
|
|
||||||
|
$parameters = [];
|
||||||
|
parse_str($query, $parameters);
|
||||||
|
$parameters = array_map(fn ($param) => $param !== '' ? $param : null, $parameters);
|
||||||
|
|
||||||
|
return new static($parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(string $key, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
if ($this->has($key)) {
|
||||||
|
return $this->parameters[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return is_callable($default) ? $default() : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has(string $key): bool
|
||||||
|
{
|
||||||
|
return array_key_exists($key, $this->parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(string $key, string|array $value): self
|
||||||
|
{
|
||||||
|
$this->parameters[$key] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unset(string $key): self
|
||||||
|
{
|
||||||
|
unset($this->parameters[$key]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unsetAll(): self
|
||||||
|
{
|
||||||
|
$this->parameters = [];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
return $this->parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return http_build_query($this->parameters, '', '&', PHP_QUERY_RFC3986);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Spatie\Url\QueryParameterBag;
|
||||||
|
|
||||||
|
it('can get a parameter', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('offset')->toEqual(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('returns null if a parameter doesnt exist', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('limit')->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can return a default if a parameter doesnt exist', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('limit', 20)->toEqual(20);
|
||||||
|
expect($queryParameterBag)->get('limit', function () {
|
||||||
|
return 100;
|
||||||
|
})->toEqual(100);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can_return_all_parameters', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->all()->toEqual(['offset' => 10]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can set a string parameter', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag([]);
|
||||||
|
|
||||||
|
$queryParameterBag->set('offset', 10);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('offset')->toEqual(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can set an array parameter', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag([]);
|
||||||
|
|
||||||
|
$queryParameterBag->set('range', [10, 20]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('range')->toEqual([10, 20]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can check if it has a parameter', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->has('offset')->toBeTrue();
|
||||||
|
expect($queryParameterBag)->has('limit')->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can unset a parameter', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag(['offset' => 10]);
|
||||||
|
|
||||||
|
$queryParameterBag->unset('offset');
|
||||||
|
|
||||||
|
expect($queryParameterBag)->has('offset')->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can be created from a string', function () {
|
||||||
|
$queryParameterBag = QueryParameterBag::fromString('offset=10&limit=20');
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('offset')->toEqual('10');
|
||||||
|
expect($queryParameterBag)->get('limit')->toEqual('20');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can be created from an empty string', function () {
|
||||||
|
$queryParameterBag = QueryParameterBag::fromString('');
|
||||||
|
|
||||||
|
expect($queryParameterBag)->all()->toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can be casted to a string', function () {
|
||||||
|
$queryParameterBag = QueryParameterBag::fromString('offset=10&limit=20');
|
||||||
|
|
||||||
|
expect($queryParameterBag)->__toString()->toEqual('offset=10&limit=20');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can be created from a string with url encoded values', function () {
|
||||||
|
$queryParameterBag = QueryParameterBag::fromString(
|
||||||
|
'category=storage%20furniture&discount=%3E40%25%20off&range%5B0%5D=10&range%5B1%5D=20'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->get('category')->toEqual('storage furniture');
|
||||||
|
expect($queryParameterBag)->get('discount')->toEqual('>40% off');
|
||||||
|
expect($queryParameterBag)->get('range')->toEqual([10, 20]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('url encodes values when being casted to a string', function () {
|
||||||
|
$queryParameterBag = new QueryParameterBag([]);
|
||||||
|
|
||||||
|
$queryParameterBag->set('category', 'storage furniture');
|
||||||
|
$queryParameterBag->set('discount', '>40% off');
|
||||||
|
$queryParameterBag->set('range', [10, 20]);
|
||||||
|
|
||||||
|
expect($queryParameterBag)->__toString()->toEqual('category=storage%20furniture&discount=%3E40%25%20off&range%5B0%5D=10&range%5B1%5D=20');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('unsets all query parameters', function () {
|
||||||
|
$queryParameterBag = QueryParameterBag::fromString(
|
||||||
|
'category=storage%20furniture&discount=%3E40%25%20off&range%5B0%5D=10&range%5B1%5D=20'
|
||||||
|
)->unsetAll();
|
||||||
|
|
||||||
|
expect($queryParameterBag)->all()->toEqual([]);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user