1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017–2018 Ryan Parman <http://ryanparman.com>. |
4
|
|
|
* Copyright (c) 2017–2018 Contributors. |
5
|
|
|
* |
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace SimplePie\Util; |
12
|
|
|
|
13
|
|
|
use DOMNode; |
14
|
|
|
use Psr\Log\NullLogger; |
15
|
|
|
use SimplePie\Mixin as T; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides tools for managing and working with XML namespaces. |
19
|
|
|
*/ |
20
|
|
|
class Ns |
21
|
|
|
{ |
22
|
|
|
use T\LoggerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A mapping of namespace URIs to preferred namespace aliases. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $mapping = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructs a new instance of this class. |
33
|
|
|
* |
34
|
|
|
* @param DOMNode $dom A DOMDocument object representing the XML file to be parsed. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(DOMNode $dom) |
37
|
|
|
{ |
38
|
|
|
$this->logger = new NullLogger(); |
39
|
|
|
$this->domDocument = $dom; |
|
|
|
|
40
|
|
|
$this->mapping = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds new aliases to this list of aliases. Set an associative array where |
45
|
|
|
* the key is the namespace URI and the namespace alias as the value. |
46
|
|
|
* |
47
|
|
|
* @param array $aliases An associative array of namespace URIs to namespace aliases. |
48
|
|
|
* |
49
|
|
|
* @return array The updated list of namespace aliases. |
50
|
|
|
*/ |
51
|
|
|
public function addAliases(array $aliases): array |
52
|
|
|
{ |
53
|
|
|
$this->mapping = \array_merge($this->mapping, $aliases); |
54
|
|
|
$this->getLogger()->info('Added namespace URIs and namespace aliases.', $this->mapping); |
55
|
|
|
|
56
|
|
|
return $this->mapping; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets the list of document-defined namespace aliases and namespace URIs. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getDocNamespaces(): array |
65
|
|
|
{ |
66
|
|
|
return \simplexml_import_dom($this->domDocument)->getDocNamespaces(true, true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the preferred namespace alias for a particular feed dialect. |
71
|
|
|
* |
72
|
|
|
* @param string|null $namespaceUri The namespace URI used inside the XML document. If the value is `null`, then |
73
|
|
|
* the preferred namespace alias of the root namespace will be returned. The |
74
|
|
|
* default value is `null`. |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function getPreferredNamespaceAlias(?string $namespaceUri = null): ?string |
79
|
|
|
{ |
80
|
|
|
$namespaceUri = $namespaceUri |
81
|
|
|
?? $this->domDocument->documentElement->namespaceUri; |
82
|
|
|
|
83
|
|
|
// If the namespace URI has an exact match, use it. |
84
|
|
|
if (isset($this->mapping[$namespaceUri])) { |
85
|
|
|
return $this->mapping[$namespaceUri]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If not an exact match, attempt a regex match. |
89
|
|
|
foreach ($this->mapping as $regex => $alias) { |
90
|
|
|
// Regex delimiter must not be alphanumeric or backslash. Check this first. |
91
|
|
|
if (0 === \preg_match('/[0-9a-z\\\\]/i', $regex[0]) && 0 !== \preg_match($regex, $namespaceUri)) { |
92
|
|
|
return $alias; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|