Completed
Push — master ( 16b7b3...90dfdb )
by Ryan
13:15
created

Atom   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 76
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 48 7
A getSingle() 0 9 1
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 DOMXPath;
14
use SimplePie\Configuration as C;
15
use SimplePie\Mixin as T;
16
use SimplePie\Type\Generator;
17
use SimplePie\Type\Node;
18
use SimplePie\Type\Person;
19
use stdClass;
20
21
/**
22
 * Support for the Atom 1.0 grammar.
23
 *
24
 * @see https://tools.ietf.org/html/rfc4287
25
 * @see https://www.w3.org/wiki/Atom
26
 */
27
class Atom extends AbstractXmlMiddleware implements XmlInterface, C\SetLoggerInterface
28
{
29
    use T\LoggerTrait;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function __invoke(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
35
    {
36
        // lang (single, scalar)
37
        $this->addArrayProperty($feedRoot, 'lang');
38
        $xq = $xpath->query($this->applyNsToQuery('/%s:feed[attribute::xml:lang][1]/@xml:lang', $namespaceAlias));
39
40
        $feedRoot->lang[$namespaceAlias] = ($xq->length > 0)
41
            ? Node::factory((string) $xq->item(0)->value)
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in DOMNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
            : null;
43
44
        // single, scalar
45
        foreach (['id', 'rights', 'subtitle', 'summary', 'title'] as $nodeName) {
46
            $this->addArrayProperty($feedRoot, $nodeName);
47
            $feedRoot->{$nodeName}[$namespaceAlias] = $this->getSingle($nodeName, $namespaceAlias, $xpath);
48
        }
49
50
        // time/date stamps
51
        foreach (['published', 'updated'] as $nodeName) {
52
            $this->addArrayProperty($feedRoot, $nodeName);
53
            $feedRoot->{$nodeName}[$namespaceAlias] = $this->getSingle($nodeName, $namespaceAlias, $xpath);
54
        }
55
56
        // generator (single, complex)
57
        $this->addArrayProperty($feedRoot, 'generator');
58
        $xq = $xpath->query($this->generateQuery($namespaceAlias, true, 'feed', 'generator'));
59
60
        $feedRoot->generator[$namespaceAlias] = ($xq->length > 0)
61
            ? new Generator($xq->item(0), $this->getLogger())
62
            : null;
63
64
        // author (single, complex)
65
        $this->addArrayProperty($feedRoot, 'author');
66
        $xq = $xpath->query($this->generateQuery($namespaceAlias, true, 'feed', 'author'));
67
68
        $feedRoot->author[$namespaceAlias] = ($xq->length > 0)
69
            ? new Person($xq->item(0), $this->getLogger())
70
            : null;
71
72
        // contributor (multiple, complex)
73
        $this->addArrayProperty($feedRoot, 'contributor');
74
        $xq = $xpath->query($this->generateQuery($namespaceAlias, true, 'feed', 'contributor'));
75
76
        $feedRoot->contributor[$namespaceAlias] = [];
77
78
        foreach ($xq as $result) {
79
            $feedRoot->contributor[$namespaceAlias][] = new Person($result, $this->getLogger());
80
        }
81
    }
82
83
    /**
84
     * Find and read the contents of the feed-level nodes which only have a single value.
85
     *
86
     * @param string   $nodeName       The name of the namespaced XML node to read.
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
     * @return Node
92
     */
93
    protected function getSingle(string $nodeName, string $namespaceAlias, DOMXPath $xpath): Node
94
    {
95
        $query = $this->generateQuery($namespaceAlias, true, 'feed', $nodeName);
96
        $this->getLogger()->debug(\sprintf('%s is running an XPath query:', __CLASS__), [$query]);
97
98
        return $this->handleSingleNode(static function () use ($xpath, $query) {
99
            return $xpath->query($query);
100
        });
101
    }
102
}
103