|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fwk |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
23
|
|
|
* |
|
24
|
|
|
* PHP Version 5.3 |
|
25
|
|
|
* |
|
26
|
|
|
* @category XML |
|
27
|
|
|
* @package Fwk\Xml |
|
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
29
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
|
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
31
|
|
|
* @link http://www.nitronet.org/fwk |
|
32
|
|
|
*/ |
|
33
|
|
|
namespace Fwk\Xml; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Path |
|
37
|
|
|
* |
|
38
|
|
|
* Represents a Path (xpath) and describes how values/attributes should be used. |
|
39
|
|
|
* |
|
40
|
|
|
* @category Library |
|
41
|
|
|
* @package Fwk\Xml |
|
42
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
43
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
44
|
|
|
* @link http://www.nitronet.org/fwk |
|
45
|
|
|
*/ |
|
46
|
|
|
class Path |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* Xpath to element |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $xpath; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Default value |
|
56
|
|
|
* @var mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $default; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Filter function |
|
62
|
|
|
* @var \Closure |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $filter; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Key name for this Path |
|
68
|
|
|
* @var type |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $key; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Should we loop on this path ? |
|
74
|
|
|
* @var type |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $loop = false; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Xpath to a value used as a key identifier when looping |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $loopId; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* List of sub-elements (Paths) |
|
86
|
|
|
* @var array |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $childrens = array(); |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* List of attributes to fetch |
|
92
|
|
|
* @var array |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $attributes = array(); |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Key used for main element's value |
|
98
|
|
|
* @var string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected $valueKey; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Constructor |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $xpath XPath to element |
|
106
|
|
|
* @param string $key Key name |
|
107
|
|
|
* @param mixed $default Default value if element not defined |
|
108
|
|
|
* @param callable $filter Filtering function |
|
|
|
|
|
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
13 |
|
public function __construct($xpath, $key, $default = null, |
|
113
|
|
|
$filter = null |
|
114
|
|
|
) { |
|
115
|
13 |
|
$this->xpath = $xpath; |
|
116
|
13 |
|
$this->default = $default; |
|
117
|
13 |
|
$this->filter = $filter; |
|
|
|
|
|
|
118
|
13 |
|
$this->key = $key; |
|
|
|
|
|
|
119
|
13 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Defines looping on this path |
|
123
|
|
|
* |
|
124
|
|
|
* @param boolean $bool Should we loop on this element ? |
|
125
|
|
|
* @param string $loopId Xpath to key identifier |
|
|
|
|
|
|
126
|
|
|
* |
|
127
|
|
|
* @return Path |
|
128
|
|
|
*/ |
|
129
|
8 |
|
public function loop($bool, $loopId = null) |
|
130
|
|
|
{ |
|
131
|
8 |
|
$this->loop = $bool; |
|
|
|
|
|
|
132
|
8 |
|
$this->loopId = $loopId; |
|
133
|
|
|
|
|
134
|
8 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Defines a filtering function applied on the value |
|
139
|
|
|
* |
|
140
|
|
|
* @param \Closure $filter Value filtering function |
|
141
|
|
|
* |
|
142
|
|
|
* @return Path |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function filter($filter) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->filter = $filter; |
|
147
|
|
|
|
|
148
|
1 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Tells the Map to fetch the element's value, in $keyName |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $keyName Key name where the value will be stored |
|
155
|
|
|
* |
|
156
|
|
|
* @return Path |
|
157
|
|
|
*/ |
|
158
|
4 |
|
public function value($keyName) |
|
159
|
|
|
{ |
|
160
|
4 |
|
$this->valueKey = $keyName; |
|
161
|
|
|
|
|
162
|
4 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Defines a default value in case element is empty |
|
167
|
|
|
* |
|
168
|
|
|
* @param mixed $default Default value |
|
169
|
|
|
* |
|
170
|
|
|
* @return Path |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function setDefault($default) |
|
173
|
|
|
{ |
|
174
|
2 |
|
$this->default = $default; |
|
175
|
|
|
|
|
176
|
2 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Returns the Xpath to the element |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
12 |
|
public function getXpath() |
|
185
|
|
|
{ |
|
186
|
12 |
|
return $this->xpath; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the defined default value (if any) |
|
191
|
|
|
* |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
12 |
|
public function getDefault() |
|
195
|
|
|
{ |
|
196
|
12 |
|
return $this->default; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns the filter function (if any) |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Closure |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function getFilter() |
|
205
|
|
|
{ |
|
206
|
1 |
|
return $this->filter; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Return the key of this Path |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
|
|
|
|
|
213
|
|
|
*/ |
|
214
|
12 |
|
public function getKey() |
|
215
|
|
|
{ |
|
216
|
12 |
|
return $this->key; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Tells if we should loop on this xpath |
|
221
|
|
|
* |
|
222
|
|
|
* @return boolean |
|
|
|
|
|
|
223
|
|
|
*/ |
|
224
|
11 |
|
public function isLoop() |
|
225
|
|
|
{ |
|
226
|
11 |
|
return $this->loop; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Tells if we should store the element's main value |
|
231
|
|
|
* |
|
232
|
|
|
* @return boolean |
|
233
|
|
|
*/ |
|
234
|
5 |
|
public function hasValueKey() |
|
235
|
|
|
{ |
|
236
|
5 |
|
return (isset($this->valueKey) && !empty($this->valueKey)); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Tells if a filter function has been defined |
|
241
|
|
|
* |
|
242
|
|
|
* @return boolean |
|
243
|
|
|
*/ |
|
244
|
9 |
|
public function hasFilter() |
|
245
|
|
|
{ |
|
246
|
9 |
|
return is_callable($this->filter); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Factory method (helps chaining) |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $xpath XPath to element |
|
253
|
|
|
* @param string $key Key name |
|
254
|
|
|
* @param mixed $default Default value if element not defined |
|
255
|
|
|
* @param callable $filter Filtering function |
|
|
|
|
|
|
256
|
|
|
* |
|
257
|
|
|
* @return Path |
|
258
|
|
|
*/ |
|
259
|
13 |
|
public static function factory($xpath, $key, $default = null, |
|
260
|
|
|
$filter = null |
|
261
|
|
|
) { |
|
262
|
13 |
|
return new self($xpath, $key, $default, $filter); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Adds a child Path |
|
267
|
|
|
* |
|
268
|
|
|
* @param Path $path Child Path |
|
269
|
|
|
* |
|
270
|
|
|
* @return Path |
|
271
|
|
|
*/ |
|
272
|
5 |
|
public function addChildren(Path $path) |
|
273
|
|
|
{ |
|
274
|
5 |
|
$this->childrens[] = $path; |
|
275
|
|
|
|
|
276
|
5 |
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Returns all child Paths |
|
281
|
|
|
* |
|
282
|
|
|
* @return array |
|
283
|
|
|
*/ |
|
284
|
11 |
|
public function getChildrens() |
|
285
|
|
|
{ |
|
286
|
11 |
|
return $this->childrens; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Tells the Map to fetch an attribute of this path. |
|
291
|
|
|
* If no $key is defined the $attrName is used. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $attrName Attribute name |
|
294
|
|
|
* @param string $key Attribute key name |
|
|
|
|
|
|
295
|
|
|
* |
|
296
|
|
|
* @return Path |
|
297
|
|
|
*/ |
|
298
|
4 |
|
public function attribute($attrName, $key = null) |
|
299
|
|
|
{ |
|
300
|
4 |
|
if (null === $key) { |
|
301
|
4 |
|
$key = $attrName; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
4 |
|
$this->attributes[$key] = $attrName; |
|
305
|
|
|
|
|
306
|
4 |
|
return $this; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Returns all attributes |
|
311
|
|
|
* |
|
312
|
|
|
* @return array |
|
313
|
|
|
*/ |
|
314
|
11 |
|
public function getAttributes() |
|
315
|
|
|
{ |
|
316
|
11 |
|
return $this->attributes; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Returns the loop key identifiers |
|
321
|
|
|
* |
|
322
|
|
|
* @return string |
|
323
|
|
|
*/ |
|
324
|
7 |
|
public function getLoopId() |
|
325
|
|
|
{ |
|
326
|
7 |
|
return $this->loopId; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Returns the key identifier where the root value is stored |
|
331
|
|
|
* |
|
332
|
|
|
* @return string |
|
333
|
|
|
*/ |
|
334
|
4 |
|
public function getValueKey() |
|
335
|
|
|
{ |
|
336
|
4 |
|
return $this->valueKey; |
|
337
|
|
|
} |
|
338
|
|
|
} |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.