Completed
Push — master ( 35068d...08b675 )
by Ryan
03:33
created

Entry::getAlias()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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