Completed
Push — master ( 4ce01f...2a02a0 )
by Ryan
12:33
created

Feed::getDefaultNs()   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 Psr\Log\NullLogger;
14
use SimplePie\Configuration as C;
15
use SimplePie\Exception\SimplePieException;
16
use SimplePie\Mixin as Tr;
17
use SimplePie\Parser\Date as DateParser;
18
use stdClass;
19
20
/**
21
 * The top-most element in a feed.
22
 *
23
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#411-the-atomfeed-element
24
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#53-channel
25
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#required-channel-elements
26
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#top-level
27
 *
28
 * phpcs:disable Generic.Files.LineLength.MaxExceeded
29
 *
30
 * @method SimplePie\Type\Person[] getAuthors(string $namespaceAlias) Returns the Authors associated with this feed.
31
 * @method SimplePie\Type\Category[] getCategories(string $namespaceAlias) Returns the list of Categories/Tags/Topics associated with this feed.
32
 * @method SimplePie\Type\Person[] getContributors(string $namespaceAlias) Returns the list of Contributors associated with this feed.
33
 * @method SimplePie\Type\Entry[] getEntries(string $namespaceAlias) Returns the list of Entries/Items associated with this feed.
34
 * @method SimplePie\Type\Generator getGenerator(string $namespaceAlias) Returns the Generator associated with this feed.
35
 * @method SimplePie\Type\Node getGuid(string $namespaceAlias) Alias for `getId()`.
36
 * @method SimplePie\Type\Node getId(string $namespaceAlias) Returns the ID associated with this feed.
37
 * @method SimplePie\Type\Image getIcon(string $namespaceAlias) Returns the Icon associated with this feed.
38
 * @method SimplePie\Type\Entry[] getItems(string $namespaceAlias) Alias for `getEntries()`.
39
 * @method SimplePie\Type\Node getLang(string $namespaceAlias) Alias for `getLanguage()`.
40
 * @method SimplePie\Type\Node getLanguage(string $namespaceAlias) Returns the language associated with this feed.
41
 * @method SimplePie\Type\Link[] getLinks(string $namespaceAlias, string $relFilter) Returns the list of Links associated with this feed.
42
 * @method SimplePie\Type\Image getLogo(string $namespaceAlias) Returns the Logo associated with this feed.
43
 * @method \DateTime getPubDate(string $namespaceAlias) Alias for `getPublished()`.
44
 * @method \DateTime getPublished(string $namespaceAlias) Returns the date that the feed was published.
45
 * @method SimplePie\Type\Node getRights(string $namespaceAlias) Returns the copyright information associated with this feed.
46
 * @method SimplePie\Type\Node getSubtitle(string $namespaceAlias) Returns the sub-title associated with this feed.
47
 * @method SimplePie\Type\Node getSummary(string $namespaceAlias) Returns the summary associated with this feed.
48
 * @method SimplePie\Type\Node getTitle(string $namespaceAlias) Returns the title associated with this feed.
49
 * @method \DateTime getUpdated(string $namespaceAlias) Returns the date that the feed was updated.
50
 *
51
 * phpcs:enable
52
 */
53
class Feed extends AbstractType implements BranchInterface, C\SetLoggerInterface
54
{
55
    use Tr\DateTrait;
56
    use Tr\DeepTypeTrait;
57
    use Tr\LoggerTrait;
58
    use Tr\RootTrait;
59
60
    /**
61
     * The preferred namespace alias for a given XML namespace URI. Should be
62
     * the result of a call to `SimplePie\Util\Ns`.
63
     *
64
     * @var string
65
     */
66
    protected $namespaceAlias;
67
68
    /**
69
     * Constructs a new instance of this class.
70
     *
71
     * @param string $namespaceAlias [description]
72
     */
73
    public function __construct(string $namespaceAlias)
74
    {
75
        $this->root           = new stdClass();
76
        $this->logger         = new NullLogger();
77
        $this->namespaceAlias = $namespaceAlias;
78
    }
79
80
    /**
81
     * Finds the default namespace alias for the feed type.
82
     *
83
     * @return string
84
     */
85
    public function getDefaultNs(): string
86
    {
87
        return $this->namespaceAlias;
88
    }
89
90
    /**
91
     * Finds the common internal alias for a given method name.
92
     *
93
     * @param string $nodeName The name of the method being called.
94
     *
95
     * @return string
96
     *
97
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
98
     */
99
    public function getAlias(string $nodeName): string
100
    {
101
        switch ($nodeName) {
102
            case 'authors':
103
                return 'author';
104
105
            case 'categories':
106
                return 'category';
107
108
            case 'contributors':
109
                return 'contributor';
110
111
            case 'entries':
112
            case 'items':
113
                return 'entry';
114
115
            case 'guid':
116
                return 'id';
117
118
            case 'language':
119
                return 'lang';
120
121
            case 'links':
122
                return 'link';
123
124
            case 'pubDate':
125
                return 'published';
126
127
            default:
128
                return $nodeName;
129
        }
130
    }
131
132
    // phpcs:enable
133
134
    /**
135
     * Get the correct handler for a whitelisted method name.
136
     *
137
     * @param string $nodeName The name of the method being called.
138
     * @param array  $args     Any arguments passed into that method. The default value is an empty array.
139
     *
140
     * @throws SimplePieException
141
     *
142
     * @return mixed Either `TypeInterface` or `TypeInterface[]`.
143
     *
144
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
145
     */
146
    public function getHandler(string $nodeName, array $args = [])
147
    {
148
        switch ($nodeName) {
149
            case 'id':
150
            case 'lang':
151
            case 'rights':
152
            case 'subtitle':
153
            case 'summary':
154
            case 'title':
155
                return $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null);
156
157
            case 'published':
158
            case 'updated':
159
                return (new DateParser(
160
                    $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null)->getValue(),
161
                    $this->outputTimezone,
162
                    $this->createFromFormat
163
                ))->getDateTime();
164
165
            case 'generator':
166
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Generator::class, $args[0] ?? null);
167
168
            case 'icon':
169
            case 'logo':
170
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Image::class, $args[0] ?? null);
171
172
            case 'author':
173
            case 'category':
174
            case 'contributor':
175
            case 'entry':
176
                return $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
177
178
            case 'link':
179
                $links = $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
180
181
                if (isset($args[1])) {
182
                    $relFilter = $args[1];
183
184
                    return \array_values(\array_filter($links, static function ($link) use ($relFilter) {
185
                        return $relFilter === $link->getRel()->getValue();
186
                    }));
187
                }
188
189
                return $links;
190
191
            default:
192
                throw new SimplePieException(
193
                    $this->getUnresolvableMessage($nodeName)
194
                );
195
        }
196
    }
197
198
    // phpcs:enable
199
}
200