Issues (46)

src/Util/Ns.php (4 issues)

1
<?php
2
/**
3
 * Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2019 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Util;
12
13
use DOMDocument;
14
use Psr\Log\NullLogger;
15
use SimplePie\Mixin as Tr;
16
17
/**
18
 * Provides tools for managing and working with XML namespaces.
19
 */
20
class Ns
21
{
22
    use Tr\DomDocumentTrait;
0 ignored issues
show
The type SimplePie\Mixin\DomDocumentTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
    use Tr\LoggerTrait;
0 ignored issues
show
The type SimplePie\Mixin\LoggerTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
    /**
26
     * A mapping of namespace URIs to preferred namespace aliases.
27
     *
28
     * @var array
29
     */
30
    protected $mapping = [];
31
32
    /**
33
     * Constructs a new instance of this class.
34
     *
35
     * @param DOMDocument $dom A DOMDocument object representing the XML file to be parsed.
36
     */
37 560
    public function __construct(DOMDocument $dom)
38
    {
39 560
        $this->logger      = new NullLogger();
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40 560
        $this->domDocument = $dom;
0 ignored issues
show
Bug Best Practice introduced by
The property domDocument does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41 560
        $this->mapping     = [];
42 560
    }
43
44
    /**
45
     * Adds new aliases to this list of aliases. Set an associative array where
46
     * the key is the namespace URI and the namespace alias as the value.
47
     *
48
     * @param array $aliases An associative array of namespace URIs to namespace aliases.
49
     *
50
     * @return array The updated list of namespace aliases.
51
     */
52 560
    public function addAliases(array $aliases): array
53
    {
54 560
        $this->mapping = \array_merge($this->mapping, $aliases);
55 560
        $this->getLogger()->info('Added namespace URIs and namespace aliases.', $this->mapping);
56
57 560
        return $this->mapping;
58
    }
59
60
    /**
61
     * Gets the list of document-defined namespace aliases and namespace URIs.
62
     */
63
    public function getDocNamespaces(): array
64
    {
65
        return \simplexml_import_dom($this->domDocument)->getDocNamespaces(true, true);
66
    }
67
68
    /**
69
     * Gets the preferred namespace alias for a particular feed dialect.
70
     *
71
     * @param string|null $namespaceUri The namespace URI used inside the XML document. If the value is `null`, then
72
     *                                  the preferred namespace alias of the root namespace will be returned. The
73
     *                                  default value is `null`.
74
     */
75 560
    public function getPreferredNamespaceAlias(?string $namespaceUri = null): string
76
    {
77 560
        $namespaceUri = $namespaceUri
78
            ?? $this->domDocument->documentElement->namespaceURI
79 560
            ?? '';
80
81
        // If the namespace URI has an exact match, use it.
82 560
        if (isset($this->mapping[$namespaceUri])) {
83 560
            return $this->mapping[$namespaceUri];
84
        }
85
86
        // If not an exact match, attempt a regex match.
87
        foreach ($this->mapping as $regex => $alias) {
88
            // Regex delimiter must not be alphanumeric or backslash. Check this first.
89
            // @phpcs:disable Generic.Files.LineLength.MaxExceeded
90
            if (!empty($regex) && 0 === \preg_match('/[0-9a-z\\\\]/i', $regex[0]) && 0 !== \preg_match($regex, $namespaceUri)) {
91
                // @phpcs:enable
92
                return $alias;
93
            }
94
        }
95
96
        return null;
97
    }
98
}
99