Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Feed / Writer / Renderer / Feed / AbstractAtom.php
blob2ff11129ac49849dafd8d4691aab8f09906b7044
1 <?php
2 /**
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
8 */
10 namespace Zend\Feed\Writer\Renderer\Feed;
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;
19 /**
21 class AbstractAtom extends Renderer\AbstractRenderer
23 /**
24 * Constructor
26 * @param Writer\AbstractFeed $container
28 public function __construct($container)
30 parent::__construct($container);
33 /**
34 * Set feed language
36 * @param DOMDocument $dom
37 * @param DOMElement $root
38 * @return void
40 protected function _setLanguage(DOMDocument $dom, DOMElement $root)
42 if ($this->getDataContainer()->getLanguage()) {
43 $root->setAttribute('xml:lang', $this->getDataContainer()
44 ->getLanguage());
48 /**
49 * Set feed title
51 * @param DOMDocument $dom
52 * @param DOMElement $root
53 * @return void
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) {
63 throw $exception;
64 } else {
65 $this->exceptions[] = $exception;
66 return;
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);
77 /**
78 * Set feed description
80 * @param DOMDocument $dom
81 * @param DOMElement $root
82 * @return void
84 protected function _setDescription(DOMDocument $dom, DOMElement $root)
86 if (!$this->getDataContainer()->getDescription()) {
87 return;
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);
96 /**
97 * Set date feed was last modified
99 * @param DOMDocument $dom
100 * @param DOMElement $root
101 * @return void
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) {
111 throw $exception;
112 } else {
113 $this->exceptions[] = $exception;
114 return;
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
131 * @return void
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']);
154 * Set link to feed
156 * @param DOMDocument $dom
157 * @param DOMElement $root
158 * @return void
160 protected function _setLink(DOMDocument $dom, DOMElement $root)
162 if (!$this->getDataContainer()->getLink()) {
163 return;
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());
173 * Set feed links
175 * @param DOMDocument $dom
176 * @param DOMElement $root
177 * @return void
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) {
190 throw $exception;
191 } else {
192 $this->exceptions[] = $exception;
193 return;
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);
208 * Set feed authors
210 * @param DOMDocument $dom
211 * @param DOMElement $root
212 * @return void
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
223 return;
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
252 * @return void
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) {
265 throw $exception;
266 } else {
267 $this->exceptions[] = $exception;
268 return;
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);
283 * Set feed copyright
285 * @param DOMDocument $dom
286 * @param DOMElement $root
287 * @return void
289 protected function _setCopyright(DOMDocument $dom, DOMElement $root)
291 $copyright = $this->getDataContainer()->getCopyright();
292 if (!$copyright) {
293 return;
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
306 * @return void
308 protected function _setImage(DOMDocument $dom, DOMElement $root)
310 $image = $this->getDataContainer()->getImage();
311 if (!$image) {
312 return;
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
325 * @return void
327 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
329 if (!$this->getDataContainer()->getDateCreated()) {
330 return;
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
344 * @return void
346 protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
348 $baseUrl = $this->getDataContainer()->getBaseUrl();
349 if (!$baseUrl) {
350 return;
352 $root->setAttribute('xml:base', $baseUrl);
356 * Set hubs to which this feed pushes
358 * @param DOMDocument $dom
359 * @param DOMElement $root
360 * @return void
362 protected function _setHubs(DOMDocument $dom, DOMElement $root)
364 $hubs = $this->getDataContainer()->getHubs();
365 if (!$hubs) {
366 return;
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
381 * @return void
383 protected function _setCategories(DOMDocument $dom, DOMElement $root)
385 $categories = $this->getDataContainer()->getCategories();
386 if (!$categories) {
387 return;
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']);
397 if (isset($cat['scheme'])) {
398 $category->setAttribute('scheme', $cat['scheme']);
400 $root->appendChild($category);