3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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;
19 * Contains all Feed level date to append in feed output
26 * Holds the value "atom" or "rss" depending on the feed type set when
31 protected $type = null;
36 protected $extensions;
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();
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)
64 if (! array_key_exists('name', $author)
65 || empty($author['name'])
66 || ! is_string($author['name'])
68 throw new Exception\InvalidArgumentException(
69 'Invalid parameter: author array must include a "name" key with a non-empty string value'
73 if (isset($author['email'])) {
74 if (empty($author['email']) || ! is_string($author['email'])) {
75 throw new Exception\InvalidArgumentException(
76 'Invalid parameter: "email" array value must be a non-empty string'
80 if (isset($author['uri'])) {
81 if (empty($author['uri']) || ! is_string($author['uri']) ||
82 ! Uri::factory($author['uri'])->isValid()
84 throw new Exception\InvalidArgumentException(
85 'Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI'
90 $this->data['authors'][] = $author;
96 * Set an array with feed authors
99 * @param array $authors
100 * @return AbstractFeed
102 public function addAuthors(array $authors)
104 foreach ($authors as $author) {
105 $this->addAuthor($author);
112 * Set the copyright entry
114 * @param string $copyright
115 * @throws Exception\InvalidArgumentException
116 * @return AbstractFeed
118 public function setCopyright($copyright)
120 if (empty($copyright) || ! is_string($copyright)) {
121 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
123 $this->data['copyright'] = $copyright;
129 * Set the feed creation date
131 * @param null|int|DateTime
132 * @throws Exception\InvalidArgumentException
133 * @return AbstractFeed
135 public function setDateCreated($date = null)
137 if ($date === null) {
138 $date = new DateTime();
139 } elseif (is_int($date)) {
140 $date = new DateTime('@' . $date);
141 } elseif (! $date instanceof DateTime) {
142 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
143 . ' passed as parameter');
145 $this->data['dateCreated'] = $date;
151 * Set the feed modification date
153 * @param null|int|DateTime
154 * @throws Exception\InvalidArgumentException
155 * @return AbstractFeed
157 public function setDateModified($date = null)
159 if ($date === null) {
160 $date = new DateTime();
161 } elseif (is_int($date)) {
162 $date = new DateTime('@' . $date);
163 } elseif (! $date instanceof DateTime) {
164 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
165 . ' passed as parameter');
167 $this->data['dateModified'] = $date;
173 * Set the feed last-build date. Ignored for Atom 1.0.
175 * @param null|int|DateTime
176 * @throws Exception\InvalidArgumentException
177 * @return AbstractFeed
179 public function setLastBuildDate($date = null)
181 if ($date === null) {
182 $date = new DateTime();
183 } elseif (is_int($date)) {
184 $date = new DateTime('@' . $date);
185 } elseif (! $date instanceof DateTime) {
186 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
187 . ' passed as parameter');
189 $this->data['lastBuildDate'] = $date;
195 * Set the feed description
197 * @param string $description
198 * @throws Exception\InvalidArgumentException
199 * @return AbstractFeed
201 public function setDescription($description)
203 if (empty($description) || ! is_string($description)) {
204 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
206 $this->data['description'] = $description;
212 * Set the feed generator entry
214 * @param array|string $name
215 * @param null|string $version
216 * @param null|string $uri
217 * @throws Exception\InvalidArgumentException
218 * @return AbstractFeed
220 public function setGenerator($name, $version = null, $uri = null)
222 if (is_array($name)) {
224 if (empty($data['name']) || ! is_string($data['name'])) {
225 throw new Exception\InvalidArgumentException('Invalid parameter: "name" must be a non-empty string');
227 $generator = ['name' => $data['name']];
228 if (isset($data['version'])) {
229 if (empty($data['version']) || ! is_string($data['version'])) {
230 throw new Exception\InvalidArgumentException(
231 'Invalid parameter: "version" must be a non-empty string'
234 $generator['version'] = $data['version'];
236 if (isset($data['uri'])) {
237 if (empty($data['uri']) || ! is_string($data['uri']) || ! Uri::factory($data['uri'])->isValid()) {
238 throw new Exception\InvalidArgumentException(
239 'Invalid parameter: "uri" must be a non-empty string and a valid URI/IRI'
242 $generator['uri'] = $data['uri'];
245 if (empty($name) || ! is_string($name)) {
246 throw new Exception\InvalidArgumentException('Invalid parameter: "name" must be a non-empty string');
248 $generator = ['name' => $name];
249 if (isset($version)) {
250 if (empty($version) || ! is_string($version)) {
251 throw new Exception\InvalidArgumentException(
252 'Invalid parameter: "version" must be a non-empty string'
255 $generator['version'] = $version;
258 if (empty($uri) || ! is_string($uri) || ! Uri::factory($uri)->isValid()) {
259 throw new Exception\InvalidArgumentException(
260 'Invalid parameter: "uri" must be a non-empty string and a valid URI/IRI'
263 $generator['uri'] = $uri;
266 $this->data['generator'] = $generator;
272 * Set the feed ID - URI or URN (via PCRE pattern) supported
275 * @throws Exception\InvalidArgumentException
276 * @return AbstractFeed
278 public function setId($id)
280 // @codingStandardsIgnoreStart
281 if ((empty($id) || ! is_string($id) || ! Uri::factory($id)->isValid())
282 && ! preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $id)
283 && ! $this->_validateTagUri($id)
285 // @codingStandardsIgnoreEnd
286 throw new Exception\InvalidArgumentException(
287 'Invalid parameter: parameter must be a non-empty string and valid URI/IRI'
290 $this->data['id'] = $id;
296 * Validate a URI using the tag scheme (RFC 4151)
301 // @codingStandardsIgnoreStart
302 protected function _validateTagUri($id)
304 // @codingStandardsIgnoreEnd
306 '/^tag:(?P<name>.*),(?P<date>\d{4}-?\d{0,2}-?\d{0,2}):(?P<specific>.*)(.*:)*$/',
311 $date = $matches['date'];
312 $d6 = strtotime($date);
313 if ((strlen($date) == 4) && $date <= date('Y')) {
315 } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
317 } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
320 $validator = new Validator\EmailAddress;
321 if ($validator->isValid($matches['name'])) {
324 $nvalid = $validator->isValid('info@' . $matches['name']);
326 return $dvalid && $nvalid;
332 * Set a feed image (URI at minimum). Parameter is a single array with the
333 * required key 'uri'. When rendering as RSS, the required keys are 'uri',
334 * 'title' and 'link'. RSS also specifies three optional parameters 'width',
335 * 'height' and 'description'. Only 'uri' is required and used for Atom rendering.
338 * @throws Exception\InvalidArgumentException
339 * @return AbstractFeed
341 public function setImage(array $data)
343 if (empty($data['uri']) || ! is_string($data['uri'])
344 || ! Uri::factory($data['uri'])->isValid()
346 throw new Exception\InvalidArgumentException('Invalid parameter: parameter \'uri\''
347 . ' must be a non-empty string and valid URI/IRI');
349 $this->data['image'] = $data;
355 * Set the feed language
357 * @param string $language
358 * @throws Exception\InvalidArgumentException
359 * @return AbstractFeed
361 public function setLanguage($language)
363 if (empty($language) || ! is_string($language)) {
364 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
366 $this->data['language'] = $language;
372 * Set a link to the HTML source
374 * @param string $link
375 * @throws Exception\InvalidArgumentException
376 * @return AbstractFeed
378 public function setLink($link)
380 if (empty($link) || ! is_string($link) || ! Uri::factory($link)->isValid()) {
381 throw new Exception\InvalidArgumentException(
382 'Invalid parameter: parameter must be a non-empty string and valid URI/IRI'
385 $this->data['link'] = $link;
391 * Set a link to an XML feed for any feed type/version
393 * @param string $link
394 * @param string $type
395 * @throws Exception\InvalidArgumentException
396 * @return AbstractFeed
398 public function setFeedLink($link, $type)
400 if (empty($link) || ! is_string($link) || ! Uri::factory($link)->isValid()) {
401 throw new Exception\InvalidArgumentException(
402 'Invalid parameter: "link"" must be a non-empty string and valid URI/IRI'
405 if (! in_array(strtolower($type), ['rss', 'rdf', 'atom'])) {
406 throw new Exception\InvalidArgumentException(
407 'Invalid parameter: "type"; You must declare the type of feed the link points to, i.e. RSS, RDF or Atom'
410 $this->data['feedLinks'][strtolower($type)] = $link;
418 * @param string $title
419 * @throws Exception\InvalidArgumentException
420 * @return AbstractFeed
422 public function setTitle($title)
424 if (empty($title) || ! is_string($title)) {
425 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
427 $this->data['title'] = $title;
433 * Set the feed character encoding
435 * @param string $encoding
436 * @throws Exception\InvalidArgumentException
437 * @return AbstractFeed
439 public function setEncoding($encoding)
441 if (empty($encoding) || ! is_string($encoding)) {
442 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
444 $this->data['encoding'] = $encoding;
450 * Set the feed's base URL
453 * @throws Exception\InvalidArgumentException
454 * @return AbstractFeed
456 public function setBaseUrl($url)
458 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) {
459 throw new Exception\InvalidArgumentException('Invalid parameter: "url" array value'
460 . ' must be a non-empty string and valid URI/IRI');
462 $this->data['baseUrl'] = $url;
468 * Add a Pubsubhubbub hub endpoint URL
471 * @throws Exception\InvalidArgumentException
472 * @return AbstractFeed
474 public function addHub($url)
476 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) {
477 throw new Exception\InvalidArgumentException('Invalid parameter: "url" array value'
478 . ' must be a non-empty string and valid URI/IRI');
480 if (! isset($this->data['hubs'])) {
481 $this->data['hubs'] = [];
483 $this->data['hubs'][] = $url;
489 * Add Pubsubhubbub hub endpoint URLs
492 * @return AbstractFeed
494 public function addHubs(array $urls)
496 foreach ($urls as $url) {
504 * Add a feed category
506 * @param array $category
507 * @throws Exception\InvalidArgumentException
508 * @return AbstractFeed
510 public function addCategory(array $category)
512 if (! isset($category['term'])) {
513 throw new Exception\InvalidArgumentException('Each category must be an array and '
514 . 'contain at least a "term" element containing the machine '
515 . ' readable category name');
517 if (isset($category['scheme'])) {
518 if (empty($category['scheme'])
519 || ! is_string($category['scheme'])
520 || ! Uri::factory($category['scheme'])->isValid()
522 throw new Exception\InvalidArgumentException('The Atom scheme or RSS domain of'
523 . ' a category must be a valid URI');
526 if (! isset($this->data['categories'])) {
527 $this->data['categories'] = [];
529 $this->data['categories'][] = $category;
535 * Set an array of feed categories
537 * @param array $categories
538 * @return AbstractFeed
540 public function addCategories(array $categories)
542 foreach ($categories as $category) {
543 $this->addCategory($category);
550 * Get a single author
553 * @return string|null
555 public function getAuthor($index = 0)
557 if (isset($this->data['authors'][$index])) {
558 return $this->data['authors'][$index];
565 * Get an array with feed authors
569 public function getAuthors()
571 if (! array_key_exists('authors', $this->data)) {
574 return $this->data['authors'];
578 * Get the copyright entry
580 * @return string|null
582 public function getCopyright()
584 if (! array_key_exists('copyright', $this->data)) {
587 return $this->data['copyright'];
591 * Get the feed creation date
593 * @return string|null
595 public function getDateCreated()
597 if (! array_key_exists('dateCreated', $this->data)) {
600 return $this->data['dateCreated'];
604 * Get the feed modification date
606 * @return string|null
608 public function getDateModified()
610 if (! array_key_exists('dateModified', $this->data)) {
613 return $this->data['dateModified'];
617 * Get the feed last-build date
619 * @return string|null
621 public function getLastBuildDate()
623 if (! array_key_exists('lastBuildDate', $this->data)) {
626 return $this->data['lastBuildDate'];
630 * Get the feed description
632 * @return string|null
634 public function getDescription()
636 if (! array_key_exists('description', $this->data)) {
639 return $this->data['description'];
643 * Get the feed generator entry
645 * @return string|null
647 public function getGenerator()
649 if (! array_key_exists('generator', $this->data)) {
652 return $this->data['generator'];
658 * @return string|null
660 public function getId()
662 if (! array_key_exists('id', $this->data)) {
665 return $this->data['id'];
669 * Get the feed image URI
673 public function getImage()
675 if (! array_key_exists('image', $this->data)) {
678 return $this->data['image'];
682 * Get the feed language
684 * @return string|null
686 public function getLanguage()
688 if (! array_key_exists('language', $this->data)) {
691 return $this->data['language'];
695 * Get a link to the HTML source
697 * @return string|null
699 public function getLink()
701 if (! array_key_exists('link', $this->data)) {
704 return $this->data['link'];
708 * Get a link to the XML feed
710 * @return string|null
712 public function getFeedLinks()
714 if (! array_key_exists('feedLinks', $this->data)) {
717 return $this->data['feedLinks'];
723 * @return string|null
725 public function getTitle()
727 if (! array_key_exists('title', $this->data)) {
730 return $this->data['title'];
734 * Get the feed character encoding
736 * @return string|null
738 public function getEncoding()
740 if (! array_key_exists('encoding', $this->data)) {
743 return $this->data['encoding'];
747 * Get the feed's base url
749 * @return string|null
751 public function getBaseUrl()
753 if (! array_key_exists('baseUrl', $this->data)) {
756 return $this->data['baseUrl'];
760 * Get the URLs used as Pubsubhubbub hubs endpoints
762 * @return string|null
764 public function getHubs()
766 if (! array_key_exists('hubs', $this->data)) {
769 return $this->data['hubs'];
773 * Get the feed categories
775 * @return string|null
777 public function getCategories()
779 if (! array_key_exists('categories', $this->data)) {
782 return $this->data['categories'];
786 * Resets the instance and deletes all data
790 public function reset()
796 * Set the current feed type being exported to "rss" or "atom". This allows
797 * other objects to gracefully choose whether to execute or not, depending
798 * on their appropriateness for the current type, e.g. renderers.
800 * @param string $type
801 * @return AbstractFeed
803 public function setType($type)
810 * Retrieve the current or last feed type exported.
812 * @return string Value will be "rss" or "atom"
814 public function getType()
820 * Unset a specific data point
822 * @param string $name
823 * @return AbstractFeed
825 public function remove($name)
827 if (isset($this->data[$name])) {
828 unset($this->data[$name]);
834 * Method overloading: call given method on first extension implementing it
836 * @param string $method
839 * @throws Exception\BadMethodCallException if no extensions implements the method
841 public function __call($method, $args)
843 foreach ($this->extensions as $extension) {
845 return call_user_func_array([$extension, $method], $args);
846 } catch (Exception\BadMethodCallException $e) {
849 throw new Exception\BadMethodCallException(
850 'Method: ' . $method . ' does not exist and could not be located on a registered Extension'
855 * Load extensions from Zend\Feed\Writer\Writer
857 * @throws Exception\RuntimeException
860 // @codingStandardsIgnoreStart
861 protected function _loadExtensions()
863 // @codingStandardsIgnoreEnd
864 $all = Writer::getExtensions();
865 $manager = Writer::getExtensionManager();
866 $exts = $all['feed'];
867 foreach ($exts as $ext) {
868 if (! $manager->has($ext)) {
869 throw new Exception\RuntimeException(
870 sprintf('Unable to load extension "%s"; could not resolve to class', $ext)
873 $this->extensions[$ext] = $manager->get($ext);
874 $this->extensions[$ext]->setEncoding($this->getEncoding());