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 / Entry / Rss.php
blobf00960d55d1555ae6d02197a38c605bf44fa9ccd
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\Entry;
12 use DateTime;
13 use DOMDocument;
14 use DOMElement;
15 use Zend\Feed\Uri;
16 use Zend\Feed\Writer;
17 use Zend\Feed\Writer\Renderer;
19 /**
21 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
23 /**
24 * Constructor
26 * @param Writer\Entry $container
28 public function __construct(Writer\Entry $container)
30 parent::__construct($container);
33 /**
34 * Render RSS entry
36 * @return Rss
38 public function render()
40 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
41 $this->dom->formatOutput = true;
42 $this->dom->substituteEntities = false;
43 $entry = $this->dom->createElement('item');
44 $this->dom->appendChild($entry);
46 $this->_setTitle($this->dom, $entry);
47 $this->_setDescription($this->dom, $entry);
48 $this->_setDateCreated($this->dom, $entry);
49 $this->_setDateModified($this->dom, $entry);
50 $this->_setLink($this->dom, $entry);
51 $this->_setId($this->dom, $entry);
52 $this->_setAuthors($this->dom, $entry);
53 $this->_setEnclosure($this->dom, $entry);
54 $this->_setCommentLink($this->dom, $entry);
55 $this->_setCategories($this->dom, $entry);
56 foreach ($this->extensions as $ext) {
57 $ext->setType($this->getType());
58 $ext->setRootElement($this->getRootElement());
59 $ext->setDOMDocument($this->getDOMDocument(), $entry);
60 $ext->render();
63 return $this;
66 /**
67 * Set entry title
69 * @param DOMDocument $dom
70 * @param DOMElement $root
71 * @return void
72 * @throws Writer\Exception\InvalidArgumentException
74 protected function _setTitle(DOMDocument $dom, DOMElement $root)
76 if (!$this->getDataContainer()->getDescription()
77 && !$this->getDataContainer()->getTitle()) {
78 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
79 . ' title element but a title has not been set. In addition, there'
80 . ' is no description as required in the absence of a title.';
81 $exception = new Writer\Exception\InvalidArgumentException($message);
82 if (!$this->ignoreExceptions) {
83 throw $exception;
84 } else {
85 $this->exceptions[] = $exception;
86 return;
89 $title = $dom->createElement('title');
90 $root->appendChild($title);
91 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
92 $title->appendChild($text);
95 /**
96 * Set entry description
98 * @param DOMDocument $dom
99 * @param DOMElement $root
100 * @return void
101 * @throws Writer\Exception\InvalidArgumentException
103 protected function _setDescription(DOMDocument $dom, DOMElement $root)
105 if (!$this->getDataContainer()->getDescription()
106 && !$this->getDataContainer()->getTitle()) {
107 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
108 . ' description element but a description has not been set. In'
109 . ' addition, there is no title element as required in the absence'
110 . ' of a description.';
111 $exception = new Writer\Exception\InvalidArgumentException($message);
112 if (!$this->ignoreExceptions) {
113 throw $exception;
114 } else {
115 $this->exceptions[] = $exception;
116 return;
119 if (!$this->getDataContainer()->getDescription()) {
120 return;
122 $subtitle = $dom->createElement('description');
123 $root->appendChild($subtitle);
124 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
125 $subtitle->appendChild($text);
129 * Set date entry was last modified
131 * @param DOMDocument $dom
132 * @param DOMElement $root
133 * @return void
135 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
137 if (!$this->getDataContainer()->getDateModified()) {
138 return;
141 $updated = $dom->createElement('pubDate');
142 $root->appendChild($updated);
143 $text = $dom->createTextNode(
144 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
146 $updated->appendChild($text);
150 * Set date entry was created
152 * @param DOMDocument $dom
153 * @param DOMElement $root
154 * @return void
156 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
158 if (!$this->getDataContainer()->getDateCreated()) {
159 return;
161 if (!$this->getDataContainer()->getDateModified()) {
162 $this->getDataContainer()->setDateModified(
163 $this->getDataContainer()->getDateCreated()
169 * Set entry authors
171 * @param DOMDocument $dom
172 * @param DOMElement $root
173 * @return void
175 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
177 $authors = $this->container->getAuthors();
178 if ((!$authors || empty($authors))) {
179 return;
181 foreach ($authors as $data) {
182 $author = $this->dom->createElement('author');
183 $name = $data['name'];
184 if (array_key_exists('email', $data)) {
185 $name = $data['email'] . ' (' . $data['name'] . ')';
187 $text = $dom->createTextNode($name);
188 $author->appendChild($text);
189 $root->appendChild($author);
194 * Set entry enclosure
196 * @param DOMDocument $dom
197 * @param DOMElement $root
198 * @return void
199 * @throws Writer\Exception\InvalidArgumentException
201 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
203 $data = $this->container->getEnclosure();
204 if ((!$data || empty($data))) {
205 return;
207 if (!isset($data['type'])) {
208 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
209 if (!$this->ignoreExceptions) {
210 throw $exception;
211 } else {
212 $this->exceptions[] = $exception;
213 return;
216 if (!isset($data['length'])) {
217 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
218 if (!$this->ignoreExceptions) {
219 throw $exception;
220 } else {
221 $this->exceptions[] = $exception;
222 return;
225 if (isset($data['length']) && (int) $data['length'] <= 0) {
226 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
227 . ' indicating the content\'s length in bytes');
228 if (!$this->ignoreExceptions) {
229 throw $exception;
230 } else {
231 $this->exceptions[] = $exception;
232 return;
235 $enclosure = $this->dom->createElement('enclosure');
236 $enclosure->setAttribute('type', $data['type']);
237 $enclosure->setAttribute('length', $data['length']);
238 $enclosure->setAttribute('url', $data['uri']);
239 $root->appendChild($enclosure);
243 * Set link to entry
245 * @param DOMDocument $dom
246 * @param DOMElement $root
247 * @return void
249 protected function _setLink(DOMDocument $dom, DOMElement $root)
251 if (!$this->getDataContainer()->getLink()) {
252 return;
254 $link = $dom->createElement('link');
255 $root->appendChild($link);
256 $text = $dom->createTextNode($this->getDataContainer()->getLink());
257 $link->appendChild($text);
261 * Set entry identifier
263 * @param DOMDocument $dom
264 * @param DOMElement $root
265 * @return void
267 protected function _setId(DOMDocument $dom, DOMElement $root)
269 if (!$this->getDataContainer()->getId()
270 && !$this->getDataContainer()->getLink()) {
271 return;
274 $id = $dom->createElement('guid');
275 $root->appendChild($id);
276 if (!$this->getDataContainer()->getId()) {
277 $this->getDataContainer()->setId(
278 $this->getDataContainer()->getLink());
280 $text = $dom->createTextNode($this->getDataContainer()->getId());
281 $id->appendChild($text);
282 if (!Uri::factory($this->getDataContainer()->getId())->isValid()) {
283 $id->setAttribute('isPermaLink', 'false');
288 * Set link to entry comments
290 * @param DOMDocument $dom
291 * @param DOMElement $root
292 * @return void
294 protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
296 $link = $this->getDataContainer()->getCommentLink();
297 if (!$link) {
298 return;
300 $clink = $this->dom->createElement('comments');
301 $text = $dom->createTextNode($link);
302 $clink->appendChild($text);
303 $root->appendChild($clink);
307 * Set entry categories
309 * @param DOMDocument $dom
310 * @param DOMElement $root
311 * @return void
313 protected function _setCategories(DOMDocument $dom, DOMElement $root)
315 $categories = $this->getDataContainer()->getCategories();
316 if (!$categories) {
317 return;
319 foreach ($categories as $cat) {
320 $category = $dom->createElement('category');
321 if (isset($cat['scheme'])) {
322 $category->setAttribute('domain', $cat['scheme']);
324 $text = $dom->createCDATASection($cat['term']);
325 $category->appendChild($text);
326 $root->appendChild($category);