Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-feed/src/Writer/Renderer/Feed/AbstractAtom.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
5 * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 */ | |
9 | |
10 namespace Zend\Feed\Writer\Renderer\Feed; | |
11 | |
12 use DateTime; | |
13 use DOMDocument; | |
14 use DOMElement; | |
15 use Zend\Feed\Writer; | |
16 use Zend\Feed\Writer\Renderer; | |
17 use Zend\Feed\Writer\Version; | |
18 | |
19 /** | |
20 */ | |
21 class AbstractAtom extends Renderer\AbstractRenderer | |
22 { | |
23 /** | |
24 * Constructor | |
25 * | |
26 * @param Writer\AbstractFeed $container | |
27 */ | |
28 public function __construct($container) | |
29 { | |
30 parent::__construct($container); | |
31 } | |
32 | |
33 /** | |
34 * Set feed language | |
35 * | |
36 * @param DOMDocument $dom | |
37 * @param DOMElement $root | |
38 * @return void | |
39 */ | |
40 protected function _setLanguage(DOMDocument $dom, DOMElement $root) | |
41 { | |
42 if ($this->getDataContainer()->getLanguage()) { | |
43 $root->setAttribute('xml:lang', $this->getDataContainer() | |
44 ->getLanguage()); | |
45 } | |
46 } | |
47 | |
48 /** | |
49 * Set feed title | |
50 * | |
51 * @param DOMDocument $dom | |
52 * @param DOMElement $root | |
53 * @return void | |
54 * @throws Writer\Exception\InvalidArgumentException | |
55 */ | |
56 protected function _setTitle(DOMDocument $dom, DOMElement $root) | |
57 { | |
58 if (!$this->getDataContainer()->getTitle()) { | |
59 $message = 'Atom 1.0 feed elements MUST contain exactly one' | |
60 . ' atom:title element but a title has not been set'; | |
61 $exception = new Writer\Exception\InvalidArgumentException($message); | |
62 if (!$this->ignoreExceptions) { | |
63 throw $exception; | |
64 } else { | |
65 $this->exceptions[] = $exception; | |
66 return; | |
67 } | |
68 } | |
69 | |
70 $title = $dom->createElement('title'); | |
71 $root->appendChild($title); | |
72 $title->setAttribute('type', 'text'); | |
73 $text = $dom->createTextNode($this->getDataContainer()->getTitle()); | |
74 $title->appendChild($text); | |
75 } | |
76 | |
77 /** | |
78 * Set feed description | |
79 * | |
80 * @param DOMDocument $dom | |
81 * @param DOMElement $root | |
82 * @return void | |
83 */ | |
84 protected function _setDescription(DOMDocument $dom, DOMElement $root) | |
85 { | |
86 if (!$this->getDataContainer()->getDescription()) { | |
87 return; | |
88 } | |
89 $subtitle = $dom->createElement('subtitle'); | |
90 $root->appendChild($subtitle); | |
91 $subtitle->setAttribute('type', 'text'); | |
92 $text = $dom->createTextNode($this->getDataContainer()->getDescription()); | |
93 $subtitle->appendChild($text); | |
94 } | |
95 | |
96 /** | |
97 * Set date feed was last modified | |
98 * | |
99 * @param DOMDocument $dom | |
100 * @param DOMElement $root | |
101 * @return void | |
102 * @throws Writer\Exception\InvalidArgumentException | |
103 */ | |
104 protected function _setDateModified(DOMDocument $dom, DOMElement $root) | |
105 { | |
106 if (!$this->getDataContainer()->getDateModified()) { | |
107 $message = 'Atom 1.0 feed elements MUST contain exactly one' | |
108 . ' atom:updated element but a modification date has not been set'; | |
109 $exception = new Writer\Exception\InvalidArgumentException($message); | |
110 if (!$this->ignoreExceptions) { | |
111 throw $exception; | |
112 } else { | |
113 $this->exceptions[] = $exception; | |
114 return; | |
115 } | |
116 } | |
117 | |
118 $updated = $dom->createElement('updated'); | |
119 $root->appendChild($updated); | |
120 $text = $dom->createTextNode( | |
121 $this->getDataContainer()->getDateModified()->format(DateTime::ATOM) | |
122 ); | |
123 $updated->appendChild($text); | |
124 } | |
125 | |
126 /** | |
127 * Set feed generator string | |
128 * | |
129 * @param DOMDocument $dom | |
130 * @param DOMElement $root | |
131 * @return void | |
132 */ | |
133 protected function _setGenerator(DOMDocument $dom, DOMElement $root) | |
134 { | |
135 if (!$this->getDataContainer()->getGenerator()) { | |
136 $this->getDataContainer()->setGenerator('Zend_Feed_Writer', | |
137 Version::VERSION, 'http://framework.zend.com'); | |
138 } | |
139 | |
140 $gdata = $this->getDataContainer()->getGenerator(); | |
141 $generator = $dom->createElement('generator'); | |
142 $root->appendChild($generator); | |
143 $text = $dom->createTextNode($gdata['name']); | |
144 $generator->appendChild($text); | |
145 if (array_key_exists('uri', $gdata)) { | |
146 $generator->setAttribute('uri', $gdata['uri']); | |
147 } | |
148 if (array_key_exists('version', $gdata)) { | |
149 $generator->setAttribute('version', $gdata['version']); | |
150 } | |
151 } | |
152 | |
153 /** | |
154 * Set link to feed | |
155 * | |
156 * @param DOMDocument $dom | |
157 * @param DOMElement $root | |
158 * @return void | |
159 */ | |
160 protected function _setLink(DOMDocument $dom, DOMElement $root) | |
161 { | |
162 if (!$this->getDataContainer()->getLink()) { | |
163 return; | |
164 } | |
165 $link = $dom->createElement('link'); | |
166 $root->appendChild($link); | |
167 $link->setAttribute('rel', 'alternate'); | |
168 $link->setAttribute('type', 'text/html'); | |
169 $link->setAttribute('href', $this->getDataContainer()->getLink()); | |
170 } | |
171 | |
172 /** | |
173 * Set feed links | |
174 * | |
175 * @param DOMDocument $dom | |
176 * @param DOMElement $root | |
177 * @return void | |
178 * @throws Writer\Exception\InvalidArgumentException | |
179 */ | |
180 protected function _setFeedLinks(DOMDocument $dom, DOMElement $root) | |
181 { | |
182 $flinks = $this->getDataContainer()->getFeedLinks(); | |
183 if (!$flinks || !array_key_exists('atom', $flinks)) { | |
184 $message = 'Atom 1.0 feed elements SHOULD contain one atom:link ' | |
185 . 'element with a rel attribute value of "self". This is the ' | |
186 . 'preferred URI for retrieving Atom Feed Documents representing ' | |
187 . 'this Atom feed but a feed link has not been set'; | |
188 $exception = new Writer\Exception\InvalidArgumentException($message); | |
189 if (!$this->ignoreExceptions) { | |
190 throw $exception; | |
191 } else { | |
192 $this->exceptions[] = $exception; | |
193 return; | |
194 } | |
195 } | |
196 | |
197 foreach ($flinks as $type => $href) { | |
198 $mime = 'application/' . strtolower($type) . '+xml'; | |
199 $flink = $dom->createElement('link'); | |
200 $root->appendChild($flink); | |
201 $flink->setAttribute('rel', 'self'); | |
202 $flink->setAttribute('type', $mime); | |
203 $flink->setAttribute('href', $href); | |
204 } | |
205 } | |
206 | |
207 /** | |
208 * Set feed authors | |
209 * | |
210 * @param DOMDocument $dom | |
211 * @param DOMElement $root | |
212 * @return void | |
213 */ | |
214 protected function _setAuthors(DOMDocument $dom, DOMElement $root) | |
215 { | |
216 $authors = $this->container->getAuthors(); | |
217 if (!$authors || empty($authors)) { | |
218 /** | |
219 * Technically we should defer an exception until we can check | |
220 * that all entries contain an author. If any entry is missing | |
221 * an author, then a missing feed author element is invalid | |
222 */ | |
223 return; | |
224 } | |
225 foreach ($authors as $data) { | |
226 $author = $this->dom->createElement('author'); | |
227 $name = $this->dom->createElement('name'); | |
228 $author->appendChild($name); | |
229 $root->appendChild($author); | |
230 $text = $dom->createTextNode($data['name']); | |
231 $name->appendChild($text); | |
232 if (array_key_exists('email', $data)) { | |
233 $email = $this->dom->createElement('email'); | |
234 $author->appendChild($email); | |
235 $text = $dom->createTextNode($data['email']); | |
236 $email->appendChild($text); | |
237 } | |
238 if (array_key_exists('uri', $data)) { | |
239 $uri = $this->dom->createElement('uri'); | |
240 $author->appendChild($uri); | |
241 $text = $dom->createTextNode($data['uri']); | |
242 $uri->appendChild($text); | |
243 } | |
244 } | |
245 } | |
246 | |
247 /** | |
248 * Set feed identifier | |
249 * | |
250 * @param DOMDocument $dom | |
251 * @param DOMElement $root | |
252 * @return void | |
253 * @throws Writer\Exception\InvalidArgumentException | |
254 */ | |
255 protected function _setId(DOMDocument $dom, DOMElement $root) | |
256 { | |
257 if (!$this->getDataContainer()->getId() | |
258 && !$this->getDataContainer()->getLink()) { | |
259 $message = 'Atom 1.0 feed elements MUST contain exactly one ' | |
260 . 'atom:id element, or as an alternative, we can use the same ' | |
261 . 'value as atom:link however neither a suitable link nor an ' | |
262 . 'id have been set'; | |
263 $exception = new Writer\Exception\InvalidArgumentException($message); | |
264 if (!$this->ignoreExceptions) { | |
265 throw $exception; | |
266 } else { | |
267 $this->exceptions[] = $exception; | |
268 return; | |
269 } | |
270 } | |
271 | |
272 if (!$this->getDataContainer()->getId()) { | |
273 $this->getDataContainer()->setId( | |
274 $this->getDataContainer()->getLink()); | |
275 } | |
276 $id = $dom->createElement('id'); | |
277 $root->appendChild($id); | |
278 $text = $dom->createTextNode($this->getDataContainer()->getId()); | |
279 $id->appendChild($text); | |
280 } | |
281 | |
282 /** | |
283 * Set feed copyright | |
284 * | |
285 * @param DOMDocument $dom | |
286 * @param DOMElement $root | |
287 * @return void | |
288 */ | |
289 protected function _setCopyright(DOMDocument $dom, DOMElement $root) | |
290 { | |
291 $copyright = $this->getDataContainer()->getCopyright(); | |
292 if (!$copyright) { | |
293 return; | |
294 } | |
295 $copy = $dom->createElement('rights'); | |
296 $root->appendChild($copy); | |
297 $text = $dom->createTextNode($copyright); | |
298 $copy->appendChild($text); | |
299 } | |
300 | |
301 /** | |
302 * Set feed level logo (image) | |
303 * | |
304 * @param DOMDocument $dom | |
305 * @param DOMElement $root | |
306 * @return void | |
307 */ | |
308 protected function _setImage(DOMDocument $dom, DOMElement $root) | |
309 { | |
310 $image = $this->getDataContainer()->getImage(); | |
311 if (!$image) { | |
312 return; | |
313 } | |
314 $img = $dom->createElement('logo'); | |
315 $root->appendChild($img); | |
316 $text = $dom->createTextNode($image['uri']); | |
317 $img->appendChild($text); | |
318 } | |
319 | |
320 /** | |
321 * Set date feed was created | |
322 * | |
323 * @param DOMDocument $dom | |
324 * @param DOMElement $root | |
325 * @return void | |
326 */ | |
327 protected function _setDateCreated(DOMDocument $dom, DOMElement $root) | |
328 { | |
329 if (!$this->getDataContainer()->getDateCreated()) { | |
330 return; | |
331 } | |
332 if (!$this->getDataContainer()->getDateModified()) { | |
333 $this->getDataContainer()->setDateModified( | |
334 $this->getDataContainer()->getDateCreated() | |
335 ); | |
336 } | |
337 } | |
338 | |
339 /** | |
340 * Set base URL to feed links | |
341 * | |
342 * @param DOMDocument $dom | |
343 * @param DOMElement $root | |
344 * @return void | |
345 */ | |
346 protected function _setBaseUrl(DOMDocument $dom, DOMElement $root) | |
347 { | |
348 $baseUrl = $this->getDataContainer()->getBaseUrl(); | |
349 if (!$baseUrl) { | |
350 return; | |
351 } | |
352 $root->setAttribute('xml:base', $baseUrl); | |
353 } | |
354 | |
355 /** | |
356 * Set hubs to which this feed pushes | |
357 * | |
358 * @param DOMDocument $dom | |
359 * @param DOMElement $root | |
360 * @return void | |
361 */ | |
362 protected function _setHubs(DOMDocument $dom, DOMElement $root) | |
363 { | |
364 $hubs = $this->getDataContainer()->getHubs(); | |
365 if (!$hubs) { | |
366 return; | |
367 } | |
368 foreach ($hubs as $hubUrl) { | |
369 $hub = $dom->createElement('link'); | |
370 $hub->setAttribute('rel', 'hub'); | |
371 $hub->setAttribute('href', $hubUrl); | |
372 $root->appendChild($hub); | |
373 } | |
374 } | |
375 | |
376 /** | |
377 * Set feed categories | |
378 * | |
379 * @param DOMDocument $dom | |
380 * @param DOMElement $root | |
381 * @return void | |
382 */ | |
383 protected function _setCategories(DOMDocument $dom, DOMElement $root) | |
384 { | |
385 $categories = $this->getDataContainer()->getCategories(); | |
386 if (!$categories) { | |
387 return; | |
388 } | |
389 foreach ($categories as $cat) { | |
390 $category = $dom->createElement('category'); | |
391 $category->setAttribute('term', $cat['term']); | |
392 if (isset($cat['label'])) { | |
393 $category->setAttribute('label', $cat['label']); | |
394 } else { | |
395 $category->setAttribute('label', $cat['term']); | |
396 } | |
397 if (isset($cat['scheme'])) { | |
398 $category->setAttribute('scheme', $cat['scheme']); | |
399 } | |
400 $root->appendChild($category); | |
401 } | |
402 } | |
403 } |