|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2017 Ryan Parman <http://ryanparman.com>. |
|
4
|
|
|
* Copyright (c) 2017 Contributors. |
|
5
|
|
|
* |
|
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SimplePie\Middleware\Xml; |
|
12
|
|
|
|
|
13
|
|
|
use SimplePie\Middleware\AbstractMiddleware; |
|
14
|
|
|
use SimplePie\Type\Node; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractXmlMiddleware extends AbstractMiddleware |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Replace all instances of `%s` with the `$namespaceAlias` parameter. |
|
20
|
|
|
* |
|
21
|
|
|
* This is similar to `sprintf()`, but the `$namespaceAlias` is applied to _all_ instances of `%s`. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $query An XPath query where `%s` is used in-place of the XML namespace alias. |
|
24
|
|
|
* @param string $namespaceAlias The XML namespace alias to apply. |
|
25
|
|
|
* |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function applyNsToQuery(string $query, string $namespaceAlias): string |
|
29
|
|
|
{ |
|
30
|
|
|
return \str_replace('%s', $namespaceAlias, $query); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Produce an XPath 1.0 expression which is used to query XML document nodes. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $namespaceAlias The XML namespace alias to apply. |
|
37
|
|
|
* @param bool $supportMixedCase Whether or not to generate an XPath query which supports |
|
38
|
|
|
* mixed-case/case-insensitive XML element names. |
|
39
|
|
|
* @param string ...$path A variadic parameter which accepts the names of the XML |
|
40
|
|
|
* tree nodes in sequence. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string An XPath 1.0 expression. |
|
43
|
|
|
* |
|
44
|
|
|
* @see https://wiki.php.net/rfc/variadics |
|
45
|
|
|
* @see http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list |
|
46
|
|
|
*/ |
|
|
|
|
|
|
47
|
|
|
public function generateQuery(string $namespaceAlias, bool $supportMixedCase = false, string ...$path): string |
|
48
|
|
|
{ |
|
49
|
|
|
$query = ''; |
|
50
|
|
|
|
|
51
|
|
|
foreach ($path as $p) { |
|
52
|
|
|
if ($supportMixedCase) { |
|
53
|
|
|
$query .= \sprintf( |
|
54
|
|
|
'/%s:*[translate(name(), \'%s\', \'%s\') = \'%s\']', |
|
55
|
|
|
$namespaceAlias, |
|
56
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', |
|
57
|
|
|
'abcdefghijklmnopqrstuvwxyz', |
|
58
|
|
|
$p |
|
59
|
|
|
); |
|
60
|
|
|
} else { |
|
61
|
|
|
$query .= \sprintf( |
|
62
|
|
|
'/%s:%s', |
|
63
|
|
|
$namespaceAlias, |
|
64
|
|
|
$p |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $query; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Some elements in the feed should only have one result. This handles those cases. |
|
74
|
|
|
* |
|
75
|
|
|
* @param callable $fn A callable which returns a `DOMElementList`. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array Returns an array with keys of `text` and `html`. |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
|
|
public function handleSingleNode(callable $fn): Node |
|
80
|
|
|
{ |
|
81
|
|
|
$nodes = $fn(); |
|
82
|
|
|
|
|
83
|
|
|
if ($nodes->length > 0) { |
|
84
|
|
|
return new Node($nodes[0]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new Node(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function handleMultipleNodes(callable $fn): Node |
|
91
|
|
|
{ |
|
92
|
|
|
$nodes = $fn(); |
|
93
|
|
|
|
|
94
|
|
|
\print_r($nodes); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.
Most often this is a case of a parameter that can be null in addition to its declared types.