-
Notifications
You must be signed in to change notification settings - Fork 162
/
UriQueryString.php
95 lines (80 loc) · 1.92 KB
/
UriQueryString.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Illuminate\Support;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Traits\InteractsWithData;
use League\Uri\QueryString;
class UriQueryString implements Arrayable
{
use InteractsWithData;
/**
* Create a new URI query string instance.
*/
public function __construct(protected Uri $uri)
{
//
}
/**
* Retrieve all data from the instance.
*
* @param array|mixed|null $keys
* @return array
*/
public function all($keys = null)
{
$query = $this->toArray();
if (! $keys) {
return $query;
}
$results = [];
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
Arr::set($results, $key, Arr::get($query, $key));
}
return $results;
}
/**
* Retrieve data from the instance.
*
* @param string|null $key
* @param mixed $default
* @return mixed
*/
protected function data($key = null, $default = null)
{
return $this->get($key, $default);
}
/**
* Get a query string parameter.
*/
public function get(?string $key = null, mixed $default = null): mixed
{
return data_get($this->toArray(), $key, $default);
}
/**
* Get the URL decoded version of the query string.
*/
public function decode(): string
{
return rawurldecode((string) $this);
}
/**
* Get the string representation of the query string.
*/
public function value(): string
{
return (string) $this;
}
/**
* Convert the query string into an array.
*/
public function toArray()
{
return QueryString::extract($this->value());
}
/**
* Get the string representation of the query string.
*/
public function __toString(): string
{
return (string) $this->uri->getUri()->getQuery();
}
}