8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: Abstract.php 12507 2008-11-10 16:29:09Z matthew $
25
* @see Zend_Feed_Element
27
require_once 'Zend/Feed/Element.php';
31
* The Zend_Feed_Abstract class is an abstract class representing feeds.
33
* Zend_Feed_Abstract implements two core PHP 5 interfaces: ArrayAccess and
34
* Iterator. In both cases the collection being treated as an array is
35
* considered to be the entry collection, such that iterating over the
36
* feed takes you through each of the feed.s entries.
40
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
41
* @license http://framework.zend.com/license/new-bsd New BSD License
43
abstract class Zend_Feed_Abstract extends Zend_Feed_Element implements Iterator
46
* Current index on the collection of feed entries for the
47
* Iterator implementation.
51
protected $_entryIndex = 0;
54
* Cache of feed entries.
63
* The Zend_Feed_Abstract constructor takes the URI of a feed or a
64
* feed represented as a string and loads it as XML.
66
* @param string $uri The full URI of the feed to load, or NULL if not retrieved via HTTP or as an array.
67
* @param string $string The feed as a string, or NULL if retrieved via HTTP or as an array.
68
* @param Zend_Feed_Builder_Interface $builder The feed as a builder instance or NULL if retrieved as a string or via HTTP.
70
* @throws Zend_Feed_Exception If loading the feed failed.
72
public function __construct($uri = null, $string = null, Zend_Feed_Builder_Interface $builder = null)
75
// Retrieve the feed via HTTP
76
$client = Zend_Feed::getHttpClient();
77
$client->setUri($uri);
78
$response = $client->request('GET');
79
if ($response->getStatus() !== 200) {
81
* @see Zend_Feed_Exception
83
require_once 'Zend/Feed/Exception.php';
84
throw new Zend_Feed_Exception('Feed failed to load, got response code ' . $response->getStatus());
86
$this->_element = $response->getBody();
88
} elseif ($string !== null) {
89
// Retrieve the feed from $string
90
$this->_element = $string;
93
// Generate the feed from the array
94
$header = $builder->getHeader();
95
$this->_element = new DOMDocument('1.0', $header['charset']);
96
$root = $this->_mapFeedHeaders($header);
97
$this->_mapFeedEntries($root, $builder->getEntries());
98
$this->_element = $root;
99
$this->_buildEntryCache();
105
* Load the feed as an XML DOMDocument object
108
* @throws Zend_Feed_Exception
110
public function __wakeup()
112
@ini_set('track_errors', 1);
113
$doc = new DOMDocument;
114
$status = @$doc->loadXML($this->_element);
115
@ini_restore('track_errors');
118
// prevent the class to generate an undefined variable notice (ZF-2590)
119
if (!isset($php_errormsg)) {
120
if (function_exists('xdebug_is_enabled')) {
121
$php_errormsg = '(error message not available, when XDebug is running)';
123
$php_errormsg = '(error message not available)';
128
* @see Zend_Feed_Exception
130
require_once 'Zend/Feed/Exception.php';
131
throw new Zend_Feed_Exception("DOMDocument cannot parse XML: $php_errormsg");
134
$this->_element = $doc;
139
* Prepare for serialiation
143
public function __sleep()
145
$this->_element = $this->saveXML();
147
return array('_element');
152
* Cache the individual feed elements so they don't need to be
153
* searched for on every operation.
157
protected function _buildEntryCache()
159
$this->_entries = array();
160
foreach ($this->_element->childNodes as $child) {
161
if ($child->localName == $this->_entryElementName) {
162
$this->_entries[] = $child;
169
* Get the number of entries in this feed object.
171
* @return integer Entry count.
173
public function count()
175
return count($this->_entries);
180
* Required by the Iterator interface.
184
public function rewind()
186
$this->_entryIndex = 0;
191
* Required by the Iterator interface.
193
* @return mixed The current row, or null if no rows.
195
public function current()
197
return new $this->_entryClassName(
199
$this->_entries[$this->_entryIndex]);
204
* Required by the Iterator interface.
206
* @return mixed The current row number (starts at 0), or NULL if no rows
208
public function key()
210
return $this->_entryIndex;
215
* Required by the Iterator interface.
217
* @return mixed The next row, or null if no more rows.
219
public function next()
221
++$this->_entryIndex;
226
* Required by the Iterator interface.
228
* @return boolean Whether the iteration is valid
230
public function valid()
232
return 0 <= $this->_entryIndex && $this->_entryIndex < $this->count();
236
* Generate the header of the feed when working in write mode
238
* @param array $array the data to use
239
* @return DOMElement root node
241
abstract protected function _mapFeedHeaders($array);
244
* Generate the entries of the feed when working in write mode
246
* @param DOMElement $root the root node to use
247
* @param array $array the data to use
248
* @return DOMElement root node
250
abstract protected function _mapFeedEntries(DOMElement $root, $array);
253
* Send feed to a http client with the correct header
255
* @throws Zend_Feed_Exception if headers have already been sent
258
abstract public function send();