1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
2
|
|
|
|
3
|
|
|
use DateTime; |
4
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract; |
5
|
|
|
/** |
6
|
|
|
* Class Url |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Url implements UrlContract |
12
|
|
|
{ |
13
|
|
|
/* ----------------------------------------------------------------- |
14
|
|
|
| Properties |
15
|
|
|
| ----------------------------------------------------------------- |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $url; |
20
|
|
|
|
21
|
|
|
/** @var string|null */ |
22
|
|
|
protected $title; |
23
|
|
|
|
24
|
|
|
/** @var \Carbon\Carbon */ |
25
|
|
|
protected $lastModDate; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $changeFrequency; |
29
|
|
|
|
30
|
|
|
/** @var float */ |
31
|
|
|
protected $priority; |
32
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
34
|
|
|
| Constructor |
35
|
|
|
| ----------------------------------------------------------------- |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Url constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $loc |
42
|
|
|
*/ |
43
|
46 |
|
public function __construct($loc) |
44
|
|
|
{ |
45
|
46 |
|
$this->setLoc($loc); |
46
|
46 |
|
$this->setLastMod(new DateTime); |
47
|
46 |
|
$this->setChangeFreq(static::CHANGE_FREQUENCY_DAILY); |
48
|
46 |
|
$this->setPriority(0.8); |
49
|
46 |
|
} |
50
|
|
|
|
51
|
|
|
/* ----------------------------------------------------------------- |
52
|
|
|
| Getters & Setters |
53
|
|
|
| ----------------------------------------------------------------- |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the url. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
36 |
|
public function getLoc() |
62
|
|
|
{ |
63
|
36 |
|
return $this->url; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the url. |
68
|
|
|
* |
69
|
|
|
* @param string $url |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
46 |
|
public function setLoc($url) |
74
|
|
|
{ |
75
|
46 |
|
$this->url = $url; |
76
|
|
|
|
77
|
46 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the last modification date. |
82
|
|
|
* |
83
|
|
|
* @return DateTime |
84
|
|
|
*/ |
85
|
24 |
|
public function getLastMod() |
86
|
|
|
{ |
87
|
24 |
|
return $this->lastModDate; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the last modification date. |
92
|
|
|
* |
93
|
|
|
* @param string|DateTime $lastModDate |
94
|
|
|
* @param string $format |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
46 |
|
public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s') |
99
|
|
|
{ |
100
|
46 |
|
$this->lastModDate = gettype($lastModDate) === 'string' |
|
|
|
|
101
|
32 |
|
? DateTime::createFromFormat($format, $lastModDate) |
102
|
46 |
|
: $lastModDate; |
103
|
|
|
|
104
|
46 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the change frequency. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
22 |
|
public function getChangeFreq() |
113
|
|
|
{ |
114
|
22 |
|
return $this->changeFrequency; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set the change frequency. |
119
|
|
|
* |
120
|
|
|
* @param string $changeFreq |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
46 |
|
public function setChangeFreq($changeFreq) |
125
|
|
|
{ |
126
|
46 |
|
$this->changeFrequency = $changeFreq; |
127
|
|
|
|
128
|
46 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the priority. |
133
|
|
|
* |
134
|
|
|
* @return float |
135
|
|
|
*/ |
136
|
10 |
|
public function getPriority() |
137
|
|
|
{ |
138
|
10 |
|
return $this->priority; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the priority. |
143
|
|
|
* |
144
|
|
|
* @param float $priority |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
46 |
|
public function setPriority($priority) |
149
|
|
|
{ |
150
|
46 |
|
$this->priority = (float) $priority; |
151
|
|
|
|
152
|
46 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the title. |
157
|
|
|
* |
158
|
|
|
* @return string|null |
159
|
|
|
*/ |
160
|
4 |
|
public function getTitle() |
161
|
|
|
{ |
162
|
4 |
|
return $this->title; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the title. |
167
|
|
|
* |
168
|
|
|
* @param string $title |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
26 |
|
public function setTitle($title) |
173
|
|
|
{ |
174
|
26 |
|
$this->title = $title; |
175
|
|
|
|
176
|
26 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/* ----------------------------------------------------------------- |
180
|
|
|
| Main Methods |
181
|
|
|
| ----------------------------------------------------------------- |
182
|
|
|
*/ |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Create a sitemap url instance. |
186
|
|
|
* |
187
|
|
|
* @param string $loc |
188
|
|
|
* |
189
|
|
|
* @return \Arcanedev\LaravelSitemap\Entities\Url |
190
|
|
|
*/ |
191
|
30 |
|
public static function create($loc) |
192
|
|
|
{ |
193
|
30 |
|
return new static($loc); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the collection of items as a plain array. |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
12 |
|
public function toArray() |
202
|
|
|
{ |
203
|
|
|
return [ |
204
|
12 |
|
'loc' => $this->getLoc(), |
205
|
12 |
|
'lastmod' => $this->getLastMod()->format(DateTime::ATOM), |
206
|
12 |
|
'changefreq' => $this->getChangeFreq(), |
207
|
12 |
|
'priority' => $this->priority, |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the sitemap url as JSON. |
213
|
|
|
* |
214
|
|
|
* @param int $options |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
2 |
|
public function toJson($options = 0) |
219
|
|
|
{ |
220
|
2 |
|
return json_encode($this->jsonSerialize(), $options); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Convert the object into something JSON serializable. |
225
|
|
|
* |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
2 |
|
public function jsonSerialize() |
229
|
|
|
{ |
230
|
2 |
|
return $this->toArray(); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.