-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUri_Query_String.php
More file actions
115 lines (100 loc) · 2.58 KB
/
Copy pathUri_Query_String.php
File metadata and controls
115 lines (100 loc) · 2.58 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Uri_Query_String class file
*
* @package mantle
*/
namespace Mantle\Support;
use Mantle\Contracts\Support\Arrayable;
use League\Uri\QueryString;
use Stringable;
use function Mantle\Support\Helpers\data_get;
/**
* URI Query String
*
* Manage the query string of Uri.
*/
class Uri_Query_String implements Arrayable, Stringable {
/**
* Parsed query string.
*
* @var array<string, mixed>
*/
protected array $parsed_query;
/**
* Create a new URI query string instance.
*
* @param Uri $uri The URI instance containing the query string.
*/
public function __construct( protected Uri $uri ) {
$this->parsed_query = QueryString::extract( $this->uri->get_uri()->getQuery() );
}
/**
* Retrieve a value from the query string.
*
* @param string $property Property name to retrieve.
* @param mixed $default Default value to return if the property does not exist.
*/
public function get( string $property, mixed $default = null ): mixed {
return data_get( $this->parsed_query, $property, $default );
}
/**
* Retrieved a value from the query string as Mixed_Data object.
*
* @param string $property Property name to retrieve.
* @param mixed $default Default value to return if the property does not exist.
*/
public function mixed( string $property, mixed $default = null ): Mixed_Data {
return Mixed_Data::of( $this->get( $property, $default ) );
}
/**
* Retrieve all values from the query string as an array.
*
* @return array<string, mixed>
*/
public function all(): array {
return $this->parsed_query;
}
/**
* Convert the query string to an array.
*
* @return array<string, mixed>
*/
public function to_array(): array {
return $this->parsed_query;
}
/**
* Check if a property exists in the query string.
*
* @param string $property Property name to check for existence.
*/
public function has( string $property ): bool {
return array_key_exists( $property, $this->parsed_query );
}
/**
* Check if the query string is missing a property.
*
* @param string $property Property name to check for absence.
*/
public function missing( string $property ): bool {
return ! $this->has( $property );
}
/**
* Retrieve the query string value.
*/
public function value(): string {
return (string) $this;
}
/**
* 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 __toString(): string {
return (string) $this->uri->get_uri()->getQuery();
}
}