Passed
Push — master ( 64278c...139ad8 )
by Ryan
16:01
created

Atom::getMultipleComplexTypes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 3
dl 0
loc 14
rs 9.4285
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
     * Fetches elements with a single, scalar value.
49
     *
50
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
51
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
52
     *                                 of a call to `SimplePie\Util\Ns`.
53
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
54
     */
55
    protected function getSingleScalarTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
56
    {
57
        foreach ([
58
            'icon',
59
            'id',
60
            'logo',
61
            'published',
62
            'rights',
63
            'subtitle',
64
            'summary',
65
            'title',
66
            'updated',
67
        ] as $nodeName) {
68
            $this->addArrayProperty($feedRoot, $nodeName);
69
            $feedRoot->{$nodeName}[$namespaceAlias] = $this->getSingle($nodeName, $namespaceAlias, $xpath);
70
        }
71
    }
72
73
    /**
74
     * Fetches elements with a single, complex value.
75
     *
76
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
77
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
78
     *                                 of a call to `SimplePie\Util\Ns`.
79
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
80
     */
81
    protected function getSingleComplexTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
82
    {
83
        foreach ([
84
            'author' => T\Person::class,
85
            'generator' => T\Generator::class,
86
        ] as $name => $class) {
87
            $this->addArrayProperty($feedRoot, $name);
88
            $xq = $xpath->query($this->generateQuery($namespaceAlias, 'feed', $name));
89
90
            $feedRoot->{$name}[$namespaceAlias] = ($xq->length > 0)
91
                ? new $class($xq->item(0), $this->getLogger())
92
                : null;
93
        }
94
    }
95
96
    /**
97
     * Fetches elements with a multiple, complex values.
98
     *
99
     * @param stdClass $feedRoot       The root of the feed. This will be written-to when the parsing middleware runs.
100
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
101
     *                                 of a call to `SimplePie\Util\Ns`.
102
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
103
     */
104
    protected function getMultipleComplexTypes(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
105
    {
106
        foreach ([
107
            'category' => T\Category::class,
108
            'contributor' => T\Person::class,
109
            'link' => T\Link::class,
110
        ] as $name => $class) {
111
            $this->addArrayProperty($feedRoot, $name);
112
            $xq = $xpath->query($this->generateQuery($namespaceAlias, 'feed', $name));
113
114
            $feedRoot->{$name}[$namespaceAlias] = [];
115
116
            foreach ($xq as $result) {
117
                $feedRoot->{$name}[$namespaceAlias][] = new $class($result, $this->getLogger());
118
            }
119
        }
120
    }
121
122
    /**
123
     * Find and read the contents of the feed-level nodes which only have a single value.
124
     *
125
     * @param string   $nodeName       The name of the namespaced XML node to read.
126
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
127
     *                                 of a call to `SimplePie\Util\Ns`.
128
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
129
     *
130
     * @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...
131
     */
132
    protected function getSingle(string $nodeName, string $namespaceAlias, DOMXPath $xpath): T\Node
133
    {
134
        $query = $this->generateQuery($namespaceAlias, 'feed', $nodeName);
135
        $this->getLogger()->debug(\sprintf('%s is running an XPath query:', __CLASS__), [$query]);
136
137
        return $this->handleSingleNode(static function () use ($xpath, $query) {
138
            return $xpath->query($query);
139
        });
140
    }
141
}
142