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