1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Sitemap\Formatter; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Image; |
6
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex; |
7
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Url; |
8
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Video; |
9
|
|
|
|
10
|
|
|
class XmlFormatter extends BaseFormatter implements SitemapIndexFormatterInterface |
11
|
|
|
{ |
12
|
|
|
public function getSitemapStart() |
13
|
|
|
{ |
14
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset '.'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" '.'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n"; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getSitemapEnd() |
18
|
|
|
{ |
19
|
|
|
return '</urlset>'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getSitemapIndexStart() |
23
|
|
|
{ |
24
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n"; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getSitemapIndexEnd() |
28
|
|
|
{ |
29
|
|
|
return '</sitemapindex>'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function formatUrl(Url $url) |
33
|
|
|
{ |
34
|
|
|
return '<url>'."\n".$this->formatBody($url).'</url>'."\n"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function formatBody(Url $url) |
38
|
|
|
{ |
39
|
|
|
$buffer = "\t".'<loc>'.$this->escape($url->getLoc()).'</loc>'."\n"; |
40
|
|
|
|
41
|
|
|
$checks = [ |
42
|
|
|
'getLastmod' => "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n", |
43
|
|
|
'getChangefreq' => "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n", |
44
|
|
|
'getPriority' => "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n", |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
foreach ($checks as $check => $text) { |
48
|
|
|
if ($url->{$check}() !== null) { |
49
|
|
|
$buffer .= $text; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($url->getVideos() as $video) { |
54
|
|
|
$buffer .= $this->formatVideo($video); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($url->getImages() as $image) { |
58
|
|
|
$buffer .= $this->formatImage($image); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $buffer; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getChecks(Video $video, $keys = false) |
65
|
|
|
{ |
66
|
|
|
$checks = [ |
67
|
|
|
'getContentLoc' => "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n", |
68
|
|
|
'getDuration' => "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n", |
69
|
|
|
'getExpirationDate' => "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n", |
70
|
|
|
'getRating' => "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n", |
71
|
|
|
'getViewCount' => "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n", |
72
|
|
|
'getPublicationDate' => "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n", |
73
|
|
|
'getCategory' => "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n", |
74
|
|
|
'getRequiresSubscription' => "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n", |
75
|
|
|
'getLive' => "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n", |
76
|
|
|
'getFamilyFriendly' => "\t\t".'<video:family_friendly>'.($video->getFamilyFriendly() ? 'yes' : 'no').'</video:family_friendly>'."\n", |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
if ($keys === true) { |
80
|
|
|
return \array_keys($checks); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $checks; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function getExtended($keys = false) |
87
|
|
|
{ |
88
|
|
|
$checks = [ |
89
|
|
|
'getPlayerLoc' => 'checkVideoPlayerLoc', |
90
|
|
|
'getTags' => 'checkVideoTags', |
91
|
|
|
'getRestrictions' => 'checkVideoRestrictions', |
92
|
|
|
'getGalleryLoc' => 'checkVideoGalleryLoc', |
93
|
|
|
'getUploader' => 'checkVideoUploader', |
94
|
|
|
'getPlatforms' => 'checkVideoPlatforms', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
if ($keys === true) { |
98
|
|
|
return \array_keys($checks); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $checks; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function getCheckKeys(Video $video) |
105
|
|
|
{ |
106
|
|
|
return array_merge($this->getChecks($video, true), self::getExtended(true)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function formatVideo(Video $video) |
110
|
|
|
{ |
111
|
|
|
$buffer = $this->videoHeader($video); |
112
|
|
|
$checks = $this->getChecks($video); |
113
|
|
|
$extended = self::getExtended(); |
114
|
|
|
|
115
|
|
|
foreach ($this->getCheckKeys($video) as $key) { |
116
|
|
|
if ($video->{$key}() !== null) { |
117
|
|
|
if (isset($checks[$key])) { |
118
|
|
|
$buffer .= $checks[$key]; |
119
|
|
|
} elseif (isset($extended[$key])) { |
120
|
|
|
$buffer .= $this->{$extended[$key]}($video); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $buffer."\t".'</video:video>'."\n"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function videoHeader($video) |
129
|
|
|
{ |
130
|
|
|
$buffer = "\t".'<video:video>'."\n"; |
131
|
|
|
|
132
|
|
|
$buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n"; |
133
|
|
|
$buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n"; |
134
|
|
|
$buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n"; |
135
|
|
|
|
136
|
|
|
return $buffer; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function formatImage(Image $image) |
140
|
|
|
{ |
141
|
|
|
$buffer = "\t".'<image:image>'."\n\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n"; |
142
|
|
|
|
143
|
|
|
$checks = [ |
144
|
|
|
'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n", |
145
|
|
|
'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n", |
146
|
|
|
'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n", |
147
|
|
|
'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n", |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
foreach ($checks as $check => $text) { |
151
|
|
|
if ($image->{$check}() !== null) { |
152
|
|
|
$buffer .= $text; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $buffer."\t".'</image:image>'."\n"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function formatSitemapIndex(SitemapIndex $sitemapIndex) |
160
|
|
|
{ |
161
|
|
|
return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex) |
165
|
|
|
{ |
166
|
|
|
$buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n"; |
167
|
|
|
|
168
|
|
|
if ($sitemapIndex->getLastmod() !== null) { |
169
|
|
|
$buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $buffer; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function checkVideoPlayerLoc(Video $video) |
176
|
|
|
{ |
177
|
|
|
$playerLoc = $video->getPlayerLoc(); |
178
|
|
|
$allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no'; |
179
|
|
|
$autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : ''; |
180
|
|
|
|
181
|
|
|
return "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n"; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function checkVideoTags(Video $video) |
185
|
|
|
{ |
186
|
|
|
$text = ''; |
187
|
|
|
$tags = $video->getTags(); |
188
|
|
|
/** @var $tags array */ |
189
|
|
|
foreach ($tags as $tag) { |
190
|
|
|
$text .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n"; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $text; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function checkVideoRestrictions(Video $video) |
197
|
|
|
{ |
198
|
|
|
$restrictions = $video->getRestrictions(); |
199
|
|
|
$relationship = $this->escape($restrictions['relationship']); |
200
|
|
|
|
201
|
|
|
return "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function checkVideoGalleryLoc(Video $video) |
205
|
|
|
{ |
206
|
|
|
$galleryLoc = $video->getGalleryLoc(); |
207
|
|
|
$title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : ''; |
208
|
|
|
|
209
|
|
|
return "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n"; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
protected function checkVideoUploader(Video $video) |
213
|
|
|
{ |
214
|
|
|
$uploader = $video->getUploader(); |
215
|
|
|
$info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : ''; |
216
|
|
|
|
217
|
|
|
return "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n"; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function checkVideoPlatforms(Video $video) |
221
|
|
|
{ |
222
|
|
|
$text = ''; |
223
|
|
|
$platforms = $video->getPlatforms(); |
224
|
|
|
/** @var $platforms array */ |
225
|
|
|
foreach ($platforms as $platform => $relationship) { |
226
|
|
|
$text .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n"; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $text; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|