Completed
Push — master ( 2991a0...bc5f9a )
by Ryan
03:06
created

Entry::getNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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