Passed
Push — master ( 115a96...ff3cf0 )
by Ryan
13:16
created

Atom::getSupportedNamespaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Middleware\Xml;
12
13
use DOMXPath;
14
use SimplePie\Configuration as C;
15
use SimplePie\Mixin as Tr;
16
use SimplePie\Type as T;
17
use stdClass;
18
19
/**
20
 * Support for the Atom 1.0 grammar.
21
 *
22
 * @see https://tools.ietf.org/html/rfc4287
23
 * @see https://www.w3.org/wiki/Atom
24
 */
25
class Atom extends AbstractXmlMiddleware implements XmlInterface, C\SetLoggerInterface
26
{
27
    use Tr\LoggerTrait;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __invoke(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
33
    {
34
        // lang (single, scalar)
35
        $this->addArrayProperty($feedRoot, 'lang');
36
        $xq = $xpath->query($this->applyNsToQuery('/%s:feed[attribute::xml:lang][1]/@xml:lang', $namespaceAlias));
37
38
        $feedRoot->lang[$namespaceAlias] = ($xq->length > 0)
39
            ? T\Node::factory((string) $xq->item(0)->nodeValue)
40
            : null;
41
42
        $this->getSingleScalarTypes($feedRoot, $namespaceAlias, $xpath);
43
        $this->getSingleComplexTypes($feedRoot, $namespaceAlias, $xpath);
44
        $this->getMultipleComplexTypes($feedRoot, $namespaceAlias, $xpath);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getSupportedNamespaces(): array
51
    {
52
        return [
53
            'http://www.w3.org/2005/Atom' => 'atom10',
54
        ];
55
    }
56
57
    /**
58
     * Fetches elements with a single, scalar value.
59
     *
60
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
61
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
62
     *                                 of a call to `SimplePie\Util\Ns`.
63
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
64
     */
65
    protected function getSingleScalarTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
66
    {
67
        foreach ([
68
            'icon',
69
            'id',
70
            'logo',
71
            'published',
72
            'rights',
73
            'subtitle',
74
            'summary',
75
            'title',
76
            'updated',
77
        ] as $nodeName) {
78
            $this->addArrayProperty($feedRoot, $nodeName);
79
            $feedRoot->{$nodeName}[$namespaceAlias] = $this->getSingle($nodeName, $namespaceAlias, $xpath);
80
        }
81
    }
82
83
    /**
84
     * Fetches elements with a single, complex value.
85
     *
86
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
87
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
88
     *                                 of a call to `SimplePie\Util\Ns`.
89
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
90
     */
91
    protected function getSingleComplexTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
92
    {
93
        foreach ([
94
            'author' => T\Person::class,
95
            'generator' => T\Generator::class,
96
        ] as $name => $class) {
97
            $this->addArrayProperty($feedRoot, $name);
98
            $xq = $xpath->query($this->generateQuery($namespaceAlias, 'feed', $name));
99
100
            $feedRoot->{$name}[$namespaceAlias] = ($xq->length > 0)
101
                ? new $class($xq->item(0), $this->getLogger())
102
                : null;
103
        }
104
    }
105
106
    /**
107
     * Fetches elements with a multiple, complex values.
108
     *
109
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
110
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
111
     *                                 of a call to `SimplePie\Util\Ns`.
112
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
113
     */
114
    protected function getMultipleComplexTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
115
    {
116
        foreach ([
117
            'category' => T\Category::class,
118
            'contributor' => T\Person::class,
119
            'link' => T\Link::class,
120
        ] as $name => $class) {
121
            $this->addArrayProperty($feedRoot, $name);
122
            $xq = $xpath->query($this->generateQuery($namespaceAlias, 'feed', $name));
123
124
            $feedRoot->{$name}[$namespaceAlias] = [];
125
126
            foreach ($xq as $result) {
127
                $feedRoot->{$name}[$namespaceAlias][] = new $class($result, $this->getLogger());
128
            }
129
        }
130
    }
131
132
    /**
133
     * Find and read the contents of the feed-level nodes which only have a single value.
134
     *
135
     * @param string   $nodeName       The name of the namespaced XML node to read.
136
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
137
     *                                 of a call to `SimplePie\Util\Ns`.
138
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
139
     *
140
     * @return Node
0 ignored issues
show
Bug introduced by
The type SimplePie\Middleware\Xml\Node 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...
141
     */
142
    protected function getSingle(string $nodeName, string $namespaceAlias, DOMXPath $xpath): T\Node
143
    {
144
        $query = $this->generateQuery($namespaceAlias, 'feed', $nodeName);
145
        $this->getLogger()->debug(\sprintf('%s is running an XPath query:', __CLASS__), [$query]);
146
147
        return $this->handleSingleNode(static function () use ($xpath, $query) {
148
            return $xpath->query($query);
149
        });
150
    }
151
}
152