Passed
Push — master ( b157e7...5032a7 )
by Ryan
24:05 queued 09:54
created

Feed::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
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 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
    // MULTIPLE COMPLEX VALUES
112
113
    public function getItems(): void
114
    {
115
    }
116
117
    //--------------------------------------------------------------------------
118
    // INTERNAL
119
120
    /**
121
     * Retrieve the root-most node in the feed.
122
     *
123
     * @return stdClass
124
     */
125
    public function getRoot(): stdClass
126
    {
127
        return $this->root;
128
    }
129
130
    /**
131
     * Finds the common internal alias for a given method name.
132
     *
133
     * @param string $nodeName The name of the method being called.
134
     *
135
     * @return string
136
     */
137
    protected function getAlias(string $nodeName): string
138
    {
139
        switch ($nodeName) {
140
            case 'categories':
141
                return 'category';
142
143
            case 'contributors':
144
                return 'contributor';
145
146
            case 'language':
147
                return 'lang';
148
149
            case 'links':
150
                return 'link';
151
152
            case 'pubDate':
153
            case 'publishDate':
154
            case 'publishedDate':
155
                return 'published';
156
157
            default:
158
                return $nodeName;
159
        }
160
    }
161
162
    /**
163
     * Get the correct handler for a whitelisted method name.
164
     *
165
     * @param string $nodeName The name of the method being called.
166
     * @param array  $args     Any arguments passed into that method.
167
     *
168
     * @throws SimplePieException
169
     *
170
     * @return mixed
171
     *
172
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
173
     */
174
    protected function getHandler(string $nodeName, array $args)
175
    {
176
        switch ($nodeName) {
177
            case 'id':
178
            case 'lang':
179
            case 'rights':
180
            case 'subtitle':
181
            case 'summary':
182
            case 'title':
183
                return $this->getScalarSingleValue($nodeName, $args[0]);
184
185
            case 'published':
186
            case 'updated':
187
                return (new DateParser(
188
                    $this->getScalarSingleValue($nodeName, $args[0])->getValue(),
189
                    $this->outputTimezone,
190
                    $this->createFromFormat
191
                ))->getDateTime();
192
193
            case 'author':
194
                return $this->getComplexSingleValue($nodeName, Person::class, $args[0]);
195
196
            case 'generator':
197
                return $this->getComplexSingleValue($nodeName, Generator::class, $args[0]);
198
199
            case 'icon':
200
            case 'logo':
201
                return $this->getComplexSingleValue($nodeName, Image::class, $args[0]);
202
203
            case 'category':
204
            case 'contributor':
205
            case 'link':
206
                return $this->getComplexMultipleValues($nodeName, $args[0]);
207
208
            default:
209
                throw new SimplePieException(
210
                    $this->getUnresolvableMessage($nodeName)
211
                );
212
        }
213
    }
214
215
    // phpcs:enable
216
217
    /**
218
     * Retrieves nodes that are simple scalars, and there is only one allowed value.
219
     *
220
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
221
     *                                    looking at the response from `getRoot()`.
222
     * @param string|null $namespaceAlias The XML namespace alias to apply.
223
     *
224
     * @return Node
225
     */
226
    protected function getScalarSingleValue(string $nodeName, ?string $namespaceAlias = null): Node
227
    {
228
        $alias = $namespaceAlias ?? $this->namespaceAlias;
229
230
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
231
            return $this->getRoot()->{$nodeName}[$alias];
232
        }
233
234
        return new Node();
235
    }
236
237
    /**
238
     * Retrieves nodes that are complex types, and there is only one allowed value.
239
     *
240
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
241
     *                                    looking at the response from `getRoot()`.
242
     * @param string      $className      The class name to instantiate when there is not a defined value.
243
     * @param string|null $namespaceAlias The XML namespace alias to apply.
244
     *
245
     * @return TypeInterface
246
     *
247
     * phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
248
     */
249
    protected function getComplexSingleValue(
250
        string $nodeName,
251
        string $className,
252
        ?string $namespaceAlias = null
253
    ): TypeInterface {
254
        // phpcs:enable
255
256
        $alias = $namespaceAlias ?? $this->namespaceAlias;
257
258
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
259
            return new $className($this->getRoot()->{$nodeName}[$alias]->getNode());
260
        }
261
262
        return new $className();
263
    }
264
265
    /**
266
     * Retrieves nodes that are complex types, and there may be are more than one value.
267
     *
268
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
269
     *                                    looking at the response from `getRoot()`.
270
     * @param string|null $namespaceAlias The XML namespace alias to apply.
271
     *
272
     * @return iterable
273
     */
274
    protected function getComplexMultipleValues(string $nodeName, ?string $namespaceAlias = null): iterable
275
    {
276
        $alias = $namespaceAlias ?? $this->namespaceAlias;
277
278
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
279
            return $this->getRoot()->{$nodeName}[$alias];
280
        }
281
282
        return [];
283
    }
284
}
285