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 / Atom.php
blobde4c631a40c29b2cba1c6770a1a5ef6b7c378fad
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;
18 use Zend\Validator;
20 class Atom extends Renderer\AbstractRenderer implements Renderer\RendererInterface
22 /**
23 * Constructor
25 * @param Writer\Entry $container
27 public function __construct(Writer\Entry $container)
29 parent::__construct($container);
32 /**
33 * Render atom entry
35 * @return Atom
37 public function render()
39 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
40 $this->dom->formatOutput = true;
41 $entry = $this->dom->createElementNS(Writer\Writer::NAMESPACE_ATOM_10, 'entry');
42 $this->dom->appendChild($entry);
44 $this->_setSource($this->dom, $entry);
45 $this->_setTitle($this->dom, $entry);
46 $this->_setDescription($this->dom, $entry);
47 $this->_setDateCreated($this->dom, $entry);
48 $this->_setDateModified($this->dom, $entry);
49 $this->_setLink($this->dom, $entry);
50 $this->_setId($this->dom, $entry);
51 $this->_setAuthors($this->dom, $entry);
52 $this->_setEnclosure($this->dom, $entry);
53 $this->_setContent($this->dom, $entry);
54 $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()->getTitle()) {
77 $message = 'Atom 1.0 entry elements MUST contain exactly one'
78 . ' atom:title element but a title has not been set';
79 $exception = new Writer\Exception\InvalidArgumentException($message);
80 if (!$this->ignoreExceptions) {
81 throw $exception;
82 } else {
83 $this->exceptions[] = $exception;
84 return;
87 $title = $dom->createElement('title');
88 $root->appendChild($title);
89 $title->setAttribute('type', 'html');
90 $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
91 $title->appendChild($cdata);
94 /**
95 * Set entry description
97 * @param DOMDocument $dom
98 * @param DOMElement $root
99 * @return void
101 protected function _setDescription(DOMDocument $dom, DOMElement $root)
103 if (!$this->getDataContainer()->getDescription()) {
104 return; // unless src content or base64
106 $subtitle = $dom->createElement('summary');
107 $root->appendChild($subtitle);
108 $subtitle->setAttribute('type', 'html');
109 $cdata = $dom->createCDATASection(
110 $this->getDataContainer()->getDescription()
112 $subtitle->appendChild($cdata);
116 * Set date entry was modified
118 * @param DOMDocument $dom
119 * @param DOMElement $root
120 * @return void
121 * @throws Writer\Exception\InvalidArgumentException
123 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
125 if (!$this->getDataContainer()->getDateModified()) {
126 $message = 'Atom 1.0 entry elements MUST contain exactly one'
127 . ' atom:updated element but a modification date has not been set';
128 $exception = new Writer\Exception\InvalidArgumentException($message);
129 if (!$this->ignoreExceptions) {
130 throw $exception;
131 } else {
132 $this->exceptions[] = $exception;
133 return;
137 $updated = $dom->createElement('updated');
138 $root->appendChild($updated);
139 $text = $dom->createTextNode(
140 $this->getDataContainer()->getDateModified()->format(DateTime::ISO8601)
142 $updated->appendChild($text);
146 * Set date entry was created
148 * @param DOMDocument $dom
149 * @param DOMElement $root
150 * @return void
152 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
154 if (!$this->getDataContainer()->getDateCreated()) {
155 return;
157 $el = $dom->createElement('published');
158 $root->appendChild($el);
159 $text = $dom->createTextNode(
160 $this->getDataContainer()->getDateCreated()->format(DateTime::ISO8601)
162 $el->appendChild($text);
166 * Set entry authors
168 * @param DOMDocument $dom
169 * @param DOMElement $root
170 * @return void
172 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
174 $authors = $this->container->getAuthors();
175 if ((!$authors || empty($authors))) {
177 * This will actually trigger an Exception at the feed level if
178 * a feed level author is not set.
180 return;
182 foreach ($authors as $data) {
183 $author = $this->dom->createElement('author');
184 $name = $this->dom->createElement('name');
185 $author->appendChild($name);
186 $root->appendChild($author);
187 $text = $dom->createTextNode($data['name']);
188 $name->appendChild($text);
189 if (array_key_exists('email', $data)) {
190 $email = $this->dom->createElement('email');
191 $author->appendChild($email);
192 $text = $dom->createTextNode($data['email']);
193 $email->appendChild($text);
195 if (array_key_exists('uri', $data)) {
196 $uri = $this->dom->createElement('uri');
197 $author->appendChild($uri);
198 $text = $dom->createTextNode($data['uri']);
199 $uri->appendChild($text);
205 * Set entry enclosure
207 * @param DOMDocument $dom
208 * @param DOMElement $root
209 * @return void
211 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
213 $data = $this->container->getEnclosure();
214 if ((!$data || empty($data))) {
215 return;
217 $enclosure = $this->dom->createElement('link');
218 $enclosure->setAttribute('rel', 'enclosure');
219 if (isset($data['type'])) {
220 $enclosure->setAttribute('type', $data['type']);
222 if (isset($data['length'])) {
223 $enclosure->setAttribute('length', $data['length']);
225 $enclosure->setAttribute('href', $data['uri']);
226 $root->appendChild($enclosure);
229 protected function _setLink(DOMDocument $dom, DOMElement $root)
231 if (!$this->getDataContainer()->getLink()) {
232 return;
234 $link = $dom->createElement('link');
235 $root->appendChild($link);
236 $link->setAttribute('rel', 'alternate');
237 $link->setAttribute('type', 'text/html');
238 $link->setAttribute('href', $this->getDataContainer()->getLink());
242 * Set entry identifier
244 * @param DOMDocument $dom
245 * @param DOMElement $root
246 * @return void
247 * @throws Writer\Exception\InvalidArgumentException
249 protected function _setId(DOMDocument $dom, DOMElement $root)
251 if (!$this->getDataContainer()->getId()
252 && !$this->getDataContainer()->getLink()) {
253 $message = 'Atom 1.0 entry elements MUST contain exactly one '
254 . 'atom:id element, or as an alternative, we can use the same '
255 . 'value as atom:link however neither a suitable link nor an '
256 . 'id have been set';
257 $exception = new Writer\Exception\InvalidArgumentException($message);
258 if (!$this->ignoreExceptions) {
259 throw $exception;
260 } else {
261 $this->exceptions[] = $exception;
262 return;
266 if (!$this->getDataContainer()->getId()) {
267 $this->getDataContainer()->setId(
268 $this->getDataContainer()->getLink());
270 if (!Uri::factory($this->getDataContainer()->getId())->isValid()
271 && !preg_match(
272 "#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#",
273 $this->getDataContainer()->getId())
274 && !$this->_validateTagUri($this->getDataContainer()->getId())
276 throw new Writer\Exception\InvalidArgumentException('Atom 1.0 IDs must be a valid URI/IRI');
278 $id = $dom->createElement('id');
279 $root->appendChild($id);
280 $text = $dom->createTextNode($this->getDataContainer()->getId());
281 $id->appendChild($text);
285 * Validate a URI using the tag scheme (RFC 4151)
287 * @param string $id
288 * @return bool
290 protected function _validateTagUri($id)
292 if (preg_match('/^tag:(?P<name>.*),(?P<date>\d{4}-?\d{0,2}-?\d{0,2}):(?P<specific>.*)(.*:)*$/', $id, $matches)) {
293 $dvalid = false;
294 $nvalid = false;
295 $date = $matches['date'];
296 $d6 = strtotime($date);
297 if ((strlen($date) == 4) && $date <= date('Y')) {
298 $dvalid = true;
299 } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
300 $dvalid = true;
301 } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
302 $dvalid = true;
304 $validator = new Validator\EmailAddress;
305 if ($validator->isValid($matches['name'])) {
306 $nvalid = true;
307 } else {
308 $nvalid = $validator->isValid('info@' . $matches['name']);
310 return $dvalid && $nvalid;
313 return false;
317 * Set entry content
319 * @param DOMDocument $dom
320 * @param DOMElement $root
321 * @return void
322 * @throws Writer\Exception\InvalidArgumentException
324 protected function _setContent(DOMDocument $dom, DOMElement $root)
326 $content = $this->getDataContainer()->getContent();
327 if (!$content && !$this->getDataContainer()->getLink()) {
328 $message = 'Atom 1.0 entry elements MUST contain exactly one '
329 . 'atom:content element, or as an alternative, at least one link '
330 . 'with a rel attribute of "alternate" to indicate an alternate '
331 . 'method to consume the content.';
332 $exception = new Writer\Exception\InvalidArgumentException($message);
333 if (!$this->ignoreExceptions) {
334 throw $exception;
335 } else {
336 $this->exceptions[] = $exception;
337 return;
340 if (!$content) {
341 return;
343 $element = $dom->createElement('content');
344 $element->setAttribute('type', 'xhtml');
345 $xhtmlElement = $this->_loadXhtml($content);
346 $xhtml = $dom->importNode($xhtmlElement, true);
347 $element->appendChild($xhtml);
348 $root->appendChild($element);
352 * Load a HTML string and attempt to normalise to XML
354 protected function _loadXhtml($content)
356 $xhtml = '';
357 if (class_exists('tidy', false)) {
358 $tidy = new \tidy;
359 $config = array(
360 'output-xhtml' => true,
361 'show-body-only' => true,
362 'quote-nbsp' => false
364 $encoding = str_replace('-', '', $this->getEncoding());
365 $tidy->parseString($content, $config, $encoding);
366 $tidy->cleanRepair();
367 $xhtml = (string) $tidy;
368 } else {
369 $xhtml = $content;
371 $xhtml = preg_replace(array(
372 "/(<[\/]?)([a-zA-Z]+)/"
373 ), '$1xhtml:$2', $xhtml);
374 $dom = new DOMDocument('1.0', $this->getEncoding());
375 $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
376 . $xhtml . '</xhtml:div>');
377 return $dom->documentElement;
381 * Set entry categories
383 * @param DOMDocument $dom
384 * @param DOMElement $root
385 * @return void
387 protected function _setCategories(DOMDocument $dom, DOMElement $root)
389 $categories = $this->getDataContainer()->getCategories();
390 if (!$categories) {
391 return;
393 foreach ($categories as $cat) {
394 $category = $dom->createElement('category');
395 $category->setAttribute('term', $cat['term']);
396 if (isset($cat['label'])) {
397 $category->setAttribute('label', $cat['label']);
398 } else {
399 $category->setAttribute('label', $cat['term']);
401 if (isset($cat['scheme'])) {
402 $category->setAttribute('scheme', $cat['scheme']);
404 $root->appendChild($category);
409 * Append Source element (Atom 1.0 Feed Metadata)
411 * @param DOMDocument $dom
412 * @param DOMElement $root
413 * @return void
415 protected function _setSource(DOMDocument $dom, DOMElement $root)
417 $source = $this->getDataContainer()->getSource();
418 if (!$source) {
419 return;
421 $renderer = new Renderer\Feed\AtomSource($source);
422 $renderer->setType($this->getType());
423 $element = $renderer->render()->getElement();
424 $imported = $dom->importNode($element, true);
425 $root->appendChild($imported);