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

Feed::getAlias()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 1
dl 0
loc 27
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
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 getId(string $namespaceAlias) Returns the ID associated with this feed.
36
 * @method SimplePie\Type\Image getIcon(string $namespaceAlias) Returns the Icon associated with this feed.
37
 * @method SimplePie\Type\Entry[] getItems(string $namespaceAlias) Alias for `getEntries()`.
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 feed.
40
 * @method SimplePie\Type\Link[] getLinks(string $namespaceAlias) Returns the list of Links associated with this feed.
41
 * @method SimplePie\Type\Image getLogo(string $namespaceAlias) Returns the Logo associated with this feed.
42
 * @method \DateTime getPubDate(string $namespaceAlias) Alias for `getPublished()`.
43
 * @method \DateTime getPublished(string $namespaceAlias) Returns the date that the feed was published.
44
 * @method SimplePie\Type\Node getRights(string $namespaceAlias) Returns the copyright information associated with this feed.
45
 * @method SimplePie\Type\Node getSubtitle(string $namespaceAlias) Returns the sub-title associated with this feed.
46
 * @method SimplePie\Type\Node getSummary(string $namespaceAlias) Returns the summary associated with this feed.
47
 * @method SimplePie\Type\Node getTitle(string $namespaceAlias) Returns the title associated with this feed.
48
 * @method \DateTime getUpdated(string $namespaceAlias) Returns the date that the feed was updated.
49
 *
50
 * phpcs:enable
51
 */
52
class Feed extends AbstractType implements BranchInterface, C\SetLoggerInterface
53
{
54
    use Tr\DateTrait;
55
    use Tr\DeepTypeTrait;
56
    use Tr\LoggerTrait;
57
    use Tr\RootTrait;
58
59
    /**
60
     * The preferred namespace alias for a given XML namespace URI. Should be
61
     * the result of a call to `SimplePie\Util\Ns`.
62
     *
63
     * @var string
64
     */
65
    protected $namespaceAlias;
66
67
    /**
68
     * Constructs a new instance of this class.
69
     *
70
     * @param string $namespaceAlias [description]
71
     */
72
    public function __construct(string $namespaceAlias)
73
    {
74
        $this->root           = new stdClass();
75
        $this->logger         = new NullLogger();
76
        $this->namespaceAlias = $namespaceAlias;
77
    }
78
79
    /**
80
     * Finds the common internal alias for a given method name.
81
     *
82
     * @param string $nodeName The name of the method being called.
83
     *
84
     * @return string
85
     *
86
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
87
     */
88
    public function getAlias(string $nodeName): string
89
    {
90
        switch ($nodeName) {
91
            case 'authors':
92
                return 'author';
93
94
            case 'categories':
95
                return 'category';
96
97
            case 'contributors':
98
                return 'contributor';
99
100
            case 'entries':
101
            case 'items':
102
                return 'entry';
103
104
            case 'language':
105
                return 'lang';
106
107
            case 'links':
108
                return 'link';
109
110
            case 'pubDate':
111
                return 'published';
112
113
            default:
114
                return $nodeName;
115
        }
116
    }
117
118
    // phpcs:enable
119
120
    /**
121
     * Get the correct handler for a whitelisted method name.
122
     *
123
     * @param string $nodeName The name of the method being called.
124
     * @param array  $args     Any arguments passed into that method. The default value is an empty array.
125
     *
126
     * @throws SimplePieException
127
     *
128
     * @return mixed Either `TypeInterface` or `TypeInterface[]`.
129
     *
130
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
131
     */
132
    public function getHandler(string $nodeName, array $args = [])
133
    {
134
        switch ($nodeName) {
135
            case 'id':
136
            case 'lang':
137
            case 'rights':
138
            case 'subtitle':
139
            case 'summary':
140
            case 'title':
141
                return $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null);
142
143
            case 'published':
144
            case 'updated':
145
                return (new DateParser(
146
                    $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null)->getValue(),
147
                    $this->outputTimezone,
148
                    $this->createFromFormat
149
                ))->getDateTime();
150
151
            case 'generator':
152
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Generator::class, $args[0] ?? null);
153
154
            case 'icon':
155
            case 'logo':
156
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Image::class, $args[0] ?? null);
157
158
            case 'author':
159
            case 'category':
160
            case 'contributor':
161
            case 'entry':
162
            case 'link':
163
                return $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
164
165
            default:
166
                throw new SimplePieException(
167
                    $this->getUnresolvableMessage($nodeName)
168
                );
169
        }
170
    }
171
172
    // phpcs:enable
173
}
174