Passed
Push — master ( eef7fd...128351 )
by Ryan
12:25
created

Feed   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 250
rs 9.2
c 0
b 0
f 0
wmc 34

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGenerator() 0 9 2
A getAuthor() 0 9 2
A setDateFormat() 0 5 1
A getScalarSingleValue() 0 9 2
A setOutputTimezone() 0 5 1
A getContributors() 0 9 2
C getHandler() 0 24 11
A getIcon() 0 9 2
A getLinks() 0 9 2
A getAlias() 0 12 4
A getRoot() 0 3 1
A getLogo() 0 9 2
A getItems() 0 2 1
1
<?php
2
/**
3
 * Copyright (c) 2017 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Type;
12
13
use DateTime;
14
use DateTimeZone;
15
use Psr\Log\NullLogger;
16
use SimplePie\Configuration as C;
17
use SimplePie\Exception\SimplePieException;
18
use SimplePie\Mixin\LoggerTrait;
19
use SimplePie\Parser\Date as DateParser;
20
use stdClass;
21
22
/**
23
 * Represents the top-level of a feed.
24
 */
25
class Feed extends AbstractType implements TypeInterface, C\SetLoggerInterface
26
{
27
    use LoggerTrait;
28
29
    /**
30
     * The root-most node in the feed.
31
     *
32
     * @var stdClass
33
     */
34
    protected $root;
35
36
    /**
37
     * The preferred namespace alias for a given XML namespace URI. Should be
38
     * the result of a call to `SimplePie\Util\Ns`.
39
     *
40
     * @var string
41
     */
42
    protected $namespaceAlias;
43
44
    /**
45
     * The format that should be used when determining how to parse a date from a date string.
46
     *
47
     * @var string
48
     */
49
    protected $createFromFormat;
50
51
    /**
52
     * The preferred timezone to use for date output.
53
     *
54
     * @var string
55
     */
56
    protected $outputTimezone;
57
58
    /**
59
     * Constructs a new instance of this class.
60
     *
61
     * @param string $namespaceAlias [description]
62
     */
63
    public function __construct(string $namespaceAlias)
64
    {
65
        $this->root           = new stdClass();
66
        $this->logger         = new NullLogger();
67
        $this->namespaceAlias = $namespaceAlias;
68
    }
69
70
    /**
71
     * Allows the user to help the date parser by providing the format of the datestamp in the feed.
72
     *
73
     * This will be passed into `DateTime::createFromFormat()` at parse-time.
74
     *
75
     * @param string $createFromFormat The format of the datestamp in the feed.
76
     *
77
     * @return self
78
     *
79
     * @see http://php.net/manual/en/datetime.createfromformat.php
80
     */
81
    public function setDateFormat(string $createFromFormat): self
82
    {
83
        $this->createFromFormat = $createFromFormat;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Set the preferred output timezone.
90
     *
91
     * This calculation is performed on a _best-effort_ basis and is not guaranteed. Factors which may affect the
92
     * calculation include:
93
     *
94
     * * the version of glibc/musl that your OS relies on
95
     * * the freshness of the timestamp data your OS relies on
96
     * * the format of the datestamp inside of the feed and PHP's ability to parse it
97
     *
98
     * @param string $timezone The timezone identifier to use. Must be compatible with `DateTimeZone`. The default
99
     *                         value is `UTC`.
100
     *
101
     * @return self
102
     */
103
    public function setOutputTimezone(string $timezone = 'UTC'): self
104
    {
105
        $this->outputTimezone = $timezone;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Retrieves nodes that are simple scalars, and there are only one allowed value.
112
     *
113
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
114
     *                                    looking at the response from `getRoot()`.
115
     * @param string|null $namespaceAlias The XML namespace alias to apply.
116
     *
117
     * @return Node
118
     */
119
    public function getScalarSingleValue(string $nodeName, ?string $namespaceAlias = null): Node
120
    {
121
        $alias = $namespaceAlias ?? $this->namespaceAlias;
122
123
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
124
            return $this->getRoot()->{$nodeName}[$alias];
125
        }
126
127
        return new Node();
128
    }
129
130
    //--------------------------------------------------------------------------
131
    // SINGLE COMPLEX VALUES
132
133
    public function getGenerator(?string $namespaceAlias = null): Generator
134
    {
135
        $alias = $namespaceAlias ?? $this->namespaceAlias;
136
137
        if (isset($this->getRoot()->generator[$alias])) {
138
            return $this->getRoot()->generator[$alias];
139
        }
140
141
        return new Generator();
142
    }
143
144
    public function getAuthor(?string $namespaceAlias = null): Person
145
    {
146
        $alias = $namespaceAlias ?? $this->namespaceAlias;
147
148
        if (isset($this->getRoot()->author[$alias])) {
149
            return $this->getRoot()->author[$alias];
150
        }
151
152
        return new Person();
153
    }
154
155
    public function getIcon(?string $namespaceAlias = null): Image
156
    {
157
        $alias = $namespaceAlias ?? $this->namespaceAlias;
158
159
        if (isset($this->getRoot()->icon[$alias])) {
160
            return new Image($this->getRoot()->icon[$alias]->getNode());
161
        }
162
163
        return new Image();
164
    }
165
166
    public function getLogo(?string $namespaceAlias = null): Image
167
    {
168
        $alias = $namespaceAlias ?? $this->namespaceAlias;
169
170
        if (isset($this->getRoot()->logo[$alias])) {
171
            return new Image($this->getRoot()->logo[$alias]->getNode());
172
        }
173
174
        return new Image();
175
    }
176
177
    //--------------------------------------------------------------------------
178
    // MULTIPLE COMPLEX VALUES
179
180
    public function getContributors(?string $namespaceAlias = null): iterable
181
    {
182
        $alias = $namespaceAlias ?? $this->namespaceAlias;
183
184
        if (isset($this->getRoot()->contributor[$alias])) {
185
            return $this->getRoot()->contributor[$alias];
186
        }
187
188
        return [];
189
    }
190
191
    public function getLinks(?string $namespaceAlias = null): iterable
192
    {
193
        $alias = $namespaceAlias ?? $this->namespaceAlias;
194
195
        if (isset($this->getRoot()->link[$alias])) {
196
            return $this->getRoot()->link[$alias];
197
        }
198
199
        return [];
200
    }
201
202
    public function getItems(): void
203
    {
204
    }
205
206
    //--------------------------------------------------------------------------
207
    // INTERNAL
208
209
    /**
210
     * Retrieve the root-most node in the feed.
211
     *
212
     * @return stdClass
213
     */
214
    public function getRoot(): stdClass
215
    {
216
        return $this->root;
217
    }
218
219
    /**
220
     * Finds the common internal alias for a given method name.
221
     *
222
     * @param string $nodeName The name of the method being called.
223
     *
224
     * @return string
225
     */
226
    protected function getAlias(string $nodeName): string
227
    {
228
        switch ($nodeName) {
229
            case 'language':
230
                return 'lang';
231
232
            case 'pubDate':
233
            case 'publishedDate':
234
                return 'published';
235
236
            default:
237
                return $nodeName;
238
        }
239
    }
240
241
    /**
242
     * Get the correct handler for a whitelisted method name.
243
     *
244
     * @param string $nodeName The name of the method being called.
245
     * @param array  $args     Any arguments passed into that method.
246
     *
247
     * @throws SimplePieException
248
     *
249
     * @return mixed
250
     */
251
    protected function getHandler(string $nodeName, array $args)
252
    {
253
        switch ($nodeName) {
254
            case 'icon':
255
            case 'id':
256
            case 'lang':
257
            case 'logo':
258
            case 'rights':
259
            case 'subtitle':
260
            case 'summary':
261
            case 'title':
262
                return $this->getScalarSingleValue($nodeName, $args[0]);
263
264
            case 'published':
265
            case 'updated':
266
                return (new DateParser(
267
                    $this->getScalarSingleValue($nodeName, $args[0])->getValue(),
268
                    $this->outputTimezone,
269
                    $this->createFromFormat
270
                ))->getDateTime();
271
272
            default:
273
                throw new SimplePieException(
274
                    $this->getUnresolvableMessage($nodeName)
275
                );
276
        }
277
    }
278
}
279