1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Wszetko\Sitemap\Items; |
5
|
|
|
|
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class News |
11
|
|
|
* |
12
|
|
|
* @package Wszetko\Sitemap\Items |
13
|
|
|
*/ |
14
|
|
|
class News extends Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Name of Namescapce |
18
|
|
|
*/ |
19
|
|
|
const NAMESPACE_NAME = 'news'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Namespace URL |
23
|
|
|
*/ |
24
|
|
|
const NAMESPACE_URL = 'http://www.google.com/schemas/sitemap-news/0.9'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Publication name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $publicationName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Publication language. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $publicationLanguage; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Access. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $access; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of genres, comma-separated string values. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $genres = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Date of publication. |
56
|
|
|
* |
57
|
|
|
* @var DateTimeInterface |
58
|
|
|
*/ |
59
|
|
|
protected $publicationDate; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Title. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $title; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Key words, comma-separated string values. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $keywords; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Key words, comma-separated string values. |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $stockTickers; |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* News constructor. |
85
|
|
|
* |
86
|
|
|
* @param string $publicationName |
87
|
|
|
* @param string $publicationLanguage |
88
|
|
|
* @param DateTimeInterface|string $publicationDate |
89
|
|
|
* @param string $title |
90
|
|
|
*/ |
91
|
|
|
public function __construct( |
92
|
|
|
string $publicationName, |
93
|
|
|
string $publicationLanguage, |
94
|
|
|
$publicationDate, |
95
|
|
|
string $title |
96
|
|
|
) { |
97
|
|
|
if (!empty($publicationName)) { |
98
|
|
|
$this->publicationName = $publicationName; |
99
|
|
|
} else { |
100
|
|
|
throw new InvalidArgumentException('Invalid publication name parameter.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
preg_match_all("/^(?'lang'zh-cn|zh-tw|([a-z]{2,3}))?$/", $publicationLanguage, $matches); |
104
|
|
|
|
105
|
|
|
if (!empty($matches['lang'])) { |
106
|
|
|
$this->publicationLanguage = $publicationLanguage; |
107
|
|
|
} else { |
108
|
|
|
throw new InvalidArgumentException('Invalid publication lang parameter.'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (is_string($publicationDate)) { |
112
|
|
|
$this->publicationDate = date_create($publicationDate); |
|
|
|
|
113
|
|
|
} elseif ($publicationDate instanceof DateTimeInterface) { |
|
|
|
|
114
|
|
|
$this->publicationDate = $publicationDate; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (($this->publicationDate && (int)$this->publicationDate->format("Y") < 0) || empty($this->publicationDate)) { |
118
|
|
|
throw new InvalidArgumentException('Invalid publication date parameter.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
$this->title = $title; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function toArray(): array |
129
|
|
|
{ |
130
|
|
|
$array = [ |
131
|
|
|
'_namespace' => static::NAMESPACE_NAME, |
132
|
|
|
'_element' => 'news', |
133
|
|
|
'news' => [ |
134
|
|
|
'publication' => [ |
135
|
|
|
'name' => $this->getPublicationName(), |
136
|
|
|
'language' => $this->getPublicationLanguage() |
137
|
|
|
], |
138
|
|
|
'publication_date' => $this->getPublicationDate(), |
139
|
|
|
'title' => $this->getTitle() |
140
|
|
|
] |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
if ($this->getAccess()) { |
144
|
|
|
$array['news']['access'] = $this->getAccess(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($this->getGenres()) { |
148
|
|
|
$array['news']['genres'] = $this->getGenres(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($this->getKeywords()) { |
152
|
|
|
$array['news']['keywords'] = $this->getKeywords(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->getStockTickers()) { |
156
|
|
|
$array['news']['stock_tickers'] = $this->getStockTickers(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $array; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Publication name. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getPublicationName() |
168
|
|
|
{ |
169
|
|
|
return $this->publicationName; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Publication language. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getPublicationLanguage() |
178
|
|
|
{ |
179
|
|
|
return $this->publicationLanguage; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Date of publication. |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getPublicationDate(): string |
188
|
|
|
{ |
189
|
|
|
if ($this->publicationDate->format('H') == 0 && |
190
|
|
|
$this->publicationDate->format('i') == 0 && |
191
|
|
|
$this->publicationDate->format('s') == 0) { |
192
|
|
|
return $this->publicationDate->format("Y-m-d"); |
193
|
|
|
} else { |
194
|
|
|
return $this->publicationDate->format(DateTimeInterface::W3C); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Title. |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getTitle() |
204
|
|
|
{ |
205
|
|
|
return $this->title; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Access. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public function getAccess() |
214
|
|
|
{ |
215
|
|
|
return $this->access; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set access. |
220
|
|
|
* |
221
|
|
|
* @param string $access |
222
|
|
|
* |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
|
|
public function setAccess(string $access): self |
226
|
|
|
{ |
227
|
|
|
if (in_array($access, ['Subscription', 'Registration'])) { |
228
|
|
|
$this->access = $access; |
229
|
|
|
} else { |
230
|
|
|
$this->access = null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* List of genres, comma-separated string values. |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getGenres() |
242
|
|
|
{ |
243
|
|
|
return implode(', ', $this->genres); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Key words, comma-separated string values. |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function getKeywords() |
252
|
|
|
{ |
253
|
|
|
return implode(', ', $this->keywords); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getStockTickers(): string |
260
|
|
|
{ |
261
|
|
|
return implode(', ', array_slice($this->stockTickers, 0, 5)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set list of genres, comma-separated string values. |
266
|
|
|
* |
267
|
|
|
* @param string $genres |
268
|
|
|
* |
269
|
|
|
* @return self |
270
|
|
|
*/ |
271
|
|
|
public function addGenres(string $genres): self |
272
|
|
|
{ |
273
|
|
|
$genres = explode(',', $genres); |
274
|
|
|
|
275
|
|
|
foreach ($genres as $genre) { |
276
|
|
|
$genre = trim($genre); |
277
|
|
|
if (in_array($genre, ['PressRelease', 'Satire', 'Blog', 'OpEd', 'Opinion', 'UserGenerated'])) { |
278
|
|
|
$this->genres[] = trim($genre); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Set key words, comma-separated string values. |
287
|
|
|
* |
288
|
|
|
* @param string $keywords |
289
|
|
|
* |
290
|
|
|
* @return self |
291
|
|
|
*/ |
292
|
|
|
public function addKeywords($keywords): self |
293
|
|
|
{ |
294
|
|
|
$keywords = explode(',', $keywords); |
295
|
|
|
|
296
|
|
|
foreach ($keywords as $keyword) { |
297
|
|
|
$this->keywords[] = trim($keyword); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param $stockTickers |
305
|
|
|
* |
306
|
|
|
* @return self |
307
|
|
|
*/ |
308
|
|
|
public function addStockTickers($stockTickers): self |
309
|
|
|
{ |
310
|
|
|
preg_match_all("/^(?'stockTickers'\w+:\w+(, *\w+:\w+){0,4})?$/", $stockTickers, $matches); |
311
|
|
|
|
312
|
|
|
if (!empty($matches['stockTickers'])) { |
313
|
|
|
$matches = explode(',', $matches['stockTickers'][0]); |
314
|
|
|
foreach ($matches as $match) { |
315
|
|
|
$this->stockTickers[] = trim($match); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.