|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Uri package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Daniel Schröder <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace GravityMedia\Uri; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Abstract query component class. |
|
12
|
|
|
* |
|
13
|
|
|
* @package GravityMedia\Uri |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractQuery |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Create query object from array. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $array |
|
21
|
|
|
* |
|
22
|
|
|
* @return static |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public static function fromArray(array $array = []) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$query = new static(); |
|
27
|
2 |
|
foreach ($array as $argument => $value) { |
|
28
|
2 |
|
$query->__set($argument, $value); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
return $query; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create query object from string. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $string |
|
38
|
|
|
* |
|
39
|
|
|
* @return static |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public static function fromString($string) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$array = []; |
|
44
|
2 |
|
parse_str($string, $array); |
|
45
|
|
|
|
|
46
|
2 |
|
return static::fromArray($array); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set a query argument. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $argument |
|
53
|
|
|
* @param mixed $value |
|
54
|
|
|
*/ |
|
55
|
4 |
View Code Duplication |
public function __set($argument, $value) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
4 |
|
$setter = 'set' . str_replace('_', '', $argument); |
|
58
|
4 |
|
if (is_callable([$this, $setter])) { |
|
59
|
4 |
|
call_user_func([$this, $setter], $value); |
|
60
|
2 |
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get a query argument. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $argument |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
8 |
View Code Duplication |
public function __get($argument) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
8 |
|
$getter = 'get' . str_replace('_', '', $argument); |
|
73
|
8 |
|
if (is_callable([$this, $getter])) { |
|
74
|
6 |
|
return call_user_func([$this, $getter]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Test if a query argument is NULL. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $argument |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function __isset($argument) |
|
88
|
|
|
{ |
|
89
|
4 |
|
$getter = 'get' . str_replace('_', '', $argument); |
|
90
|
4 |
|
if (!method_exists($this, $getter)) { |
|
91
|
2 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return null !== $this->__get($argument); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set a query argument to NULL. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $argument |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function __unset($argument) |
|
103
|
|
|
{ |
|
104
|
2 |
|
$this->__set($argument, null); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return string representation. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
4 |
|
public function __toString() |
|
113
|
|
|
{ |
|
114
|
4 |
|
return $this->toString(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Convert query into a string representation. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function toString() |
|
123
|
|
|
{ |
|
124
|
4 |
|
return http_build_query($this->toArray()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Convert query into a array representation. |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
4 |
|
public function toArray() |
|
133
|
|
|
{ |
|
134
|
4 |
|
$array = []; |
|
135
|
4 |
|
foreach ($this as $argument => $value) { |
|
|
|
|
|
|
136
|
4 |
|
$normalizedArgument = preg_replace_callback('/([A-Z])/', function ($letters) { |
|
137
|
4 |
|
$letter = array_shift($letters); |
|
138
|
|
|
|
|
139
|
4 |
|
return '_' . strtolower($letter); |
|
140
|
4 |
|
}, $argument); |
|
141
|
|
|
|
|
142
|
4 |
|
$array[$normalizedArgument] = $value; |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
4 |
|
return $array; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.