Passed
Push — master ( a13bb5...cada17 )
by Ryan
32:56 queued 18:49
created

Entry::getComplexSingleValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
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\Type;
12
13
use DOMNode;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
use SimplePie\Configuration as C;
17
use SimplePie\Exception\SimplePieException;
18
use SimplePie\Mixin as Tr;
19
use SimplePie\Parser\Date as DateParser;
20
21
/**
22
 * A type model for an Entry element.
23
 *
24
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#412-the-atomentry-element
25
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#535-items
26
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#55-item
27
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#elements-of-item
28
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#items
29
 */
30
class Entry extends AbstractType implements NodeInterface, BranchInterface, C\SetLoggerInterface
31
{
32
    use Tr\DateTrait;
33
    use Tr\DeepTypeTrait;
34
    use Tr\LoggerTrait;
35
36
    /**
37
     * The DOMNode element to parse.
38
     *
39
     * @var DOMNode
40
     */
41
    protected $node;
42
43
    /**
44
     * The preferred namespace alias for a given XML namespace URI. Should be
45
     * the result of a call to `SimplePie\Util\Ns`.
46
     *
47
     * @var string
48
     */
49
    protected $namespaceAlias;
50
51
    /**
52
     * Constructs a new instance of this class.
53
     *
54
     * @param string          $namespaceAlias [description]
55
     * @param DOMNode|null    $node           The `DOMNode` element to parse.
56
     * @param LoggerInterface $logger         The PSR-3 logger.
57
     *
58
     * phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
59
     */
60
    public function __construct(
61
        string $namespaceAlias,
62
        ?DOMNode $node = null,
63
        LoggerInterface $logger = null
64
    ) {
65
        // phpcs:enable
66
67
        $this->namespaceAlias = $namespaceAlias;
68
69
        if ($node) {
70
            $this->logger = $logger ?? new NullLogger();
71
            $this->node   = $node;
72
        }
73
    }
74
75
    /**
76
     * Converts this object into a string representation.
77
     *
78
     * @return string
79
     */
80
    public function __toString(): string
81
    {
82
        return \sprintf('<%s: resource %s>', \get_called_class(), \md5(\spl_object_hash($this)));
83
    }
84
85
    /**
86
     * Gets the DOMNode element.
87
     *
88
     * @return DOMNode|null
89
     */
90
    public function getNode(): ?DOMNode
91
    {
92
        return $this->node;
93
    }
94
95
    /**
96
     * Finds the common internal alias for a given method name.
97
     *
98
     * @param string $nodeName The name of the method being called.
99
     *
100
     * @return string
101
     */
102
    public function getAlias(string $nodeName): string
103
    {
104
        switch ($nodeName) {
105
            case 'categories':
106
                return 'category';
107
108
            case 'contributors':
109
                return 'contributor';
110
111
            case 'language':
112
                return 'lang';
113
114
            case 'links':
115
                return 'link';
116
117
            case 'pubDate':
118
            case 'publishDate':
119
            case 'publishedDate':
120
                return 'published';
121
122
            default:
123
                return $nodeName;
124
        }
125
    }
126
127
    /**
128
     * Get the correct handler for a whitelisted method name.
129
     *
130
     * @param string $nodeName The name of the method being called.
131
     * @param array  $args     Any arguments passed into that method.
132
     *
133
     * @throws SimplePieException
134
     *
135
     * @return mixed
136
     *
137
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
138
     */
139
    public function getHandler(string $nodeName, array $args = [])
140
    {
141
        switch ($nodeName) {
142
            case 'content':
143
            case 'id':
144
            case 'lang':
145
            case 'rights':
146
            case 'summary':
147
            case 'title':
148
                return $this->getScalarSingleValue($this, $nodeName, $args[0]);
149
150
            case 'published':
151
            case 'updated':
152
                return (new DateParser(
153
                    $this->getScalarSingleValue($this, $nodeName, $args[0])->getValue(),
154
                    $this->outputTimezone,
155
                    $this->createFromFormat
156
                ))->getDateTime();
157
158
            case 'author':
159
                return $this->getComplexSingleValue($this, $nodeName, Person::class, $args[0]);
160
161
            case 'category':
162
            case 'contributor':
163
            case 'link':
164
                return $this->getComplexMultipleValues($this, $nodeName, $args[0]);
165
166
            default:
167
                throw new SimplePieException(
168
                    $this->getUnresolvableMessage($nodeName)
169
                );
170
        }
171
    }
172
173
    // phpcs:enable
174
}
175