12 * obtain it through the world-wide-web, please send an email |
12 * obtain it through the world-wide-web, please send an email |
13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Feed_Writer |
16 * @package Zend_Feed_Writer |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @version $Id: Entry.php 22662 2010-07-24 17:37:36Z mabe $ |
19 * @version $Id: Entry.php 25160 2012-12-18 15:17:16Z matthew $ |
20 */ |
20 */ |
21 |
21 |
22 /** |
22 /** |
23 * @see Zend_Date |
23 * @see Zend_Date |
24 */ |
24 */ |
32 require_once 'Zend/Feed/Writer/Source.php'; |
32 require_once 'Zend/Feed/Writer/Source.php'; |
33 |
33 |
34 /** |
34 /** |
35 * @category Zend |
35 * @category Zend |
36 * @package Zend_Feed_Writer |
36 * @package Zend_Feed_Writer |
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
37 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
39 */ |
39 */ |
40 class Zend_Feed_Writer_Entry |
40 class Zend_Feed_Writer_Entry |
41 { |
41 { |
42 |
42 |
44 * Internal array containing all data associated with this entry or item. |
44 * Internal array containing all data associated with this entry or item. |
45 * |
45 * |
46 * @var array |
46 * @var array |
47 */ |
47 */ |
48 protected $_data = array(); |
48 protected $_data = array(); |
49 |
49 |
50 /** |
50 /** |
51 * Registered extensions |
51 * Registered extensions |
52 * |
52 * |
53 * @var array |
53 * @var array |
54 */ |
54 */ |
55 protected $_extensions = array(); |
55 protected $_extensions = array(); |
56 |
56 |
57 /** |
57 /** |
58 * Holds the value "atom" or "rss" depending on the feed type set when |
58 * Holds the value "atom" or "rss" depending on the feed type set when |
59 * when last exported. |
59 * when last exported. |
60 * |
60 * |
61 * @var string |
61 * @var string |
62 */ |
62 */ |
63 protected $_type = null; |
63 protected $_type = null; |
64 |
64 |
65 /** |
65 /** |
66 * Constructor: Primarily triggers the registration of core extensions and |
66 * Constructor: Primarily triggers the registration of core extensions and |
67 * loads those appropriate to this data container. |
67 * loads those appropriate to this data container. |
68 * |
68 * |
69 * @return void |
69 * @return void |
82 */ |
82 */ |
83 public function addAuthor($name, $email = null, $uri = null) |
83 public function addAuthor($name, $email = null, $uri = null) |
84 { |
84 { |
85 $author = array(); |
85 $author = array(); |
86 if (is_array($name)) { |
86 if (is_array($name)) { |
87 if (!array_key_exists('name', $name) |
87 if (!array_key_exists('name', $name) |
88 || empty($name['name']) |
88 || empty($name['name']) |
89 || !is_string($name['name']) |
89 || !is_string($name['name']) |
90 ) { |
90 ) { |
91 require_once 'Zend/Feed/Exception.php'; |
91 require_once 'Zend/Feed/Exception.php'; |
92 throw new Zend_Feed_Exception('Invalid parameter: author array must include a "name" key with a non-empty string value'); |
92 throw new Zend_Feed_Exception('Invalid parameter: author array must include a "name" key with a non-empty string value'); |
93 } |
93 } |
98 throw new Zend_Feed_Exception('Invalid parameter: "email" array value must be a non-empty string'); |
98 throw new Zend_Feed_Exception('Invalid parameter: "email" array value must be a non-empty string'); |
99 } |
99 } |
100 $author['email'] = $name['email']; |
100 $author['email'] = $name['email']; |
101 } |
101 } |
102 if (isset($name['uri'])) { |
102 if (isset($name['uri'])) { |
103 if (empty($name['uri']) |
103 if (empty($name['uri']) |
104 || !is_string($name['uri']) |
104 || !is_string($name['uri']) |
105 || !Zend_Uri::check($name['uri']) |
105 || !Zend_Uri::check($name['uri']) |
106 ) { |
106 ) { |
107 require_once 'Zend/Feed/Exception.php'; |
107 require_once 'Zend/Feed/Exception.php'; |
108 throw new Zend_Feed_Exception('Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI'); |
108 throw new Zend_Feed_Exception('Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI'); |
109 } |
109 } |
112 /** |
112 /** |
113 * @deprecated |
113 * @deprecated |
114 * Array notation (above) is preferred and will be the sole supported input from ZF 2.0 |
114 * Array notation (above) is preferred and will be the sole supported input from ZF 2.0 |
115 */ |
115 */ |
116 } else { |
116 } else { |
117 if (empty($name['name']) || !is_string($name['name'])) { |
117 if (empty($name) || !is_string($name)) { |
118 require_once 'Zend/Feed/Exception.php'; |
118 require_once 'Zend/Feed/Exception.php'; |
119 throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string value'); |
119 throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string value'); |
120 } |
120 } |
121 $author['name'] = $name; |
121 $author['name'] = $name; |
122 if (isset($email)) { |
122 if (isset($email)) { |
212 public function setDateCreated($date = null) |
212 public function setDateCreated($date = null) |
213 { |
213 { |
214 $zdate = null; |
214 $zdate = null; |
215 if ($date === null) { |
215 if ($date === null) { |
216 $zdate = new Zend_Date; |
216 $zdate = new Zend_Date; |
217 } elseif (ctype_digit($date) && strlen($date) == 10) { |
|
218 $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP); |
|
219 } elseif ($date instanceof Zend_Date) { |
217 } elseif ($date instanceof Zend_Date) { |
220 $zdate = $date; |
218 $zdate = $date; |
|
219 } elseif (ctype_digit((string)$date)) { |
|
220 $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP); |
221 } else { |
221 } else { |
222 require_once 'Zend/Feed/Exception.php'; |
222 require_once 'Zend/Feed/Exception.php'; |
223 throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter'); |
223 throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter'); |
224 } |
224 } |
225 $this->_data['dateCreated'] = $zdate; |
225 $this->_data['dateCreated'] = $zdate; |
233 public function setDateModified($date = null) |
233 public function setDateModified($date = null) |
234 { |
234 { |
235 $zdate = null; |
235 $zdate = null; |
236 if ($date === null) { |
236 if ($date === null) { |
237 $zdate = new Zend_Date; |
237 $zdate = new Zend_Date; |
238 } elseif (ctype_digit($date) && strlen($date) == 10) { |
|
239 $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP); |
|
240 } elseif ($date instanceof Zend_Date) { |
238 } elseif ($date instanceof Zend_Date) { |
241 $zdate = $date; |
239 $zdate = $date; |
|
240 } elseif (ctype_digit((string)$date)) { |
|
241 $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP); |
242 } else { |
242 } else { |
243 require_once 'Zend/Feed/Exception.php'; |
243 require_once 'Zend/Feed/Exception.php'; |
244 throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter'); |
244 throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter'); |
245 } |
245 } |
246 $this->_data['dateModified'] = $zdate; |
246 $this->_data['dateModified'] = $zdate; |
293 * |
293 * |
294 * @return string|null |
294 * @return string|null |
295 */ |
295 */ |
296 public function setCommentCount($count) |
296 public function setCommentCount($count) |
297 { |
297 { |
298 if (empty($count) || !is_numeric($count) || (int) $count < 0) { |
298 if ( !is_numeric($count) || (int) $count < 0) { |
299 require_once 'Zend/Feed/Exception.php'; |
299 require_once 'Zend/Feed/Exception.php'; |
300 throw new Zend_Feed_Exception('Invalid parameter: "count" must be a non-empty integer number'); |
300 throw new Zend_Feed_Exception('Invalid parameter: "count" must be a non-empty integer number'); |
301 } |
301 } |
302 $this->_data['commentCount'] = (int) $count; |
302 $this->_data['commentCount'] = (int) $count; |
303 } |
303 } |
335 if (!isset($this->_data['commentFeedLinks'])) { |
335 if (!isset($this->_data['commentFeedLinks'])) { |
336 $this->_data['commentFeedLinks'] = array(); |
336 $this->_data['commentFeedLinks'] = array(); |
337 } |
337 } |
338 $this->_data['commentFeedLinks'][] = $link; |
338 $this->_data['commentFeedLinks'][] = $link; |
339 } |
339 } |
340 |
340 |
341 /** |
341 /** |
342 * Set a links to an XML feed for any comments associated with this entry. |
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: |
343 * Each link is an array with keys "uri" and "type", where type is one of: |
344 * "atom", "rss" or "rdf". |
344 * "atom", "rss" or "rdf". |
345 * |
345 * |
534 if (!array_key_exists('commentFeedLinks', $this->_data)) { |
534 if (!array_key_exists('commentFeedLinks', $this->_data)) { |
535 return null; |
535 return null; |
536 } |
536 } |
537 return $this->_data['commentFeedLinks']; |
537 return $this->_data['commentFeedLinks']; |
538 } |
538 } |
539 |
539 |
540 /** |
540 /** |
541 * Add a entry category |
541 * Add a entry category |
542 * |
542 * |
543 * @param string $category |
543 * @param string $category |
544 */ |
544 */ |
545 public function addCategory(array $category) |
545 public function addCategory(array $category) |
546 { |
546 { |
547 if (!isset($category['term'])) { |
547 if (!isset($category['term'])) { |
548 require_once 'Zend/Feed/Exception.php'; |
548 require_once 'Zend/Feed/Exception.php'; |
549 throw new Zend_Feed_Exception('Each category must be an array and ' |
549 throw new Zend_Feed_Exception('Each category must be an array and ' |
550 . 'contain at least a "term" element containing the machine ' |
550 . 'contain at least a "term" element containing the machine ' |
551 . ' readable category name'); |
551 . ' readable category name'); |
552 } |
552 } |
553 if (isset($category['scheme'])) { |
553 if (isset($category['scheme'])) { |
554 if (empty($category['scheme']) |
554 if (empty($category['scheme']) |
555 || !is_string($category['scheme']) |
555 || !is_string($category['scheme']) |
556 || !Zend_Uri::check($category['scheme']) |
556 || !Zend_Uri::check($category['scheme']) |
557 ) { |
557 ) { |
558 require_once 'Zend/Feed/Exception.php'; |
558 require_once 'Zend/Feed/Exception.php'; |
559 throw new Zend_Feed_Exception('The Atom scheme or RSS domain of' |
559 throw new Zend_Feed_Exception('The Atom scheme or RSS domain of' |
563 if (!isset($this->_data['categories'])) { |
563 if (!isset($this->_data['categories'])) { |
564 $this->_data['categories'] = array(); |
564 $this->_data['categories'] = array(); |
565 } |
565 } |
566 $this->_data['categories'][] = $category; |
566 $this->_data['categories'][] = $category; |
567 } |
567 } |
568 |
568 |
569 /** |
569 /** |
570 * Set an array of entry categories |
570 * Set an array of entry categories |
571 * |
571 * |
572 * @param array $categories |
572 * @param array $categories |
573 */ |
573 */ |
588 if (!array_key_exists('categories', $this->_data)) { |
588 if (!array_key_exists('categories', $this->_data)) { |
589 return null; |
589 return null; |
590 } |
590 } |
591 return $this->_data['categories']; |
591 return $this->_data['categories']; |
592 } |
592 } |
593 |
593 |
594 /** |
594 /** |
595 * Adds an enclosure to the entry. The array parameter may contain the |
595 * Adds an enclosure to the entry. The array parameter may contain the |
596 * keys 'uri', 'type' and 'length'. Only 'uri' is required for Atom, though the |
596 * keys 'uri', 'type' and 'length'. Only 'uri' is required for Atom, though the |
597 * others must also be provided or RSS rendering (where they are required) |
597 * others must also be provided or RSS rendering (where they are required) |
598 * will throw an Exception. |
598 * will throw an Exception. |
658 if (array_key_exists($name . '_Entry', $this->_extensions)) { |
658 if (array_key_exists($name . '_Entry', $this->_extensions)) { |
659 return $this->_extensions[$name . '_Entry']; |
659 return $this->_extensions[$name . '_Entry']; |
660 } |
660 } |
661 return null; |
661 return null; |
662 } |
662 } |
663 |
663 |
664 /** |
664 /** |
665 * Set the current feed type being exported to "rss" or "atom". This allows |
665 * Set the current feed type being exported to "rss" or "atom". This allows |
666 * other objects to gracefully choose whether to execute or not, depending |
666 * other objects to gracefully choose whether to execute or not, depending |
667 * on their appropriateness for the current type, e.g. renderers. |
667 * on their appropriateness for the current type, e.g. renderers. |
668 * |
668 * |
701 } |
701 } |
702 require_once 'Zend/Feed/Exception.php'; |
702 require_once 'Zend/Feed/Exception.php'; |
703 throw new Zend_Feed_Exception('Method: ' . $method |
703 throw new Zend_Feed_Exception('Method: ' . $method |
704 . ' does not exist and could not be located on a registered Extension'); |
704 . ' does not exist and could not be located on a registered Extension'); |
705 } |
705 } |
706 |
706 |
707 /** |
707 /** |
708 * Creates a new Zend_Feed_Writer_Source data container for use. This is NOT |
708 * Creates a new Zend_Feed_Writer_Source data container for use. This is NOT |
709 * added to the current feed automatically, but is necessary to create a |
709 * added to the current feed automatically, but is necessary to create a |
710 * container with some initial values preset based on the current feed data. |
710 * container with some initial values preset based on the current feed data. |
711 * |
711 * |