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;
20 * Internal array containing all data associated with this entry or item.
27 * Registered extensions
31 protected $extensions = [];
34 * Holds the value "atom" or "rss" depending on the feed type set when
39 protected $type = null;
42 * Constructor: Primarily triggers the registration of core extensions and
43 * loads those appropriate to this data container.
46 public function __construct()
48 Writer::registerCoreExtensions();
49 $this->_loadExtensions();
55 * The following option keys are supported:
56 * 'name' => (string) The name
57 * 'email' => (string) An optional email
58 * 'uri' => (string) An optional and valid URI
60 * @param array $author
61 * @throws Exception\InvalidArgumentException If any value of $author not follow the format.
64 public function addAuthor(array $author)
67 if (! array_key_exists('name', $author)
68 || empty($author['name'])
69 || ! is_string($author['name'])
71 throw new Exception\InvalidArgumentException(
72 'Invalid parameter: author array must include a "name" key with a non-empty string value'
76 if (isset($author['email'])) {
77 if (empty($author['email']) || ! is_string($author['email'])) {
78 throw new Exception\InvalidArgumentException(
79 'Invalid parameter: "email" array value must be a non-empty string'
83 if (isset($author['uri'])) {
84 if (empty($author['uri']) || ! is_string($author['uri']) ||
85 ! Uri::factory($author['uri'])->isValid()
87 throw new Exception\InvalidArgumentException(
88 'Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI'
93 $this->data['authors'][] = $author;
99 * Set an array with feed authors
102 * @param array $authors
105 public function addAuthors(array $authors)
107 foreach ($authors as $author) {
108 $this->addAuthor($author);
115 * Set the feed character encoding
117 * @param string $encoding
118 * @throws Exception\InvalidArgumentException
121 public function setEncoding($encoding)
123 if (empty($encoding) || ! is_string($encoding)) {
124 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
126 $this->data['encoding'] = $encoding;
132 * Get the feed character encoding
134 * @return string|null
136 public function getEncoding()
138 if (! array_key_exists('encoding', $this->data)) {
141 return $this->data['encoding'];
145 * Set the copyright entry
147 * @param string $copyright
148 * @throws Exception\InvalidArgumentException
151 public function setCopyright($copyright)
153 if (empty($copyright) || ! is_string($copyright)) {
154 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
156 $this->data['copyright'] = $copyright;
162 * Set the entry's content
164 * @param string $content
165 * @throws Exception\InvalidArgumentException
168 public function setContent($content)
170 if (empty($content) || ! is_string($content)) {
171 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
173 $this->data['content'] = $content;
179 * Set the feed creation date
181 * @param null|int|DateTime $date
182 * @throws Exception\InvalidArgumentException
185 public function setDateCreated($date = null)
187 if ($date === null) {
188 $date = new DateTime();
189 } elseif (is_int($date)) {
190 $date = new DateTime('@' . $date);
191 } elseif (! $date instanceof DateTime) {
192 throw new Exception\InvalidArgumentException(
193 'Invalid DateTime object or UNIX Timestamp passed as parameter'
196 $this->data['dateCreated'] = $date;
202 * Set the feed modification date
204 * @param null|int|DateTime $date
205 * @throws Exception\InvalidArgumentException
208 public function setDateModified($date = null)
210 if ($date === null) {
211 $date = new DateTime();
212 } elseif (is_int($date)) {
213 $date = new DateTime('@' . $date);
214 } elseif (! $date instanceof DateTime) {
215 throw new Exception\InvalidArgumentException(
216 'Invalid DateTime object or UNIX Timestamp passed as parameter'
219 $this->data['dateModified'] = $date;
225 * Set the feed description
227 * @param string $description
228 * @throws Exception\InvalidArgumentException
231 public function setDescription($description)
233 if (empty($description) || ! is_string($description)) {
234 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
236 $this->data['description'] = $description;
245 * @throws Exception\InvalidArgumentException
248 public function setId($id)
250 if (empty($id) || ! is_string($id)) {
251 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
253 $this->data['id'] = $id;
259 * Set a link to the HTML source of this entry
261 * @param string $link
262 * @throws Exception\InvalidArgumentException
265 public function setLink($link)
267 if (empty($link) || ! is_string($link) || ! Uri::factory($link)->isValid()) {
268 throw new Exception\InvalidArgumentException(
269 'Invalid parameter: parameter must be a non-empty string and valid URI/IRI'
272 $this->data['link'] = $link;
278 * Set the number of comments associated with this entry
281 * @throws Exception\InvalidArgumentException
284 public function setCommentCount($count)
286 if (! is_numeric($count) || (int) $count != $count || (int) $count < 0) {
287 throw new Exception\InvalidArgumentException(
288 'Invalid parameter: "count" must be a positive integer number or zero'
291 $this->data['commentCount'] = (int) $count;
297 * Set a link to a HTML page containing comments associated with this entry
299 * @param string $link
300 * @throws Exception\InvalidArgumentException
303 public function setCommentLink($link)
305 if (empty($link) || ! is_string($link) || ! Uri::factory($link)->isValid()) {
306 throw new Exception\InvalidArgumentException(
307 'Invalid parameter: "link" must be a non-empty string and valid URI/IRI'
310 $this->data['commentLink'] = $link;
316 * Set a link to an XML feed for any comments associated with this entry
319 * @throws Exception\InvalidArgumentException
322 public function setCommentFeedLink(array $link)
324 if (! isset($link['uri']) || ! is_string($link['uri']) || ! Uri::factory($link['uri'])->isValid()) {
325 throw new Exception\InvalidArgumentException(
326 'Invalid parameter: "link" must be a non-empty string and valid URI/IRI'
329 if (! isset($link['type']) || ! in_array($link['type'], ['atom', 'rss', 'rdf'])) {
330 throw new Exception\InvalidArgumentException('Invalid parameter: "type" must be one'
331 . ' of "atom", "rss" or "rdf"');
333 if (! isset($this->data['commentFeedLinks'])) {
334 $this->data['commentFeedLinks'] = [];
336 $this->data['commentFeedLinks'][] = $link;
342 * Set a links to an XML feed for any comments associated with this entry.
343 * Each link is an array with keys "uri" and "type", where type is one of:
344 * "atom", "rss" or "rdf".
346 * @param array $links
349 public function setCommentFeedLinks(array $links)
351 foreach ($links as $link) {
352 $this->setCommentFeedLink($link);
361 * @param string $title
362 * @throws Exception\InvalidArgumentException
365 public function setTitle($title)
367 if (empty($title) || ! is_string($title)) {
368 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
370 $this->data['title'] = $title;
376 * Get an array with feed authors
380 public function getAuthors()
382 if (! array_key_exists('authors', $this->data)) {
385 return $this->data['authors'];
389 * Get the entry content
393 public function getContent()
395 if (! array_key_exists('content', $this->data)) {
398 return $this->data['content'];
402 * Get the entry copyright information
406 public function getCopyright()
408 if (! array_key_exists('copyright', $this->data)) {
411 return $this->data['copyright'];
415 * Get the entry creation date
419 public function getDateCreated()
421 if (! array_key_exists('dateCreated', $this->data)) {
424 return $this->data['dateCreated'];
428 * Get the entry modification date
432 public function getDateModified()
434 if (! array_key_exists('dateModified', $this->data)) {
437 return $this->data['dateModified'];
441 * Get the entry description
445 public function getDescription()
447 if (! array_key_exists('description', $this->data)) {
450 return $this->data['description'];
458 public function getId()
460 if (! array_key_exists('id', $this->data)) {
463 return $this->data['id'];
467 * Get a link to the HTML source
469 * @return string|null
471 public function getLink()
473 if (! array_key_exists('link', $this->data)) {
476 return $this->data['link'];
485 public function getLinks()
487 if (! array_key_exists('links', $this->data)) {
490 return $this->data['links'];
494 * Get the entry title
498 public function getTitle()
500 if (! array_key_exists('title', $this->data)) {
503 return $this->data['title'];
507 * Get the number of comments/replies for current entry
511 public function getCommentCount()
513 if (! array_key_exists('commentCount', $this->data)) {
516 return $this->data['commentCount'];
520 * Returns a URI pointing to the HTML page where comments can be made on this entry
524 public function getCommentLink()
526 if (! array_key_exists('commentLink', $this->data)) {
529 return $this->data['commentLink'];
533 * Returns an array of URIs pointing to a feed of all comments for this entry
534 * where the array keys indicate the feed type (atom, rss or rdf).
538 public function getCommentFeedLinks()
540 if (! array_key_exists('commentFeedLinks', $this->data)) {
543 return $this->data['commentFeedLinks'];
547 * Add an entry category
549 * @param array $category
550 * @throws Exception\InvalidArgumentException
553 public function addCategory(array $category)
555 if (! isset($category['term'])) {
556 throw new Exception\InvalidArgumentException('Each category must be an array and '
557 . 'contain at least a "term" element containing the machine '
558 . ' readable category name');
560 if (isset($category['scheme'])) {
561 if (empty($category['scheme'])
562 || ! is_string($category['scheme'])
563 || ! Uri::factory($category['scheme'])->isValid()
565 throw new Exception\InvalidArgumentException('The Atom scheme or RSS domain of'
566 . ' a category must be a valid URI');
569 if (! isset($this->data['categories'])) {
570 $this->data['categories'] = [];
572 $this->data['categories'][] = $category;
578 * Set an array of entry categories
580 * @param array $categories
583 public function addCategories(array $categories)
585 foreach ($categories as $category) {
586 $this->addCategory($category);
593 * Get the entry categories
595 * @return string|null
597 public function getCategories()
599 if (! array_key_exists('categories', $this->data)) {
602 return $this->data['categories'];
606 * Adds an enclosure to the entry. The array parameter may contain the
607 * keys 'uri', 'type' and 'length'. Only 'uri' is required for Atom, though the
608 * others must also be provided or RSS rendering (where they are required)
609 * will throw an Exception.
611 * @param array $enclosure
612 * @throws Exception\InvalidArgumentException
615 public function setEnclosure(array $enclosure)
617 if (! isset($enclosure['uri'])) {
618 throw new Exception\InvalidArgumentException('Enclosure "uri" is not set');
620 if (! Uri::factory($enclosure['uri'])->isValid()) {
621 throw new Exception\InvalidArgumentException('Enclosure "uri" is not a valid URI/IRI');
623 $this->data['enclosure'] = $enclosure;
629 * Retrieve an array of all enclosures to be added to entry.
633 public function getEnclosure()
635 if (! array_key_exists('enclosure', $this->data)) {
638 return $this->data['enclosure'];
642 * Unset a specific data point
644 * @param string $name
647 public function remove($name)
649 if (isset($this->data[$name])) {
650 unset($this->data[$name]);
657 * Get registered extensions
661 public function getExtensions()
663 return $this->extensions;
667 * Return an Extension object with the matching name (postfixed with _Entry)
669 * @param string $name
672 public function getExtension($name)
674 if (array_key_exists($name . '\\Entry', $this->extensions)) {
675 return $this->extensions[$name . '\\Entry'];
681 * Set the current feed type being exported to "rss" or "atom". This allows
682 * other objects to gracefully choose whether to execute or not, depending
683 * on their appropriateness for the current type, e.g. renderers.
685 * @param string $type
688 public function setType($type)
695 * Retrieve the current or last feed type exported.
697 * @return string Value will be "rss" or "atom"
699 public function getType()
705 * Method overloading: call given method on first extension implementing it
707 * @param string $method
710 * @throws Exception\BadMethodCallException if no extensions implements the method
712 public function __call($method, $args)
714 foreach ($this->extensions as $extension) {
716 return call_user_func_array([$extension, $method], $args);
717 } catch (\BadMethodCallException $e) {
720 throw new Exception\BadMethodCallException('Method: ' . $method
721 . ' does not exist and could not be located on a registered Extension');
725 * Creates a new Zend\Feed\Writer\Source data container for use. This is NOT
726 * added to the current feed automatically, but is necessary to create a
727 * container with some initial values preset based on the current feed data.
731 public function createSource()
733 $source = new Source;
734 if ($this->getEncoding()) {
735 $source->setEncoding($this->getEncoding());
737 $source->setType($this->getType());
742 * Appends a Zend\Feed\Writer\Entry object representing a new entry/item
743 * the feed data container's internal group of entries.
745 * @param Source $source
748 public function setSource(Source $source)
750 $this->data['source'] = $source;
757 public function getSource()
759 if (isset($this->data['source'])) {
760 return $this->data['source'];
766 * Load extensions from Zend\Feed\Writer\Writer
770 // @codingStandardsIgnoreStart
771 protected function _loadExtensions()
773 // @codingStandardsIgnoreEnd
774 $all = Writer::getExtensions();
775 $manager = Writer::getExtensionManager();
776 $exts = $all['entry'];
777 foreach ($exts as $ext) {
778 $this->extensions[$ext] = $manager->get($ext);
779 $this->extensions[$ext]->setEncoding($this->getEncoding());