1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Sitemap\Formatter; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Image; |
6
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Url; |
7
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Video; |
8
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex; |
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
|
|
View Code Duplication |
if ($url->getLastmod() !== null) { |
|
|
|
|
42
|
|
|
$buffer .= "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($url->getChangefreq() !== null) { |
46
|
|
|
$buffer .= "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($url->getPriority() !== null) { |
50
|
|
|
$buffer .= "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n"; |
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
|
|
|
public function formatSitemapIndex(SitemapIndex $sitemapIndex) |
65
|
|
|
{ |
66
|
|
|
return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex) |
70
|
|
|
{ |
71
|
|
|
$buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n"; |
72
|
|
|
|
73
|
|
View Code Duplication |
if ($sitemapIndex->getLastmod() !== null) { |
|
|
|
|
74
|
|
|
$buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $buffer; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function formatVideo(Video $video) |
81
|
|
|
{ |
82
|
|
|
$buffer = "\t".'<video:video>'."\n"; |
83
|
|
|
|
84
|
|
|
$buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n"; |
85
|
|
|
$buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n"; |
86
|
|
|
$buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n"; |
87
|
|
|
|
88
|
|
View Code Duplication |
if ($video->getContentLoc() !== null) { |
|
|
|
|
89
|
|
|
$buffer .= "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($video->getPlayerLoc() !== null) { |
93
|
|
|
$playerLoc = $video->getPlayerLoc(); |
94
|
|
|
$allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no'; |
95
|
|
|
$autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : ''; |
96
|
|
|
|
97
|
|
|
$buffer .= "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
if ($video->getDuration() !== null) { |
|
|
|
|
101
|
|
|
$buffer .= "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if ($video->getExpirationDate() !== null) { |
|
|
|
|
105
|
|
|
$buffer .= "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
if ($video->getRating() !== null) { |
|
|
|
|
109
|
|
|
$buffer .= "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
if ($video->getViewCount() !== null) { |
|
|
|
|
113
|
|
|
$buffer .= "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($video->getPublicationDate() !== null) { |
|
|
|
|
117
|
|
|
$buffer .= "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($video->getFamilyFriendly() === false) { |
121
|
|
|
$buffer .= "\t\t".'<video:family_friendly>no</video:family_friendly>'."\n"; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($video->getTags() !== null) { |
|
|
|
|
125
|
|
|
foreach ($video->getTags() as $tag) { |
126
|
|
|
$buffer .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n"; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
if ($video->getCategory() !== null) { |
|
|
|
|
131
|
|
|
$buffer .= "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n"; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($video->getRestrictions() !== null) { |
135
|
|
|
$restrictions = $video->getRestrictions(); |
136
|
|
|
$relationship = $this->escape($restrictions['relationship']); |
137
|
|
|
|
138
|
|
|
$buffer .= "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n"; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
if ($video->getGalleryLoc() !== null) { |
|
|
|
|
142
|
|
|
$galleryLoc = $video->getGalleryLoc(); |
143
|
|
|
$title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : ''; |
144
|
|
|
|
145
|
|
|
$buffer .= "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
if ($video->getRequiresSubscription() !== null) { |
|
|
|
|
149
|
|
|
$buffer .= "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
if ($video->getUploader() !== null) { |
|
|
|
|
153
|
|
|
$uploader = $video->getUploader(); |
154
|
|
|
$info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : ''; |
155
|
|
|
|
156
|
|
|
$buffer .= "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($video->getPlatforms() !== null) { |
160
|
|
|
foreach ($video->getPlatforms() as $platform => $relationship) { |
161
|
|
|
$buffer .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n"; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
View Code Duplication |
if ($video->getLive() !== null) { |
|
|
|
|
166
|
|
|
$buffer .= "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n"; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $buffer."\t".'</video:video>'."\n"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function formatImage(Image $image) |
173
|
|
|
{ |
174
|
|
|
$buffer = "\t".'<image:image>'."\n"; |
175
|
|
|
|
176
|
|
|
$buffer .= "\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n"; |
177
|
|
|
|
178
|
|
View Code Duplication |
if ($image->getCaption() !== null) { |
|
|
|
|
179
|
|
|
$buffer .= "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n"; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
if ($image->getGeoLocation() !== null) { |
|
|
|
|
183
|
|
|
$buffer .= "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n"; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
if ($image->getTitle() !== null) { |
|
|
|
|
187
|
|
|
$buffer .= "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n"; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
if ($image->getLicense() !== null) { |
|
|
|
|
191
|
|
|
$buffer .= "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n"; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $buffer."\t".'</image:image>'."\n"; |
195
|
|
|
} |
196
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.