1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thepixeldeveloper\Sitemap; |
4
|
|
|
|
5
|
|
|
use XMLWriter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Url |
9
|
|
|
* |
10
|
|
|
* @package Thepixeldeveloper\Sitemap |
11
|
|
|
*/ |
12
|
|
|
class Url implements OutputInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Location (URL). |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $loc; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Last modified time. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $lastMod; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Change frequency of the location. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $changeFreq; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Priority of page importance. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $priority; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Array of sub-elements. |
44
|
|
|
* |
45
|
|
|
* @var OutputInterface[] |
46
|
|
|
*/ |
47
|
|
|
protected $subelements = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sub-elements that append to the collection attributes. |
51
|
|
|
* |
52
|
|
|
* @var AppendAttributeInterface[] |
53
|
|
|
*/ |
54
|
|
|
protected $subelementsThatAppend = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Url constructor |
58
|
|
|
* |
59
|
|
|
* @param string $loc |
60
|
|
|
*/ |
61
|
|
|
public function __construct($loc) |
62
|
|
|
{ |
63
|
|
|
$this->loc = $loc; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param XMLWriter $XMLWriter |
68
|
|
|
*/ |
69
|
|
|
public function generateXML(XMLWriter $XMLWriter) |
70
|
|
|
{ |
71
|
|
|
foreach ($this->getSubelementsThatAppend() as $subelement) { |
72
|
|
|
$subelement->appendAttributeToCollectionXML($XMLWriter); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$XMLWriter->startElement('url'); |
76
|
|
|
$XMLWriter->writeElement('loc', $this->getLoc()); |
77
|
|
|
|
78
|
|
|
$this->optionalWriteElement($XMLWriter, 'lastmod', $this->getLastMod()); |
79
|
|
|
$this->optionalWriteElement($XMLWriter, 'changefreq', $this->getChangeFreq()); |
80
|
|
|
$this->optionalWriteElement($XMLWriter, 'priority', $this->getPriority()); |
81
|
|
|
|
82
|
|
|
foreach ($this->getSubelements() as $subelement) { |
83
|
|
|
$subelement->generateXML($XMLWriter); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$XMLWriter->endElement(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Array of sub-elements. |
91
|
|
|
* |
92
|
|
|
* @return OutputInterface[] |
93
|
|
|
*/ |
94
|
|
|
public function getSubelements() |
95
|
|
|
{ |
96
|
|
|
return $this->subelements; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Array of sub-elements that append to the collections attributes. |
101
|
|
|
* |
102
|
|
|
* @return AppendAttributeInterface[] |
103
|
|
|
*/ |
104
|
|
|
public function getSubelementsThatAppend() |
105
|
|
|
{ |
106
|
|
|
return $this->subelementsThatAppend; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get location (URL). |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getLoc() |
115
|
|
|
{ |
116
|
|
|
return $this->loc; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Only write the XML element if it has a truthy value. |
121
|
|
|
* |
122
|
|
|
* @param XMLWriter $XMLWriter |
123
|
|
|
* @param string $name |
124
|
|
|
* @param string $value |
125
|
|
|
*/ |
126
|
|
|
protected function optionalWriteElement(XMLWriter $XMLWriter, $name, $value) |
127
|
|
|
{ |
128
|
|
|
if ($value) { |
129
|
|
|
$XMLWriter->writeElement($name, $value); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get last modification time. |
135
|
|
|
* |
136
|
|
|
* @return null|string |
137
|
|
|
*/ |
138
|
|
|
public function getLastMod() |
139
|
|
|
{ |
140
|
|
|
return $this->lastMod; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set last modification time. |
145
|
|
|
* |
146
|
|
|
* @param string $lastMod |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setLastMod($lastMod) |
151
|
|
|
{ |
152
|
|
|
$this->lastMod = $lastMod; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get change frequency. |
159
|
|
|
* |
160
|
|
|
* @return null|string |
161
|
|
|
*/ |
162
|
|
|
public function getChangeFreq() |
163
|
|
|
{ |
164
|
|
|
return $this->changeFreq; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set change frequency. |
169
|
|
|
* |
170
|
|
|
* @param string $changeFreq |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setChangeFreq($changeFreq) |
175
|
|
|
{ |
176
|
|
|
$this->changeFreq = $changeFreq; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Url priority. |
183
|
|
|
* |
184
|
|
|
* @return null|string |
185
|
|
|
*/ |
186
|
|
|
public function getPriority() |
187
|
|
|
{ |
188
|
|
|
return $this->priority; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Set priority. |
193
|
|
|
* |
194
|
|
|
* @param string $priority |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function setPriority($priority) |
199
|
|
|
{ |
200
|
|
|
$this->priority = $priority; |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Add a new sub element. |
207
|
|
|
* |
208
|
|
|
* @param OutputInterface $subElement |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
|
|
*/ |
212
|
|
|
public function addSubElement(OutputInterface $subElement) |
213
|
|
|
{ |
214
|
|
|
$this->subelements[] = $subElement; |
215
|
|
|
|
216
|
|
|
if ($this->isSubelementGoingToAppend($subElement)) { |
217
|
|
|
$this->subelementsThatAppend[get_class($subElement)] = $subElement; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Checks if the sub-element is going to append collection attributes. |
225
|
|
|
* |
226
|
|
|
* @param OutputInterface $subelement |
227
|
|
|
* |
228
|
|
|
* @return boolean |
229
|
|
|
*/ |
230
|
|
|
protected function isSubelementGoingToAppend(OutputInterface $subelement) |
231
|
|
|
{ |
232
|
|
|
if (!$subelement instanceof AppendAttributeInterface) { |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return !in_array(get_class($subelement), $this->subelementsThatAppend, false); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|