1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract; |
4
|
|
|
use Arcanedev\LaravelSitemap\Exceptions\SitemapException; |
5
|
|
|
use ArrayAccess; |
6
|
|
|
use DateTime; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Url |
12
|
|
|
* |
13
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Url implements ArrayAccess, UrlContract |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $loc; |
25
|
|
|
|
26
|
|
|
/** @var string|null */ |
27
|
|
|
protected $title; |
28
|
|
|
|
29
|
|
|
/** @var \DateTimeInterface */ |
30
|
|
|
protected $lastModDate; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $changeFrequency; |
34
|
|
|
|
35
|
|
|
/** @var float */ |
36
|
|
|
protected $priority; |
37
|
|
|
|
38
|
|
|
/* ----------------------------------------------------------------- |
39
|
|
|
| Constructor |
40
|
|
|
| ----------------------------------------------------------------- |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Url constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $loc |
47
|
|
|
*/ |
48
|
72 |
|
public function __construct($loc) |
49
|
|
|
{ |
50
|
72 |
|
$this->setLoc($loc); |
51
|
72 |
|
$this->setLastMod(new DateTime); |
52
|
72 |
|
$this->setChangeFreq(ChangeFrequency::DAILY); |
53
|
72 |
|
$this->setPriority(0.8); |
54
|
72 |
|
} |
55
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
57
|
|
|
| Getters & Setters |
58
|
|
|
| ----------------------------------------------------------------- |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the url location. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
54 |
|
public function getLoc() |
67
|
|
|
{ |
68
|
54 |
|
return $this->escape($this->loc); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the url location (alias). |
73
|
|
|
* |
74
|
|
|
* @see getLoc() |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
14 |
|
public function loc() |
79
|
|
|
{ |
80
|
14 |
|
return $this->getLoc(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the url location. |
85
|
|
|
* |
86
|
|
|
* @param string $loc |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
* |
90
|
|
|
* @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException |
91
|
|
|
*/ |
92
|
72 |
|
public function setLoc($loc) |
93
|
|
|
{ |
94
|
72 |
|
$this->loc = $this->checkLoc($loc); |
95
|
|
|
|
96
|
72 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the last modification date. |
101
|
|
|
* |
102
|
|
|
* @return \DateTimeInterface |
103
|
|
|
*/ |
104
|
30 |
|
public function getLastMod() |
105
|
|
|
{ |
106
|
30 |
|
return $this->lastModDate; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the last modification date (alias). |
111
|
|
|
* |
112
|
|
|
* @see getLastMod() |
113
|
|
|
* |
114
|
|
|
* @return \DateTimeInterface |
115
|
|
|
*/ |
116
|
10 |
|
public function lastMod() |
117
|
|
|
{ |
118
|
10 |
|
return $this->getLastMod(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Format the url last modification. |
123
|
|
|
* |
124
|
|
|
* @param string $format |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
24 |
|
public function formatLastMod($format = DateTime::ATOM) |
129
|
|
|
{ |
130
|
24 |
|
return $this->getLastMod()->format($format); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the last modification date. |
135
|
|
|
* |
136
|
|
|
* @param string|\DateTimeInterface $lastModDate |
137
|
|
|
* @param string $format |
138
|
|
|
* |
139
|
|
|
* @return self |
140
|
|
|
*/ |
141
|
72 |
|
public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s') |
142
|
|
|
{ |
143
|
72 |
|
$this->lastModDate = gettype($lastModDate) === 'string' |
144
|
46 |
|
? DateTime::createFromFormat($format, $lastModDate) |
145
|
72 |
|
: $lastModDate; |
146
|
|
|
|
147
|
72 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the change frequency. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
28 |
|
public function getChangeFreq() |
156
|
|
|
{ |
157
|
28 |
|
return $this->changeFrequency; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the change frequency (alias). |
162
|
|
|
* |
163
|
|
|
* @see getChangeFreq() |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
10 |
|
public function changeFreq() |
168
|
|
|
{ |
169
|
10 |
|
return $this->getChangeFreq(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the change frequency. |
174
|
|
|
* |
175
|
|
|
* @param string $changeFreq |
176
|
|
|
* |
177
|
|
|
* @return self |
178
|
|
|
*/ |
179
|
72 |
|
public function setChangeFreq($changeFreq) |
180
|
|
|
{ |
181
|
72 |
|
$this->changeFrequency = strtolower(trim($changeFreq)); |
182
|
|
|
|
183
|
72 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the priority. |
188
|
|
|
* |
189
|
|
|
* @return float |
190
|
|
|
*/ |
191
|
28 |
|
public function getPriority() |
192
|
|
|
{ |
193
|
28 |
|
return $this->priority; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the priority (alias). |
198
|
|
|
* |
199
|
|
|
* @see getPriority() |
200
|
|
|
* |
201
|
|
|
* @return float |
202
|
|
|
*/ |
203
|
10 |
|
public function priority() |
204
|
|
|
{ |
205
|
10 |
|
return $this->getPriority(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the priority. |
210
|
|
|
* |
211
|
|
|
* @param float $priority |
212
|
|
|
* |
213
|
|
|
* @return self |
214
|
|
|
* |
215
|
|
|
* @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException |
216
|
|
|
*/ |
217
|
72 |
|
public function setPriority($priority) |
218
|
|
|
{ |
219
|
72 |
|
$this->priority = $this->checkPriority($priority); |
220
|
|
|
|
221
|
72 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the title. |
226
|
|
|
* |
227
|
|
|
* @return string|null |
228
|
|
|
*/ |
229
|
24 |
|
public function getTitle() |
230
|
|
|
{ |
231
|
24 |
|
return $this->escape($this->title); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get the title. |
236
|
|
|
* |
237
|
|
|
* @param string $title |
238
|
|
|
* |
239
|
|
|
* @return self |
240
|
|
|
*/ |
241
|
44 |
|
public function setTitle($title) |
242
|
|
|
{ |
243
|
44 |
|
$this->title = $title; |
244
|
|
|
|
245
|
44 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/* ----------------------------------------------------------------- |
249
|
|
|
| Main Methods |
250
|
|
|
| ----------------------------------------------------------------- |
251
|
|
|
*/ |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Create a sitemap url instance. |
255
|
|
|
* |
256
|
|
|
* @param string $loc |
257
|
|
|
* |
258
|
|
|
* @return \Arcanedev\LaravelSitemap\Entities\Url |
259
|
|
|
*/ |
260
|
56 |
|
public static function make($loc) |
261
|
|
|
{ |
262
|
56 |
|
return new static($loc); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Make a URL instance with attributes. |
267
|
|
|
* |
268
|
|
|
* @param array $attributes |
269
|
|
|
* |
270
|
|
|
* @return \Arcanedev\LaravelSitemap\Entities\Url |
271
|
|
|
*/ |
272
|
2 |
|
public static function makeFromArray(array $attributes) |
273
|
|
|
{ |
274
|
2 |
|
return static::make(Arr::get($attributes, 'loc')) |
275
|
2 |
|
->setChangeFreq(Arr::get($attributes, 'changefreq', ChangeFrequency::DAILY)) |
276
|
2 |
|
->setLastMod(Arr::get($attributes, 'lastmod', new DateTime)) |
277
|
2 |
|
->setPriority(Arr::get($attributes, 'priority', 0.8)) |
278
|
2 |
|
->setTitle(Arr::get($attributes, 'title')); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the collection of items as a plain array. |
283
|
|
|
* |
284
|
|
|
* @return array |
285
|
|
|
*/ |
286
|
14 |
|
public function toArray() |
287
|
|
|
{ |
288
|
|
|
return [ |
289
|
14 |
|
'title' => $this->getTitle(), |
290
|
14 |
|
'loc' => $this->getLoc(), |
291
|
14 |
|
'lastmod' => $this->formatLastMod(), |
292
|
14 |
|
'changefreq' => $this->getChangeFreq(), |
293
|
14 |
|
'priority' => $this->getPriority(), |
294
|
|
|
]; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Get the sitemap url as JSON. |
299
|
|
|
* |
300
|
|
|
* @param int $options |
301
|
|
|
* |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
2 |
|
public function toJson($options = 0) |
305
|
|
|
{ |
306
|
2 |
|
return json_encode($this->jsonSerialize(), $options); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Convert the object into something JSON serializable. |
311
|
|
|
* |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
2 |
|
public function jsonSerialize() |
315
|
|
|
{ |
316
|
2 |
|
return $this->toArray(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/* ----------------------------------------------------------------- |
320
|
|
|
| Other Methods |
321
|
|
|
| ----------------------------------------------------------------- |
322
|
|
|
*/ |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Escape the given value. |
326
|
|
|
* |
327
|
|
|
* @param string $value |
328
|
|
|
* |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
56 |
|
protected function escape($value) |
332
|
|
|
{ |
333
|
56 |
|
if (is_null($value)) |
334
|
14 |
|
return $value; |
335
|
|
|
|
336
|
56 |
|
return config('sitemap.escaping', true) |
337
|
56 |
|
? htmlentities($value, ENT_XML1, 'UTF-8') |
338
|
56 |
|
: $value; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Determine if the given attribute exists. |
343
|
|
|
* |
344
|
|
|
* @param mixed $offset |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
2 |
|
public function offsetExists($offset) |
349
|
|
|
{ |
350
|
2 |
|
return method_exists($this, 'get'.Str::studly($offset)); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Get the value for a given offset. |
355
|
|
|
* |
356
|
|
|
* @param mixed $offset |
357
|
|
|
* |
358
|
|
|
* @return mixed |
359
|
|
|
*/ |
360
|
2 |
|
public function offsetGet($offset) |
361
|
|
|
{ |
362
|
2 |
|
return call_user_func([$this, 'get'.Str::studly($offset)]); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Set the value for a given offset. |
367
|
|
|
* |
368
|
|
|
* @param mixed $offset |
369
|
|
|
* @param mixed $value |
370
|
|
|
* |
371
|
|
|
* @return void |
372
|
|
|
*/ |
373
|
|
|
public function offsetSet($offset, $value) {} // Do nothing... |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Unset the value for a given offset. |
377
|
|
|
* |
378
|
|
|
* @param mixed $offset |
379
|
|
|
* |
380
|
|
|
* @return void |
381
|
|
|
*/ |
382
|
|
|
public function offsetUnset($offset) {} // Do nothing... |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Check the loc value. |
386
|
|
|
* |
387
|
|
|
* @param string $loc |
388
|
|
|
* |
389
|
|
|
* @return string |
390
|
|
|
* |
391
|
|
|
* @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException |
392
|
|
|
*/ |
393
|
72 |
|
protected function checkLoc($loc) |
394
|
|
|
{ |
395
|
72 |
|
if (is_null($loc) || ! is_string($loc)) |
396
|
4 |
|
throw new SitemapException('The [loc] attribute is required and must be string value.'); |
397
|
|
|
|
398
|
72 |
|
return $loc; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Check the priority value. |
403
|
|
|
* |
404
|
|
|
* @param float $priority |
405
|
|
|
* |
406
|
|
|
* @return float |
407
|
|
|
* |
408
|
|
|
* @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException |
409
|
|
|
*/ |
410
|
72 |
|
protected function checkPriority($priority) |
411
|
|
|
{ |
412
|
72 |
|
if ( ! is_numeric($priority)) |
413
|
2 |
|
throw new SitemapException("The [priority] value must be numeric."); |
414
|
|
|
|
415
|
72 |
|
$priority = round($priority, 1); |
416
|
|
|
|
417
|
72 |
|
if ($priority > 1 || $priority < 0) |
418
|
2 |
|
throw new SitemapException("The [priority] value must be between `0.0` and `1.0`, `{$priority}` was given."); |
419
|
|
|
|
420
|
72 |
|
return $priority; |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|