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