1: <?php
2: /**
3: * Portions Copyright 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
4: * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
5: *
6: * @category Horde
7: * @package Feed
8: */
9:
10: /**
11: * The Horde_Feed_Base class is an abstract class representing feeds.
12: *
13: * Horde_Feed_Base implements two core PHP 5 interfaces: ArrayAccess
14: * and Iterator. In both cases the collection being treated as an
15: * array is considered to be the entry collection, such that iterating
16: * over the feed takes you through each of the feed's entries.
17: *
18: * @category Horde
19: * @package Feed
20: */
21: abstract class Horde_Feed_Base extends Horde_Xml_Element_List
22: {
23: /**
24: * Our root ("home") URI
25: *
26: * @var string
27: */
28: protected $_uri;
29:
30: /**
31: * @var Horde_Http_Client
32: */
33: protected $_httpClient;
34:
35: /**
36: * Feed constructor
37: *
38: * The Horde_Feed_Base constructor takes the URI of a feed or a
39: * feed represented as a string and loads it as XML.
40: *
41: * @throws Horde_Feed_Exception If loading the feed failed.
42: *
43: * @param mixed $xml The feed as a string, a DOMElement, or null.
44: * @param string $uri The full URI of the feed, or null if unknown.
45: */
46: public function __construct($xml = null, $uri = null, Horde_Http_Client $httpClient = null)
47: {
48: $this->_uri = $uri;
49:
50: if (is_null($httpClient)) {
51: $httpClient = new Horde_Http_Client();
52: }
53: $this->_httpClient = $httpClient;
54:
55: try {
56: parent::__construct($xml);
57: } catch (Horde_Xml_Element_Exception $e) {
58: throw new Horde_Feed_Exception('Unable to load feed: ' . $e->getMessage());
59: }
60: }
61:
62: /**
63: * Handle null or array values for $this->_element by initializing
64: * with $this->_emptyXml, and importing the array with
65: * Horde_Xml_Element::fromArray() if necessary.
66: *
67: * @see Horde_Xml_Element::__wakeup
68: * @see Horde_Xml_Element::fromArray
69: */
70: public function __wakeup()
71: {
72: // If we've been passed an array, we'll store it for importing
73: // after initializing with the default "empty" feed XML.
74: $importArray = null;
75: if (is_null($this->_element)) {
76: $this->_element = $this->_emptyXml;
77: } elseif (is_array($this->_element)) {
78: $importArray = $this->_element;
79: $this->_element = $this->_emptyXml;
80: }
81:
82: parent::__wakeup();
83:
84: if (!is_null($importArray)) {
85: $this->fromArray($importArray);
86: }
87: }
88:
89: /**
90: * Required by the Iterator interface.
91: *
92: * @internal
93: *
94: * @return mixed The current row, or null if no rows.
95: */
96: public function current()
97: {
98: return new $this->_listItemClassName(
99: $this->_listItems[$this->_listItemIndex], $this->_httpClient);
100: }
101: }
102: