/RELEASE_0_2_8/Tools/Phlickr/Framework/ListBase.php

https://bitbucket.org/haichau59/manga · PHP · 192 lines · 61 code · 6 blank · 125 comment · 2 complexity · 276b1f8ef466ad32e88813ca4fab2ac0 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author Andrew Morton <drewish@katherinehouse.com>
  5. * @license http://opensource.org/licenses/lgpl-license.php
  6. * GNU Lesser General Public License, Version 2.1
  7. * @package Phlickr
  8. */
  9. /**
  10. * This class implements the IList interface.
  11. */
  12. require_once 'Phlickr/Framework/IList.php';
  13. /**
  14. * A base class for the Phlickr lists that wrap XML returned by API calls.
  15. *
  16. * This class provide default implementations for all its functions. You can
  17. * probably obtain better performance by overriding them.
  18. *
  19. * @author Andrew Morton <drewish@katherinehouse.com>
  20. * @package Phlickr
  21. */
  22. abstract class Phlickr_Framework_ListBase implements Phlickr_Framework_IList {
  23. /**
  24. * Request the PhotoList is based on.
  25. *
  26. * @var object Phlickr_Request
  27. */
  28. protected $_request = null;
  29. /**
  30. * XML from Flickr.
  31. *
  32. * @var object SimpleXMLElement
  33. */
  34. protected $_cachedXml = null;
  35. /**
  36. * The name of the XML element in the response that defines this list.
  37. *
  38. * @var string
  39. */
  40. protected $_respListElement;
  41. /**
  42. * The name of the XML element in the response that defines this list's
  43. * members.
  44. *
  45. * @var string
  46. */
  47. protected $_respElement;
  48. /**
  49. * Constructor.
  50. *
  51. * @param object Phlickr_Request $request
  52. * @param string $responseElement Name of the XML element in the response
  53. * that defines this list's members.
  54. * @param string $responseListElement Name of the XML element in the
  55. * response that defines this list.
  56. * @throws Phlickr_Exception, Phlickr_ConnectionException,
  57. * Phlickr_XmlParseException
  58. */
  59. function __construct(Phlickr_Request $request, $responseElement, $responseListElement) {
  60. $this->_respListElement = $responseListElement;
  61. $this->_respElement = $responseElement;
  62. $this->_request = $request;
  63. $this->load();
  64. }
  65. /**
  66. * Returns the name of the XML element in the response that defines the
  67. * list object.
  68. *
  69. * If the response looks like <resp><objs><obj><obj></objs></resp> this
  70. * should return "objs".
  71. *
  72. * @return string
  73. */
  74. protected function getResponseListElement() {
  75. return $this->_respListElement;
  76. }
  77. /**
  78. * Returns the name of the XML element in the response that defines the
  79. * object.
  80. *
  81. * If the response looks like <resp><objs><obj><obj></objs></resp> this
  82. * should return "obj".
  83. *
  84. * @return string
  85. */
  86. protected function getResponseElement() {
  87. return $this->_respElement;
  88. }
  89. /**
  90. * Return a reference to this object's Phlickr_Api.
  91. *
  92. * @return object Plickr_Api
  93. */
  94. public function &getApi() {
  95. return $this->_request->getApi();
  96. }
  97. /**
  98. * Return the Phlickr_Request the PhotoList is based on.
  99. *
  100. * @return object Phlickr_Request
  101. */
  102. public function getRequest() {
  103. return $this->_request;
  104. }
  105. /**
  106. * Connect to Flickr and update class's cashedXml member.
  107. *
  108. * @param boolean $allowCached If a cached result exists, should it be
  109. * returned?
  110. * @return object SimpleXMLElement
  111. * @throws Phlickr_ConnectionException, Phlickr_XmlParseException
  112. */
  113. protected function requestXml($allowCached = false) {
  114. $response = $this->getRequest()->execute($allowCached);
  115. $xml = $response->xml->{$this->getResponseListElement()};
  116. if (is_null($xml)) {
  117. throw new Exception(
  118. sprintf("Could not load object with request: '%s'.",
  119. $this->getRequest())
  120. );
  121. }
  122. return $xml;
  123. }
  124. /**
  125. * Load the complete information on object.
  126. *
  127. * If this request has been executed previously the cached data will be
  128. * returned. To force a connection use refresh().
  129. *
  130. * @return void
  131. * @throws Phlickr_Exception, Phlickr_ConnectionException,
  132. * Phlickr_XmlParseException
  133. * @see refresh(), requestXml()
  134. */
  135. public function load() {
  136. // allow cached results
  137. $this->_cachedXml = $this->requestXml(true);
  138. }
  139. /**
  140. * Connect to Flickr and get the current, complete information on this
  141. * object.
  142. *
  143. * This function always connect to the Flickr service. To allow cashed
  144. * results use load().
  145. *
  146. * @return void
  147. * @throws Phlickr_Exception, Phlickr_ConnectionException,
  148. * Phlickr_XmlParseException
  149. * @see load(), requestXml()
  150. */
  151. public function refresh() {
  152. // force a non-cached update
  153. $this->_cachedXml = $this->requestXml(false);
  154. }
  155. /**
  156. * Return the total number of items in the list.
  157. *
  158. * @return integer
  159. */
  160. public function getCount() {
  161. try {
  162. return count($this->getIds());
  163. }
  164. catch (Phlickr_Exception $ex) {
  165. return 0;
  166. }
  167. }
  168. /**
  169. * Return an array of the integer ids in this list.
  170. *
  171. * If the list uses another datatype for the ids this method will need to be
  172. * overridden.
  173. *
  174. * @return array
  175. */
  176. public function getIds() {
  177. if (!isset($this->_cachedXml->{$this->getResponseElement()})) {
  178. $this->load();
  179. }
  180. $ret = array();
  181. foreach ($this->_cachedXml->{$this->getResponseElement()} as $xml) {
  182. $ret[] = (string) $xml['id'];
  183. }
  184. return $ret;
  185. }
  186. }