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 / AbstractFeed.php
blob389a987d2fec556d701c8a21fb4b78ea47d4f01b
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;
12 use DateTime;
13 use Zend\Feed\Uri;
14 use Zend\Validator;
16 class AbstractFeed
18 /**
19 * Contains all Feed level date to append in feed output
21 * @var array
23 protected $data = array();
25 /**
26 * Holds the value "atom" or "rss" depending on the feed type set when
27 * when last exported.
29 * @var string
31 protected $type = null;
33 /**
34 * @var $extensions
36 protected $extensions;
38 /**
39 * Constructor: Primarily triggers the registration of core extensions and
40 * loads those appropriate to this data container.
43 public function __construct()
45 Writer::registerCoreExtensions();
46 $this->_loadExtensions();
49 /**
50 * Set a single author
52 * The following option keys are supported:
53 * 'name' => (string) The name
54 * 'email' => (string) An optional email
55 * 'uri' => (string) An optional and valid URI
57 * @param array $author
58 * @throws Exception\InvalidArgumentException If any value of $author not follow the format.
59 * @return AbstractFeed
61 public function addAuthor(array $author)
63 // Check array values
64 if (!array_key_exists('name', $author)
65 || empty($author['name'])
66 || !is_string($author['name'])
67 ) {
68 throw new Exception\InvalidArgumentException(
69 'Invalid parameter: author array must include a "name" key with a non-empty string value');
72 if (isset($author['email'])) {
73 if (empty($author['email']) || !is_string($author['email'])) {
74 throw new Exception\InvalidArgumentException(
75 'Invalid parameter: "email" array value must be a non-empty string');
78 if (isset($author['uri'])) {
79 if (empty($author['uri']) || !is_string($author['uri']) ||
80 !Uri::factory($author['uri'])->isValid()
81 ) {
82 throw new Exception\InvalidArgumentException(
83 'Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI');
87 $this->data['authors'][] = $author;
89 return $this;
92 /**
93 * Set an array with feed authors
95 * @see addAuthor
96 * @param array $authors
97 * @return AbstractFeed
99 public function addAuthors(array $authors)
101 foreach ($authors as $author) {
102 $this->addAuthor($author);
105 return $this;
109 * Set the copyright entry
111 * @param string $copyright
112 * @throws Exception\InvalidArgumentException
113 * @return AbstractFeed
115 public function setCopyright($copyright)
117 if (empty($copyright) || !is_string($copyright)) {
118 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
120 $this->data['copyright'] = $copyright;
122 return $this;
126 * Set the feed creation date
128 * @param null|int|DateTime
129 * @throws Exception\InvalidArgumentException
130 * @return AbstractFeed
132 public function setDateCreated($date = null)
134 if ($date === null) {
135 $date = new DateTime();
136 } elseif (is_int($date)) {
137 $date = new DateTime('@' . $date);
138 } elseif (!$date instanceof DateTime) {
139 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
140 . ' passed as parameter');
142 $this->data['dateCreated'] = $date;
144 return $this;
148 * Set the feed modification date
150 * @param null|int|DateTime
151 * @throws Exception\InvalidArgumentException
152 * @return AbstractFeed
154 public function setDateModified($date = null)
156 if ($date === null) {
157 $date = new DateTime();
158 } elseif (is_int($date)) {
159 $date = new DateTime('@' . $date);
160 } elseif (!$date instanceof DateTime) {
161 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
162 . ' passed as parameter');
164 $this->data['dateModified'] = $date;
166 return $this;
170 * Set the feed last-build date. Ignored for Atom 1.0.
172 * @param null|int|DateTime
173 * @throws Exception\InvalidArgumentException
174 * @return AbstractFeed
176 public function setLastBuildDate($date = null)
178 if ($date === null) {
179 $date = new DateTime();
180 } elseif (is_int($date)) {
181 $date = new DateTime('@' . $date);
182 } elseif (!$date instanceof DateTime) {
183 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
184 . ' passed as parameter');
186 $this->data['lastBuildDate'] = $date;
188 return $this;
192 * Set the feed description
194 * @param string $description
195 * @throws Exception\InvalidArgumentException
196 * @return AbstractFeed
198 public function setDescription($description)
200 if (empty($description) || !is_string($description)) {
201 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
203 $this->data['description'] = $description;
205 return $this;
209 * Set the feed generator entry
211 * @param array|string $name
212 * @param null|string $version
213 * @param null|string $uri
214 * @throws Exception\InvalidArgumentException
215 * @return AbstractFeed
217 public function setGenerator($name, $version = null, $uri = null)
219 if (is_array($name)) {
220 $data = $name;
221 if (empty($data['name']) || !is_string($data['name'])) {
222 throw new Exception\InvalidArgumentException('Invalid parameter: "name" must be a non-empty string');
224 $generator = array('name' => $data['name']);
225 if (isset($data['version'])) {
226 if (empty($data['version']) || !is_string($data['version'])) {
227 throw new Exception\InvalidArgumentException('Invalid parameter: "version" must be a non-empty string');
229 $generator['version'] = $data['version'];
231 if (isset($data['uri'])) {
232 if (empty($data['uri']) || !is_string($data['uri']) || !Uri::factory($data['uri'])->isValid()) {
233 throw new Exception\InvalidArgumentException('Invalid parameter: "uri" must be a non-empty string and a valid URI/IRI');
235 $generator['uri'] = $data['uri'];
237 } else {
238 if (empty($name) || !is_string($name)) {
239 throw new Exception\InvalidArgumentException('Invalid parameter: "name" must be a non-empty string');
241 $generator = array('name' => $name);
242 if (isset($version)) {
243 if (empty($version) || !is_string($version)) {
244 throw new Exception\InvalidArgumentException('Invalid parameter: "version" must be a non-empty string');
246 $generator['version'] = $version;
248 if (isset($uri)) {
249 if (empty($uri) || !is_string($uri) || !Uri::factory($uri)->isValid()) {
250 throw new Exception\InvalidArgumentException('Invalid parameter: "uri" must be a non-empty string and a valid URI/IRI');
252 $generator['uri'] = $uri;
255 $this->data['generator'] = $generator;
257 return $this;
261 * Set the feed ID - URI or URN (via PCRE pattern) supported
263 * @param string $id
264 * @throws Exception\InvalidArgumentException
265 * @return AbstractFeed
267 public function setId($id)
269 if ((empty($id) || !is_string($id) || !Uri::factory($id)->isValid())
270 && !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $id)
271 && !$this->_validateTagUri($id)
273 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
275 $this->data['id'] = $id;
277 return $this;
281 * Validate a URI using the tag scheme (RFC 4151)
283 * @param string $id
284 * @return bool
286 protected function _validateTagUri($id)
288 if (preg_match('/^tag:(?P<name>.*),(?P<date>\d{4}-?\d{0,2}-?\d{0,2}):(?P<specific>.*)(.*:)*$/', $id, $matches)) {
289 $dvalid = false;
290 $nvalid = false;
291 $date = $matches['date'];
292 $d6 = strtotime($date);
293 if ((strlen($date) == 4) && $date <= date('Y')) {
294 $dvalid = true;
295 } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
296 $dvalid = true;
297 } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
298 $dvalid = true;
300 $validator = new Validator\EmailAddress;
301 if ($validator->isValid($matches['name'])) {
302 $nvalid = true;
303 } else {
304 $nvalid = $validator->isValid('info@' . $matches['name']);
306 return $dvalid && $nvalid;
309 return false;
313 * Set a feed image (URI at minimum). Parameter is a single array with the
314 * required key 'uri'. When rendering as RSS, the required keys are 'uri',
315 * 'title' and 'link'. RSS also specifies three optional parameters 'width',
316 * 'height' and 'description'. Only 'uri' is required and used for Atom rendering.
318 * @param array $data
319 * @throws Exception\InvalidArgumentException
320 * @return AbstractFeed
322 public function setImage(array $data)
324 if (empty($data['uri']) || !is_string($data['uri'])
325 || !Uri::factory($data['uri'])->isValid()
327 throw new Exception\InvalidArgumentException('Invalid parameter: parameter \'uri\''
328 . ' must be a non-empty string and valid URI/IRI');
330 $this->data['image'] = $data;
332 return $this;
336 * Set the feed language
338 * @param string $language
339 * @throws Exception\InvalidArgumentException
340 * @return AbstractFeed
342 public function setLanguage($language)
344 if (empty($language) || !is_string($language)) {
345 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
347 $this->data['language'] = $language;
349 return $this;
353 * Set a link to the HTML source
355 * @param string $link
356 * @throws Exception\InvalidArgumentException
357 * @return AbstractFeed
359 public function setLink($link)
361 if (empty($link) || !is_string($link) || !Uri::factory($link)->isValid()) {
362 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
364 $this->data['link'] = $link;
366 return $this;
370 * Set a link to an XML feed for any feed type/version
372 * @param string $link
373 * @param string $type
374 * @throws Exception\InvalidArgumentException
375 * @return AbstractFeed
377 public function setFeedLink($link, $type)
379 if (empty($link) || !is_string($link) || !Uri::factory($link)->isValid()) {
380 throw new Exception\InvalidArgumentException('Invalid parameter: "link"" must be a non-empty string and valid URI/IRI');
382 if (!in_array(strtolower($type), array('rss', 'rdf', 'atom'))) {
383 throw new Exception\InvalidArgumentException('Invalid parameter: "type"; You must declare the type of feed the link points to, i.e. RSS, RDF or Atom');
385 $this->data['feedLinks'][strtolower($type)] = $link;
387 return $this;
391 * Set the feed title
393 * @param string $title
394 * @throws Exception\InvalidArgumentException
395 * @return AbstractFeed
397 public function setTitle($title)
399 if (empty($title) || !is_string($title)) {
400 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
402 $this->data['title'] = $title;
404 return $this;
408 * Set the feed character encoding
410 * @param string $encoding
411 * @throws Exception\InvalidArgumentException
412 * @return AbstractFeed
414 public function setEncoding($encoding)
416 if (empty($encoding) || !is_string($encoding)) {
417 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
419 $this->data['encoding'] = $encoding;
421 return $this;
425 * Set the feed's base URL
427 * @param string $url
428 * @throws Exception\InvalidArgumentException
429 * @return AbstractFeed
431 public function setBaseUrl($url)
433 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
434 throw new Exception\InvalidArgumentException('Invalid parameter: "url" array value'
435 . ' must be a non-empty string and valid URI/IRI');
437 $this->data['baseUrl'] = $url;
439 return $this;
443 * Add a Pubsubhubbub hub endpoint URL
445 * @param string $url
446 * @throws Exception\InvalidArgumentException
447 * @return AbstractFeed
449 public function addHub($url)
451 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
452 throw new Exception\InvalidArgumentException('Invalid parameter: "url" array value'
453 . ' must be a non-empty string and valid URI/IRI');
455 if (!isset($this->data['hubs'])) {
456 $this->data['hubs'] = array();
458 $this->data['hubs'][] = $url;
460 return $this;
464 * Add Pubsubhubbub hub endpoint URLs
466 * @param array $urls
467 * @return AbstractFeed
469 public function addHubs(array $urls)
471 foreach ($urls as $url) {
472 $this->addHub($url);
475 return $this;
479 * Add a feed category
481 * @param array $category
482 * @throws Exception\InvalidArgumentException
483 * @return AbstractFeed
485 public function addCategory(array $category)
487 if (!isset($category['term'])) {
488 throw new Exception\InvalidArgumentException('Each category must be an array and '
489 . 'contain at least a "term" element containing the machine '
490 . ' readable category name');
492 if (isset($category['scheme'])) {
493 if (empty($category['scheme'])
494 || !is_string($category['scheme'])
495 || !Uri::factory($category['scheme'])->isValid()
497 throw new Exception\InvalidArgumentException('The Atom scheme or RSS domain of'
498 . ' a category must be a valid URI');
501 if (!isset($this->data['categories'])) {
502 $this->data['categories'] = array();
504 $this->data['categories'][] = $category;
506 return $this;
510 * Set an array of feed categories
512 * @param array $categories
513 * @return AbstractFeed
515 public function addCategories(array $categories)
517 foreach ($categories as $category) {
518 $this->addCategory($category);
521 return $this;
525 * Get a single author
527 * @param int $index
528 * @return string|null
530 public function getAuthor($index = 0)
532 if (isset($this->data['authors'][$index])) {
533 return $this->data['authors'][$index];
536 return null;
540 * Get an array with feed authors
542 * @return array
544 public function getAuthors()
546 if (!array_key_exists('authors', $this->data)) {
547 return null;
549 return $this->data['authors'];
553 * Get the copyright entry
555 * @return string|null
557 public function getCopyright()
559 if (!array_key_exists('copyright', $this->data)) {
560 return null;
562 return $this->data['copyright'];
566 * Get the feed creation date
568 * @return string|null
570 public function getDateCreated()
572 if (!array_key_exists('dateCreated', $this->data)) {
573 return null;
575 return $this->data['dateCreated'];
579 * Get the feed modification date
581 * @return string|null
583 public function getDateModified()
585 if (!array_key_exists('dateModified', $this->data)) {
586 return null;
588 return $this->data['dateModified'];
592 * Get the feed last-build date
594 * @return string|null
596 public function getLastBuildDate()
598 if (!array_key_exists('lastBuildDate', $this->data)) {
599 return null;
601 return $this->data['lastBuildDate'];
605 * Get the feed description
607 * @return string|null
609 public function getDescription()
611 if (!array_key_exists('description', $this->data)) {
612 return null;
614 return $this->data['description'];
618 * Get the feed generator entry
620 * @return string|null
622 public function getGenerator()
624 if (!array_key_exists('generator', $this->data)) {
625 return null;
627 return $this->data['generator'];
631 * Get the feed ID
633 * @return string|null
635 public function getId()
637 if (!array_key_exists('id', $this->data)) {
638 return null;
640 return $this->data['id'];
644 * Get the feed image URI
646 * @return array
648 public function getImage()
650 if (!array_key_exists('image', $this->data)) {
651 return null;
653 return $this->data['image'];
657 * Get the feed language
659 * @return string|null
661 public function getLanguage()
663 if (!array_key_exists('language', $this->data)) {
664 return null;
666 return $this->data['language'];
670 * Get a link to the HTML source
672 * @return string|null
674 public function getLink()
676 if (!array_key_exists('link', $this->data)) {
677 return null;
679 return $this->data['link'];
683 * Get a link to the XML feed
685 * @return string|null
687 public function getFeedLinks()
689 if (!array_key_exists('feedLinks', $this->data)) {
690 return null;
692 return $this->data['feedLinks'];
696 * Get the feed title
698 * @return string|null
700 public function getTitle()
702 if (!array_key_exists('title', $this->data)) {
703 return null;
705 return $this->data['title'];
709 * Get the feed character encoding
711 * @return string|null
713 public function getEncoding()
715 if (!array_key_exists('encoding', $this->data)) {
716 return 'UTF-8';
718 return $this->data['encoding'];
722 * Get the feed's base url
724 * @return string|null
726 public function getBaseUrl()
728 if (!array_key_exists('baseUrl', $this->data)) {
729 return null;
731 return $this->data['baseUrl'];
735 * Get the URLs used as Pubsubhubbub hubs endpoints
737 * @return string|null
739 public function getHubs()
741 if (!array_key_exists('hubs', $this->data)) {
742 return null;
744 return $this->data['hubs'];
748 * Get the feed categories
750 * @return string|null
752 public function getCategories()
754 if (!array_key_exists('categories', $this->data)) {
755 return null;
757 return $this->data['categories'];
761 * Resets the instance and deletes all data
763 * @return void
765 public function reset()
767 $this->data = array();
771 * Set the current feed type being exported to "rss" or "atom". This allows
772 * other objects to gracefully choose whether to execute or not, depending
773 * on their appropriateness for the current type, e.g. renderers.
775 * @param string $type
776 * @return AbstractFeed
778 public function setType($type)
780 $this->type = $type;
781 return $this;
785 * Retrieve the current or last feed type exported.
787 * @return string Value will be "rss" or "atom"
789 public function getType()
791 return $this->type;
795 * Unset a specific data point
797 * @param string $name
798 * @return AbstractFeed
800 public function remove($name)
802 if (isset($this->data[$name])) {
803 unset($this->data[$name]);
805 return $this;
809 * Method overloading: call given method on first extension implementing it
811 * @param string $method
812 * @param array $args
813 * @return mixed
814 * @throws Exception\BadMethodCallException if no extensions implements the method
816 public function __call($method, $args)
818 foreach ($this->extensions as $extension) {
819 try {
820 return call_user_func_array(array($extension, $method), $args);
821 } catch (Exception\BadMethodCallException $e) {
824 throw new Exception\BadMethodCallException(
825 'Method: ' . $method . ' does not exist and could not be located on a registered Extension'
830 * Load extensions from Zend\Feed\Writer\Writer
832 * @throws Exception\RuntimeException
833 * @return void
835 protected function _loadExtensions()
837 $all = Writer::getExtensions();
838 $manager = Writer::getExtensionManager();
839 $exts = $all['feed'];
840 foreach ($exts as $ext) {
841 if (!$manager->has($ext)) {
842 throw new Exception\RuntimeException(sprintf('Unable to load extension "%s"; could not resolve to class', $ext));
844 $this->extensions[$ext] = $manager->get($ext);
845 $this->extensions[$ext]->setEncoding($this->getEncoding());