|
1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use ArrayAccess; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class SitemapItem |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class SitemapItem implements ArrayAccess |
|
12
|
|
|
{ |
|
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
14
|
|
|
| Properties |
|
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* URL of the page. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $loc; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The date of last modification of the file. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $lastmod; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* How frequently the page is likely to change. |
|
33
|
|
|
* Valid values: always|hourly|daily|weekly|monthly|yearly|never |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $freq; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The priority of this URL relative to other URLs on your site. |
|
41
|
|
|
* Valid values range from 0.0 to 1.0 |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $priority; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string */ |
|
48
|
|
|
protected $title; |
|
49
|
|
|
|
|
50
|
|
|
/** @var array */ |
|
51
|
|
|
protected $images = []; |
|
52
|
|
|
|
|
53
|
|
|
/** @var array */ |
|
54
|
|
|
protected $videos = []; |
|
55
|
|
|
|
|
56
|
|
|
/** @var array */ |
|
57
|
|
|
protected $translations = []; |
|
58
|
|
|
|
|
59
|
|
|
/** @var array */ |
|
60
|
|
|
protected $googlenews = []; |
|
61
|
|
|
|
|
62
|
|
|
/** @var array */ |
|
63
|
|
|
protected $alternates = []; |
|
64
|
|
|
|
|
65
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
66
|
|
|
| Constructor |
|
67
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
68
|
|
|
*/ |
|
69
|
|
|
/** |
|
70
|
|
|
* SitemapItem constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $params |
|
73
|
|
|
* @param bool $escape |
|
74
|
|
|
*/ |
|
75
|
144 |
|
public function __construct(array $params, $escape = true) |
|
76
|
|
|
{ |
|
77
|
144 |
|
$this->setLoc(array_get($params, 'loc', '/')); |
|
78
|
144 |
|
$this->setLastmod(array_get($params, 'lastmod')); |
|
79
|
144 |
|
$this->setPriority(array_get($params, 'priority')); |
|
80
|
144 |
|
$this->setFreq(array_get($params, 'freq')); |
|
81
|
144 |
|
$this->setTitle(array_get($params, 'title')); |
|
82
|
144 |
|
$this->setImages(array_get($params, 'images', [])); |
|
83
|
144 |
|
$this->setVideos(array_get($params, 'videos', [])); |
|
84
|
144 |
|
$this->setTranslations(array_get($params, 'translations', [])); |
|
85
|
144 |
|
$this->setGooglenews(array_get($params, 'googlenews', [])); |
|
86
|
144 |
|
$this->setAlternates(array_get($params, 'alternates', [])); |
|
87
|
|
|
|
|
88
|
144 |
|
if ($escape) $this->escape(); |
|
89
|
144 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
92
|
|
|
| Getters & Setters |
|
93
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
94
|
|
|
*/ |
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
48 |
|
public function getLoc() |
|
99
|
|
|
{ |
|
100
|
48 |
|
return $this->loc; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $loc |
|
105
|
|
|
* |
|
106
|
|
|
* @return self |
|
107
|
|
|
*/ |
|
108
|
144 |
|
public function setLoc($loc) |
|
109
|
|
|
{ |
|
110
|
144 |
|
$this->loc = $loc; |
|
111
|
|
|
|
|
112
|
144 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param bool $format |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
48 |
|
public function getLastmod($format = true) |
|
121
|
|
|
{ |
|
122
|
48 |
|
return ($format && $this->lastmod !== null) |
|
123
|
44 |
|
? date('c', strtotime($this->lastmod)) |
|
124
|
48 |
|
: $this->lastmod; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param \DateTime|string $lastmod |
|
129
|
|
|
* |
|
130
|
|
|
* @return self |
|
131
|
|
|
*/ |
|
132
|
144 |
|
public function setLastmod($lastmod) |
|
133
|
|
|
{ |
|
134
|
144 |
|
if ($lastmod instanceof \DateTime) { |
|
135
|
8 |
|
$lastmod = $lastmod->format('Y-m-d H:i:s'); |
|
136
|
6 |
|
} |
|
137
|
|
|
|
|
138
|
144 |
|
$this->lastmod = $lastmod; |
|
139
|
|
|
|
|
140
|
144 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
24 |
|
public function getPriority() |
|
147
|
|
|
{ |
|
148
|
24 |
|
return $this->priority; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $priority |
|
153
|
|
|
* |
|
154
|
|
|
* @return self |
|
155
|
|
|
*/ |
|
156
|
144 |
|
public function setPriority($priority) |
|
157
|
|
|
{ |
|
158
|
144 |
|
$this->priority = $priority; |
|
159
|
|
|
|
|
160
|
144 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
24 |
|
public function getFreq() |
|
167
|
|
|
{ |
|
168
|
24 |
|
return $this->freq; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $freq |
|
173
|
|
|
* |
|
174
|
|
|
* @return self |
|
175
|
|
|
*/ |
|
176
|
144 |
|
public function setFreq($freq) |
|
177
|
|
|
{ |
|
178
|
144 |
|
$this->freq = $freq; |
|
179
|
|
|
|
|
180
|
144 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
24 |
|
public function getTitle() |
|
187
|
|
|
{ |
|
188
|
24 |
|
return $this->title; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $title |
|
193
|
|
|
* |
|
194
|
|
|
* @return self |
|
195
|
|
|
*/ |
|
196
|
144 |
|
public function setTitle($title) |
|
197
|
|
|
{ |
|
198
|
144 |
|
$this->title = $title; |
|
199
|
|
|
|
|
200
|
144 |
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
24 |
|
public function getImages() |
|
207
|
|
|
{ |
|
208
|
24 |
|
return $this->images; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param array $images |
|
213
|
|
|
* |
|
214
|
|
|
* @return self |
|
215
|
|
|
*/ |
|
216
|
144 |
|
public function setImages(array $images) |
|
217
|
|
|
{ |
|
218
|
144 |
|
$this->images = $images; |
|
219
|
|
|
|
|
220
|
144 |
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
24 |
|
public function getVideos() |
|
227
|
|
|
{ |
|
228
|
24 |
|
return $this->videos; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param array $videos |
|
233
|
|
|
* |
|
234
|
|
|
* @return self |
|
235
|
|
|
*/ |
|
236
|
144 |
|
public function setVideos(array $videos) |
|
237
|
|
|
{ |
|
238
|
144 |
|
$this->videos = $videos; |
|
239
|
|
|
|
|
240
|
144 |
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
24 |
|
public function getTranslations() |
|
247
|
|
|
{ |
|
248
|
24 |
|
return $this->translations; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param array $translations |
|
253
|
|
|
* |
|
254
|
|
|
* @return self |
|
255
|
|
|
*/ |
|
256
|
144 |
|
public function setTranslations(array $translations) |
|
257
|
|
|
{ |
|
258
|
144 |
|
$this->translations = $translations; |
|
259
|
|
|
|
|
260
|
144 |
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @return array |
|
265
|
|
|
*/ |
|
266
|
24 |
|
public function getGooglenews() |
|
267
|
|
|
{ |
|
268
|
24 |
|
return $this->googlenews; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param array $googlenews |
|
273
|
|
|
* |
|
274
|
|
|
* @return self |
|
275
|
|
|
*/ |
|
276
|
144 |
|
public function setGooglenews(array $googlenews) |
|
277
|
|
|
{ |
|
278
|
144 |
|
$this->googlenews['sitename'] = array_get($googlenews, 'sitename', ''); |
|
279
|
144 |
|
$this->googlenews['language'] = array_get($googlenews, 'language', 'en'); |
|
280
|
144 |
|
$this->googlenews['publication_date'] = array_get($googlenews, 'publication_date', date('Y-m-d H:i:s')); |
|
281
|
|
|
|
|
282
|
144 |
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @return array |
|
287
|
|
|
*/ |
|
288
|
24 |
|
public function getAlternates() |
|
289
|
|
|
{ |
|
290
|
24 |
|
return $this->alternates; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @param array $alternates |
|
295
|
|
|
* |
|
296
|
|
|
* @return self |
|
297
|
|
|
*/ |
|
298
|
144 |
|
public function setAlternates(array $alternates) |
|
299
|
|
|
{ |
|
300
|
144 |
|
$this->alternates = $alternates; |
|
301
|
|
|
|
|
302
|
144 |
|
return $this; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
306
|
|
|
| ArrayAccess Functions |
|
307
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
308
|
|
|
*/ |
|
309
|
|
|
/** |
|
310
|
|
|
* Whether a offset exists |
|
311
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
312
|
|
|
* |
|
313
|
|
|
* @param mixed $offset An offset to check for. |
|
314
|
|
|
* |
|
315
|
|
|
* @return bool |
|
316
|
|
|
*/ |
|
317
|
8 |
|
public function offsetExists($offset) |
|
318
|
|
|
{ |
|
319
|
8 |
|
return property_exists($this, $offset); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Offset to retrieve |
|
324
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
325
|
|
|
* |
|
326
|
|
|
* @param mixed $offset The offset to retrieve. |
|
327
|
|
|
* |
|
328
|
|
|
* @return mixed |
|
329
|
|
|
*/ |
|
330
|
24 |
|
public function offsetGet($offset) |
|
331
|
|
|
{ |
|
332
|
24 |
|
$method = 'get'.ucfirst($offset); |
|
333
|
|
|
|
|
334
|
24 |
|
return method_exists($this, $method) ? $this->{$method}() : null; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Offset to set |
|
339
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
340
|
|
|
* |
|
341
|
|
|
* @param mixed $offset The offset to assign the value to. |
|
342
|
|
|
* @param mixed $value The value to set. |
|
343
|
|
|
*/ |
|
344
|
16 |
|
public function offsetSet($offset, $value) |
|
345
|
|
|
{ |
|
346
|
16 |
|
$method = 'set'.ucfirst($offset); |
|
347
|
|
|
|
|
348
|
16 |
|
if (method_exists($this, $method)) { |
|
349
|
16 |
|
$this->{$method}($value); |
|
350
|
12 |
|
} |
|
351
|
16 |
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Offset to unset |
|
355
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
356
|
|
|
* |
|
357
|
|
|
* @param mixed $offset The offset to unset. |
|
358
|
|
|
*/ |
|
359
|
8 |
|
public function offsetUnset($offset) |
|
360
|
|
|
{ |
|
361
|
8 |
|
$this->offsetSet($offset, null); |
|
362
|
8 |
|
} |
|
363
|
|
|
|
|
364
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
365
|
|
|
| Main Functions |
|
366
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
367
|
|
|
*/ |
|
368
|
|
|
/** |
|
369
|
|
|
* Make a new sitemap item. |
|
370
|
|
|
* |
|
371
|
|
|
* @param array $params |
|
372
|
|
|
* @param bool $escape |
|
373
|
|
|
* |
|
374
|
|
|
* @return self |
|
375
|
|
|
*/ |
|
376
|
24 |
|
public static function make(array $params, $escape = true) |
|
377
|
|
|
{ |
|
378
|
24 |
|
return new self($params, $escape); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Escaping the sitemap item. |
|
383
|
|
|
*/ |
|
384
|
144 |
|
public function escape() |
|
385
|
|
|
{ |
|
386
|
144 |
|
$this->escapeLoc(); |
|
387
|
144 |
|
$this->escapeTitle(); |
|
388
|
144 |
|
$this->escapeImages(); |
|
389
|
144 |
|
$this->escapeTranslations(); |
|
390
|
144 |
|
$this->escapeAlternates(); |
|
391
|
144 |
|
$this->escapeVideos(); |
|
392
|
144 |
|
$this->escapeGooglenews(); |
|
393
|
144 |
|
} |
|
394
|
|
|
|
|
395
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
396
|
|
|
| Other Functions |
|
397
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
398
|
|
|
*/ |
|
399
|
144 |
|
private function escapeLoc() |
|
400
|
|
|
{ |
|
401
|
144 |
|
$this->setLoc(htmlentities($this->loc, ENT_XML1)); |
|
402
|
144 |
|
} |
|
403
|
|
|
|
|
404
|
144 |
|
private function escapeTitle() |
|
405
|
|
|
{ |
|
406
|
144 |
|
if ( ! is_null($this->title)) { |
|
407
|
8 |
|
$this->setTitle(htmlentities($this->title, ENT_XML1)); |
|
408
|
6 |
|
} |
|
409
|
144 |
|
} |
|
410
|
|
|
|
|
411
|
144 |
|
private function escapeImages() |
|
412
|
|
|
{ |
|
413
|
144 |
|
if ($this->images) { |
|
|
|
|
|
|
414
|
8 |
|
foreach ($this->images as $k => $image) { |
|
415
|
8 |
|
foreach ($image as $key => $value) { |
|
416
|
8 |
|
$this->images[$k][$key] = htmlentities($value, ENT_XML1); |
|
417
|
6 |
|
} |
|
418
|
6 |
|
} |
|
419
|
6 |
|
} |
|
420
|
144 |
|
} |
|
421
|
|
|
|
|
422
|
144 |
|
private function escapeTranslations() |
|
423
|
|
|
{ |
|
424
|
144 |
|
if ($this->translations) { |
|
|
|
|
|
|
425
|
8 |
|
foreach ($this->translations as $k => $translation) { |
|
426
|
8 |
|
foreach ($translation as $key => $value) { |
|
427
|
8 |
|
$this->translations[$k][$key] = htmlentities($value, ENT_XML1); |
|
428
|
6 |
|
} |
|
429
|
6 |
|
} |
|
430
|
6 |
|
} |
|
431
|
144 |
|
} |
|
432
|
|
|
|
|
433
|
144 |
|
private function escapeAlternates() |
|
434
|
|
|
{ |
|
435
|
144 |
|
if ($this->alternates) { |
|
|
|
|
|
|
436
|
8 |
|
foreach ($this->alternates as $k => $alternate) { |
|
437
|
8 |
|
foreach ($alternate as $key => $value) { |
|
438
|
8 |
|
$this->alternates[$k][$key] = htmlentities($value, ENT_XML1); |
|
439
|
6 |
|
} |
|
440
|
6 |
|
} |
|
441
|
6 |
|
} |
|
442
|
144 |
|
} |
|
443
|
|
|
|
|
444
|
144 |
|
private function escapeVideos() |
|
445
|
|
|
{ |
|
446
|
144 |
|
if ($this->videos) { |
|
|
|
|
|
|
447
|
8 |
|
foreach ($this->videos as $k => $video) { |
|
448
|
8 |
|
if ($video['title']) |
|
449
|
8 |
|
$this->videos[$k]['title'] = htmlentities($video['title'], ENT_XML1); |
|
450
|
8 |
|
if ($video['description']) |
|
451
|
8 |
|
$this->videos[$k]['description'] = htmlentities($video['description'], ENT_XML1); |
|
452
|
6 |
|
} |
|
453
|
6 |
|
} |
|
454
|
144 |
|
} |
|
455
|
|
|
|
|
456
|
144 |
|
private function escapeGooglenews() |
|
457
|
|
|
{ |
|
458
|
144 |
|
if ($this->googlenews && isset($this->googlenews['sitename'])) { |
|
|
|
|
|
|
459
|
144 |
|
$this->googlenews['sitename'] = htmlentities($this->googlenews['sitename'], ENT_XML1); |
|
460
|
108 |
|
} |
|
461
|
144 |
|
} |
|
462
|
|
|
} |
|
463
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.