Passed
Push — master ( 5620d1...59a82b )
by Ryan
11:31
created

Feed::getComplexSingleValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
    // 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 'contributors':
141
                return 'contributor';
142
143
            case 'links':
144
                return 'link';
145
146
            case 'language':
147
                return 'lang';
148
149
            case 'pubDate':
150
            case 'publishedDate':
151
                return 'published';
152
153
            default:
154
                return $nodeName;
155
        }
156
    }
157
158
    /**
159
     * Get the correct handler for a whitelisted method name.
160
     *
161
     * @param string $nodeName The name of the method being called.
162
     * @param array  $args     Any arguments passed into that method.
163
     *
164
     * @throws SimplePieException
165
     *
166
     * @return mixed
167
     */
168
    protected function getHandler(string $nodeName, array $args)
169
    {
170
        switch ($nodeName) {
171
            case 'id':
172
            case 'lang':
173
            case 'rights':
174
            case 'subtitle':
175
            case 'summary':
176
            case 'title':
177
                return $this->getScalarSingleValue($nodeName, $args[0]);
178
179
            case 'published':
180
            case 'updated':
181
                return (new DateParser(
182
                    $this->getScalarSingleValue($nodeName, $args[0])->getValue(),
183
                    $this->outputTimezone,
184
                    $this->createFromFormat
185
                ))->getDateTime();
186
187
            case 'author':
188
                return $this->getComplexSingleValue($nodeName, Person::class, $args[0]);
189
190
            case 'generator':
191
                return $this->getComplexSingleValue($nodeName, Generator::class, $args[0]);
192
193
            case 'icon':
194
            case 'logo':
195
                return $this->getComplexSingleValue($nodeName, Image::class, $args[0]);
196
197
            case 'contributor':
198
            case 'link':
199
                return $this->getComplexMultipleValues($nodeName, $args[0]);
200
201
            default:
202
                throw new SimplePieException(
203
                    $this->getUnresolvableMessage($nodeName)
204
                );
205
        }
206
    }
207
208
    /**
209
     * Retrieves nodes that are simple scalars, and there is only one allowed value.
210
     *
211
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
212
     *                                    looking at the response from `getRoot()`.
213
     * @param string|null $namespaceAlias The XML namespace alias to apply.
214
     *
215
     * @return Node
216
     */
217
    protected function getScalarSingleValue(string $nodeName, ?string $namespaceAlias = null): Node
218
    {
219
        $alias = $namespaceAlias ?? $this->namespaceAlias;
220
221
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
222
            return $this->getRoot()->{$nodeName}[$alias];
223
        }
224
225
        return new Node();
226
    }
227
228
    /**
229
     * Retrieves nodes that are complex types, and there is only one allowed value.
230
     *
231
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
232
     *                                    looking at the response from `getRoot()`.
233
     * @param string      $className      The class name to instantiate when there is not a defined value.
234
     * @param string|null $namespaceAlias The XML namespace alias to apply.
235
     *
236
     * @return TypeInterface
237
     *
238
     * @codingStandardsIgnoreStart
239
     */
240
    protected function getComplexSingleValue(
241
        string $nodeName,
242
        string $className,
243
        ?string $namespaceAlias = null
244
    ): TypeInterface {
245
        // @codingStandardsIgnoreEnd
246
247
        $alias = $namespaceAlias ?? $this->namespaceAlias;
248
249
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
250
            return new $className($this->getRoot()->{$nodeName}[$alias]->getNode());
251
        }
252
253
        return new $className();
254
    }
255
256
    /**
257
     * Retrieves nodes that are complex types, and there may be are more than one value.
258
     *
259
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
260
     *                                    looking at the response from `getRoot()`.
261
     * @param string|null $namespaceAlias The XML namespace alias to apply.
262
     *
263
     * @return iterable
264
     */
265
    protected function getComplexMultipleValues(string $nodeName, ?string $namespaceAlias = null): iterable
266
    {
267
        $alias = $namespaceAlias ?? $this->namespaceAlias;
268
269
        if (isset($this->getRoot()->{$nodeName}[$alias])) {
270
            return $this->getRoot()->{$nodeName}[$alias];
271
        }
272
273
        return [];
274
    }
275
}
276