Passed
Push — master ( e52ce1...eef7fd )
by Ryan
13:31
created

Feed::getLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
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
     * 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
    //--------------------------------------------------------------------------
156
    // MULTIPLE COMPLEX VALUES
157
158
    public function getContributors(?string $namespaceAlias = null): iterable
159
    {
160
        $alias = $namespaceAlias ?? $this->namespaceAlias;
161
162
        if (isset($this->getRoot()->contributor[$alias])) {
163
            return $this->getRoot()->contributor[$alias];
164
        }
165
166
        return [];
167
    }
168
169
    public function getLinks(?string $namespaceAlias = null): iterable
170
    {
171
        $alias = $namespaceAlias ?? $this->namespaceAlias;
172
173
        if (isset($this->getRoot()->link[$alias])) {
174
            return $this->getRoot()->link[$alias];
175
        }
176
177
        return [];
178
    }
179
180
    public function getItems(): void
181
    {
182
    }
183
184
    //--------------------------------------------------------------------------
185
    // INTERNAL
186
187
    /**
188
     * Retrieve the root-most node in the feed.
189
     *
190
     * @return stdClass
191
     */
192
    public function getRoot(): stdClass
193
    {
194
        return $this->root;
195
    }
196
197
    /**
198
     * Finds the common internal alias for a given method name.
199
     *
200
     * @param string $nodeName The name of the method being called.
201
     *
202
     * @return string
203
     */
204
    protected function getAlias(string $nodeName): string
205
    {
206
        switch ($nodeName) {
207
            case 'language':
208
                return 'lang';
209
210
            case 'pubDate':
211
            case 'publishedDate':
212
                return 'published';
213
214
            default:
215
                return $nodeName;
216
        }
217
    }
218
219
    /**
220
     * Get the correct handler for a whitelisted method name.
221
     *
222
     * @param string $nodeName The name of the method being called.
223
     * @param array  $args     Any arguments passed into that method.
224
     *
225
     * @throws SimplePieException
226
     *
227
     * @return mixed
228
     */
229
    protected function getHandler(string $nodeName, array $args)
230
    {
231
        switch ($nodeName) {
232
            case 'id':
233
            case 'lang':
234
            case 'rights':
235
            case 'subtitle':
236
            case 'summary':
237
            case 'title':
238
                return $this->getScalarSingleValue($nodeName, $args[0]);
239
240
            case 'published':
241
            case 'updated':
242
                return (new DateParser(
243
                    $this->getScalarSingleValue($nodeName, $args[0])->getValue(),
244
                    $this->outputTimezone,
245
                    $this->createFromFormat
246
                ))->getDateTime();
247
248
            default:
249
                throw new SimplePieException(
250
                    \sprintf('%s is an unresolvable method.')
251
                );
252
        }
253
    }
254
}
255