|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Url\Params; |
|
8
|
|
|
|
|
9
|
|
|
use Zicht\Bundle\FrameworkExtraBundle\Util\SortedSetMap; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Abstracts faceted uri's into a Map construct, with each key in the map corresponding to the name of the |
|
13
|
|
|
* facet and each facet represented as a sorted set. |
|
14
|
|
|
* |
|
15
|
|
|
* The with() method will return a clone of the instance with a value for the given key added |
|
16
|
|
|
* or removed. It is added if it wasn't yet in the set, it is removed if it was. |
|
17
|
|
|
* |
|
18
|
|
|
* Usage: |
|
19
|
|
|
* |
|
20
|
|
|
* $uri = new Zicht_Util_FacetUri('a=1/b=2/c=3,4'); |
|
21
|
|
|
* |
|
22
|
|
|
* $uri->with('a', 2); // will return a new instance with "a=1,2/b=2/c=3,4" |
|
23
|
|
|
* $uri->with('c', 3); // will return a new instance with "a=1/b=2/c=4" |
|
24
|
|
|
*/ |
|
25
|
|
|
class Params extends SortedSetMap |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var UriParser |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $parser; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Construct the object and parse the URI. |
|
34
|
|
|
* |
|
35
|
|
|
* Each of the separator parameters is used for providing different formats of the URI. These separators |
|
36
|
|
|
* will be used to generate a string of the object too. |
|
37
|
|
|
* |
|
38
|
|
|
* @param null|UriParser $parser |
|
39
|
|
|
* @return Params |
|
40
|
|
|
*/ |
|
41
|
18 |
|
public function __construct(UriParser $parser = null) |
|
42
|
|
|
{ |
|
43
|
18 |
|
parent::__construct(); |
|
44
|
18 |
|
if (is_null($parser)) { |
|
45
|
17 |
|
$parser = new UriParser(); |
|
46
|
|
|
} |
|
47
|
18 |
|
$this->parser = $parser; |
|
48
|
18 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Parses the uri and assigns the parsed values. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $uri |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
17 |
|
public function setUri($uri) |
|
58
|
|
|
{ |
|
59
|
17 |
|
$this->setValues($this->parser->parseUri($uri)); |
|
60
|
17 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Process the POST and merges the (translated) values. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $post |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function mergePost($post) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->mergeAll($this->parser->parsePost($post)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Duplicates the current instance with one value changed. |
|
77
|
|
|
* |
|
78
|
|
|
* - If the value already exists, the value is removed; |
|
79
|
|
|
* - If the value does not exist, it is added; |
|
80
|
|
|
* - If the value exists, and 'multiple' is false, it is replaced. |
|
81
|
|
|
* - If the value does not exists, and 'multiple' is false, it is added. |
|
82
|
|
|
* |
|
83
|
|
|
* The $multiple parameter is typically useful for paging or other parameters. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* @param string $value |
|
87
|
|
|
* @param bool $multiple |
|
88
|
|
|
* @return Params |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function with($key, $value, $multiple = true) |
|
91
|
|
|
{ |
|
92
|
7 |
|
$ret = clone $this; |
|
93
|
7 |
|
$ret = self::doWith($ret, $key, $value, $multiple); |
|
94
|
|
|
|
|
95
|
6 |
|
return $ret; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Helper for with() |
|
101
|
|
|
* |
|
102
|
|
|
* @param Params $ret |
|
103
|
|
|
* @param string $key |
|
104
|
|
|
* @param string $value |
|
105
|
|
|
* @param bool $multiple |
|
106
|
|
|
* @return Params |
|
107
|
|
|
*/ |
|
108
|
7 |
|
private static function doWith(Params $ret, $key, $value, $multiple = true) |
|
109
|
|
|
{ |
|
110
|
7 |
|
if (!$multiple) { |
|
111
|
3 |
|
if (!is_scalar($value)) { |
|
|
|
|
|
|
112
|
1 |
|
throw new \InvalidArgumentException( |
|
113
|
1 |
|
"Invalid argument \$value to with(), expected scalar, got " . gettype($value) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
2 |
|
if ($ret->contains($key, $value)) { |
|
117
|
1 |
|
$ret->remove($key, $value); |
|
118
|
|
|
} else { |
|
119
|
2 |
|
$ret->replace($key, array($value)); |
|
120
|
|
|
} |
|
121
|
|
|
} else { |
|
122
|
4 |
|
if ($ret->contains($key, $value)) { |
|
123
|
2 |
|
$ret->remove($key, $value); |
|
124
|
|
|
} else { |
|
125
|
3 |
|
$ret->add($key, $value); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
6 |
|
return $ret; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns a single value, and defaults to the given default parameter if not available. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $key |
|
137
|
|
|
* @param mixed $default |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function getOne($key, $default = null) |
|
141
|
|
|
{ |
|
142
|
1 |
|
$ret = $default; |
|
143
|
1 |
|
$all = $this->get($key); |
|
144
|
1 |
|
if (count($all) > 0) { |
|
145
|
1 |
|
$ret = array_shift($all); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
return $ret; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Duplicates the current instance with one or more sets of values removed |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $keys |
|
156
|
|
|
* @return Params |
|
157
|
|
|
*/ |
|
158
|
3 |
|
public function without($keys) |
|
159
|
|
|
{ |
|
160
|
3 |
|
if (is_scalar($keys)) { |
|
161
|
2 |
|
$keys = array($keys); |
|
162
|
|
|
} |
|
163
|
3 |
|
$ret = clone $this; |
|
164
|
3 |
|
foreach ($keys as $key) { |
|
165
|
3 |
|
$ret->removeKey($key); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
3 |
|
return $ret; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns all keys in the current set. |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getKeys() |
|
178
|
|
|
{ |
|
179
|
|
|
return array_keys($this->toArray()); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Renders a string of the URI, joining it using the separators provided at construction time. |
|
184
|
|
|
* |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
13 |
|
public function __toString() |
|
188
|
|
|
{ |
|
189
|
13 |
|
return $this->parser->composeUri($this->toArray()); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|