3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Feed\Writer\Renderer\Feed;
16 use Zend\Feed\Writer\Renderer;
17 use Zend\Feed\Writer\Version;
21 class AbstractAtom extends Renderer\AbstractRenderer
26 * @param Writer\AbstractFeed $container
28 public function __construct($container)
30 parent::__construct($container);
36 * @param DOMDocument $dom
37 * @param DOMElement $root
40 // @codingStandardsIgnoreStart
41 protected function _setLanguage(DOMDocument $dom, DOMElement $root)
43 // @codingStandardsIgnoreEnd
44 if ($this->getDataContainer()->getLanguage()) {
45 $root->setAttribute('xml:lang', $this->getDataContainer()
53 * @param DOMDocument $dom
54 * @param DOMElement $root
56 * @throws Writer\Exception\InvalidArgumentException
58 // @codingStandardsIgnoreStart
59 protected function _setTitle(DOMDocument $dom, DOMElement $root)
61 // @codingStandardsIgnoreEnd
62 if (! $this->getDataContainer()->getTitle()) {
63 $message = 'Atom 1.0 feed elements MUST contain exactly one'
64 . ' atom:title element but a title has not been set';
65 $exception = new Writer\Exception\InvalidArgumentException($message);
66 if (! $this->ignoreExceptions) {
69 $this->exceptions[] = $exception;
74 $title = $dom->createElement('title');
75 $root->appendChild($title);
76 $title->setAttribute('type', 'text');
77 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
78 $title->appendChild($text);
82 * Set feed description
84 * @param DOMDocument $dom
85 * @param DOMElement $root
88 // @codingStandardsIgnoreStart
89 protected function _setDescription(DOMDocument $dom, DOMElement $root)
91 // @codingStandardsIgnoreEnd
92 if (! $this->getDataContainer()->getDescription()) {
95 $subtitle = $dom->createElement('subtitle');
96 $root->appendChild($subtitle);
97 $subtitle->setAttribute('type', 'text');
98 $text = $dom->createTextNode($this->getDataContainer()->getDescription());
99 $subtitle->appendChild($text);
103 * Set date feed was last modified
105 * @param DOMDocument $dom
106 * @param DOMElement $root
108 * @throws Writer\Exception\InvalidArgumentException
110 // @codingStandardsIgnoreStart
111 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
113 // @codingStandardsIgnoreEnd
114 if (! $this->getDataContainer()->getDateModified()) {
115 $message = 'Atom 1.0 feed elements MUST contain exactly one'
116 . ' atom:updated element but a modification date has not been set';
117 $exception = new Writer\Exception\InvalidArgumentException($message);
118 if (! $this->ignoreExceptions) {
121 $this->exceptions[] = $exception;
126 $updated = $dom->createElement('updated');
127 $root->appendChild($updated);
128 $text = $dom->createTextNode(
129 $this->getDataContainer()->getDateModified()->format(DateTime::ATOM)
131 $updated->appendChild($text);
135 * Set feed generator string
137 * @param DOMDocument $dom
138 * @param DOMElement $root
141 // @codingStandardsIgnoreStart
142 protected function _setGenerator(DOMDocument $dom, DOMElement $root)
144 // @codingStandardsIgnoreEnd
145 if (! $this->getDataContainer()->getGenerator()) {
146 $this->getDataContainer()->setGenerator(
149 'http://framework.zend.com'
153 $gdata = $this->getDataContainer()->getGenerator();
154 $generator = $dom->createElement('generator');
155 $root->appendChild($generator);
156 $text = $dom->createTextNode($gdata['name']);
157 $generator->appendChild($text);
158 if (array_key_exists('uri', $gdata)) {
159 $generator->setAttribute('uri', $gdata['uri']);
161 if (array_key_exists('version', $gdata)) {
162 $generator->setAttribute('version', $gdata['version']);
169 * @param DOMDocument $dom
170 * @param DOMElement $root
173 // @codingStandardsIgnoreStart
174 protected function _setLink(DOMDocument $dom, DOMElement $root)
176 // @codingStandardsIgnoreEnd
177 if (! $this->getDataContainer()->getLink()) {
180 $link = $dom->createElement('link');
181 $root->appendChild($link);
182 $link->setAttribute('rel', 'alternate');
183 $link->setAttribute('type', 'text/html');
184 $link->setAttribute('href', $this->getDataContainer()->getLink());
190 * @param DOMDocument $dom
191 * @param DOMElement $root
193 * @throws Writer\Exception\InvalidArgumentException
195 // @codingStandardsIgnoreStart
196 protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
198 // @codingStandardsIgnoreEnd
199 $flinks = $this->getDataContainer()->getFeedLinks();
200 if (! $flinks || ! array_key_exists('atom', $flinks)) {
201 $message = 'Atom 1.0 feed elements SHOULD contain one atom:link '
202 . 'element with a rel attribute value of "self". This is the '
203 . 'preferred URI for retrieving Atom Feed Documents representing '
204 . 'this Atom feed but a feed link has not been set';
205 $exception = new Writer\Exception\InvalidArgumentException($message);
206 if (! $this->ignoreExceptions) {
209 $this->exceptions[] = $exception;
214 foreach ($flinks as $type => $href) {
215 $mime = 'application/' . strtolower($type) . '+xml';
216 $flink = $dom->createElement('link');
217 $root->appendChild($flink);
218 $flink->setAttribute('rel', 'self');
219 $flink->setAttribute('type', $mime);
220 $flink->setAttribute('href', $href);
227 * @param DOMDocument $dom
228 * @param DOMElement $root
231 // @codingStandardsIgnoreStart
232 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
234 // @codingStandardsIgnoreEnd
235 $authors = $this->container->getAuthors();
236 if (! $authors || empty($authors)) {
238 * Technically we should defer an exception until we can check
239 * that all entries contain an author. If any entry is missing
240 * an author, then a missing feed author element is invalid
244 foreach ($authors as $data) {
245 $author = $this->dom->createElement('author');
246 $name = $this->dom->createElement('name');
247 $author->appendChild($name);
248 $root->appendChild($author);
249 $text = $dom->createTextNode($data['name']);
250 $name->appendChild($text);
251 if (array_key_exists('email', $data)) {
252 $email = $this->dom->createElement('email');
253 $author->appendChild($email);
254 $text = $dom->createTextNode($data['email']);
255 $email->appendChild($text);
257 if (array_key_exists('uri', $data)) {
258 $uri = $this->dom->createElement('uri');
259 $author->appendChild($uri);
260 $text = $dom->createTextNode($data['uri']);
261 $uri->appendChild($text);
267 * Set feed identifier
269 * @param DOMDocument $dom
270 * @param DOMElement $root
272 * @throws Writer\Exception\InvalidArgumentException
274 // @codingStandardsIgnoreStart
275 protected function _setId(DOMDocument $dom, DOMElement $root)
277 // @codingStandardsIgnoreEnd
278 if (! $this->getDataContainer()->getId()
279 && ! $this->getDataContainer()->getLink()) {
280 $message = 'Atom 1.0 feed elements MUST contain exactly one '
281 . 'atom:id element, or as an alternative, we can use the same '
282 . 'value as atom:link however neither a suitable link nor an '
283 . 'id have been set';
284 $exception = new Writer\Exception\InvalidArgumentException($message);
285 if (! $this->ignoreExceptions) {
288 $this->exceptions[] = $exception;
293 if (! $this->getDataContainer()->getId()) {
294 $this->getDataContainer()->setId(
295 $this->getDataContainer()->getLink()
298 $id = $dom->createElement('id');
299 $root->appendChild($id);
300 $text = $dom->createTextNode($this->getDataContainer()->getId());
301 $id->appendChild($text);
307 * @param DOMDocument $dom
308 * @param DOMElement $root
311 // @codingStandardsIgnoreStart
312 protected function _setCopyright(DOMDocument $dom, DOMElement $root)
314 // @codingStandardsIgnoreEnd
315 $copyright = $this->getDataContainer()->getCopyright();
319 $copy = $dom->createElement('rights');
320 $root->appendChild($copy);
321 $text = $dom->createTextNode($copyright);
322 $copy->appendChild($text);
326 * Set feed level logo (image)
328 * @param DOMDocument $dom
329 * @param DOMElement $root
332 // @codingStandardsIgnoreStart
333 protected function _setImage(DOMDocument $dom, DOMElement $root)
335 // @codingStandardsIgnoreEnd
336 $image = $this->getDataContainer()->getImage();
340 $img = $dom->createElement('logo');
341 $root->appendChild($img);
342 $text = $dom->createTextNode($image['uri']);
343 $img->appendChild($text);
347 * Set date feed was created
349 * @param DOMDocument $dom
350 * @param DOMElement $root
353 // @codingStandardsIgnoreStart
354 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
356 // @codingStandardsIgnoreEnd
357 if (! $this->getDataContainer()->getDateCreated()) {
360 if (! $this->getDataContainer()->getDateModified()) {
361 $this->getDataContainer()->setDateModified(
362 $this->getDataContainer()->getDateCreated()
368 * Set base URL to feed links
370 * @param DOMDocument $dom
371 * @param DOMElement $root
374 // @codingStandardsIgnoreStart
375 protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
377 // @codingStandardsIgnoreEnd
378 $baseUrl = $this->getDataContainer()->getBaseUrl();
382 $root->setAttribute('xml:base', $baseUrl);
386 * Set hubs to which this feed pushes
388 * @param DOMDocument $dom
389 * @param DOMElement $root
392 // @codingStandardsIgnoreStart
393 protected function _setHubs(DOMDocument $dom, DOMElement $root)
395 // @codingStandardsIgnoreEnd
396 $hubs = $this->getDataContainer()->getHubs();
400 foreach ($hubs as $hubUrl) {
401 $hub = $dom->createElement('link');
402 $hub->setAttribute('rel', 'hub');
403 $hub->setAttribute('href', $hubUrl);
404 $root->appendChild($hub);
409 * Set feed categories
411 * @param DOMDocument $dom
412 * @param DOMElement $root
415 // @codingStandardsIgnoreStart
416 protected function _setCategories(DOMDocument $dom, DOMElement $root)
418 // @codingStandardsIgnoreEnd
419 $categories = $this->getDataContainer()->getCategories();
423 foreach ($categories as $cat) {
424 $category = $dom->createElement('category');
425 $category->setAttribute('term', $cat['term']);
426 if (isset($cat['label'])) {
427 $category->setAttribute('label', $cat['label']);
429 $category->setAttribute('label', $cat['term']);
431 if (isset($cat['scheme'])) {
432 $category->setAttribute('scheme', $cat['scheme']);
434 $root->appendChild($category);