3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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 protected function _setLanguage(DOMDocument
$dom, DOMElement
$root)
42 if ($this->getDataContainer()->getLanguage()) {
43 $root->setAttribute('xml:lang', $this->getDataContainer()
51 * @param DOMDocument $dom
52 * @param DOMElement $root
54 * @throws Writer\Exception\InvalidArgumentException
56 protected function _setTitle(DOMDocument
$dom, DOMElement
$root)
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
) {
65 $this->exceptions
[] = $exception;
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);
78 * Set feed description
80 * @param DOMDocument $dom
81 * @param DOMElement $root
84 protected function _setDescription(DOMDocument
$dom, DOMElement
$root)
86 if (!$this->getDataContainer()->getDescription()) {
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);
97 * Set date feed was last modified
99 * @param DOMDocument $dom
100 * @param DOMElement $root
102 * @throws Writer\Exception\InvalidArgumentException
104 protected function _setDateModified(DOMDocument
$dom, DOMElement
$root)
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
) {
113 $this->exceptions
[] = $exception;
118 $updated = $dom->createElement('updated');
119 $root->appendChild($updated);
120 $text = $dom->createTextNode(
121 $this->getDataContainer()->getDateModified()->format(DateTime
::ISO8601
)
123 $updated->appendChild($text);
127 * Set feed generator string
129 * @param DOMDocument $dom
130 * @param DOMElement $root
133 protected function _setGenerator(DOMDocument
$dom, DOMElement
$root)
135 if (!$this->getDataContainer()->getGenerator()) {
136 $this->getDataContainer()->setGenerator('Zend_Feed_Writer',
137 Version
::VERSION
, 'http://framework.zend.com');
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']);
148 if (array_key_exists('version', $gdata)) {
149 $generator->setAttribute('version', $gdata['version']);
156 * @param DOMDocument $dom
157 * @param DOMElement $root
160 protected function _setLink(DOMDocument
$dom, DOMElement
$root)
162 if (!$this->getDataContainer()->getLink()) {
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());
175 * @param DOMDocument $dom
176 * @param DOMElement $root
178 * @throws Writer\Exception\InvalidArgumentException
180 protected function _setFeedLinks(DOMDocument
$dom, DOMElement
$root)
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
) {
192 $this->exceptions
[] = $exception;
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);
210 * @param DOMDocument $dom
211 * @param DOMElement $root
214 protected function _setAuthors(DOMDocument
$dom, DOMElement
$root)
216 $authors = $this->container
->getAuthors();
217 if (!$authors ||
empty($authors)) {
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
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);
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);
248 * Set feed identifier
250 * @param DOMDocument $dom
251 * @param DOMElement $root
253 * @throws Writer\Exception\InvalidArgumentException
255 protected function _setId(DOMDocument
$dom, DOMElement
$root)
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
) {
267 $this->exceptions
[] = $exception;
272 if (!$this->getDataContainer()->getId()) {
273 $this->getDataContainer()->setId(
274 $this->getDataContainer()->getLink());
276 $id = $dom->createElement('id');
277 $root->appendChild($id);
278 $text = $dom->createTextNode($this->getDataContainer()->getId());
279 $id->appendChild($text);
285 * @param DOMDocument $dom
286 * @param DOMElement $root
289 protected function _setCopyright(DOMDocument
$dom, DOMElement
$root)
291 $copyright = $this->getDataContainer()->getCopyright();
295 $copy = $dom->createElement('rights');
296 $root->appendChild($copy);
297 $text = $dom->createTextNode($copyright);
298 $copy->appendChild($text);
302 * Set feed level logo (image)
304 * @param DOMDocument $dom
305 * @param DOMElement $root
308 protected function _setImage(DOMDocument
$dom, DOMElement
$root)
310 $image = $this->getDataContainer()->getImage();
314 $img = $dom->createElement('logo');
315 $root->appendChild($img);
316 $text = $dom->createTextNode($image['uri']);
317 $img->appendChild($text);
321 * Set date feed was created
323 * @param DOMDocument $dom
324 * @param DOMElement $root
327 protected function _setDateCreated(DOMDocument
$dom, DOMElement
$root)
329 if (!$this->getDataContainer()->getDateCreated()) {
332 if (!$this->getDataContainer()->getDateModified()) {
333 $this->getDataContainer()->setDateModified(
334 $this->getDataContainer()->getDateCreated()
340 * Set base URL to feed links
342 * @param DOMDocument $dom
343 * @param DOMElement $root
346 protected function _setBaseUrl(DOMDocument
$dom, DOMElement
$root)
348 $baseUrl = $this->getDataContainer()->getBaseUrl();
352 $root->setAttribute('xml:base', $baseUrl);
356 * Set hubs to which this feed pushes
358 * @param DOMDocument $dom
359 * @param DOMElement $root
362 protected function _setHubs(DOMDocument
$dom, DOMElement
$root)
364 $hubs = $this->getDataContainer()->getHubs();
368 foreach ($hubs as $hubUrl) {
369 $hub = $dom->createElement('link');
370 $hub->setAttribute('rel', 'hub');
371 $hub->setAttribute('href', $hubUrl);
372 $root->appendChild($hub);
377 * Set feed categories
379 * @param DOMDocument $dom
380 * @param DOMElement $root
383 protected function _setCategories(DOMDocument
$dom, DOMElement
$root)
385 $categories = $this->getDataContainer()->getCategories();
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']);
395 $category->setAttribute('label', $cat['term']);
397 if (isset($cat['scheme'])) {
398 $category->setAttribute('scheme', $cat['scheme']);
400 $root->appendChild($category);