Completed
Push — master ( 8146b2...061ea8 )
by Ryan
03:02
created

Entry::getAlias()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
                            return $relFilter === $link->/** @scrutinizer ignore-call */ getRel()->getValue();
Loading history...
200
                        })
201
                    );
202
                }
203
204
                return $links;
205
206
            default:
207
                throw new SimplePieException(
208
                    $this->getUnresolvableMessage($nodeName)
209
                );
210
        }
211
    }
212
213
    // @phpcs:enable
214
}
215