Items.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Ebay
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Items.php 22804 2010-08-08 05:08:05Z renanbr $
  21. */
  22. /**
  23. * @see Zend_Service_Ebay_Finding_Response_Histograms
  24. */
  25. require_once 'Zend/Service/Ebay/Finding/Response/Histograms.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Ebay
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @uses Zend_Service_Ebay_Finding_Response_Histograms
  33. */
  34. class Zend_Service_Ebay_Finding_Response_Items extends Zend_Service_Ebay_Finding_Response_Histograms
  35. {
  36. /**
  37. * @link http://developer.ebay.com/DevZone/finding/CallRef/types/PaginationInput.html
  38. */
  39. const PAGE_MAX_DEFAULT = 100;
  40. const PAGE_MAX_INFINITY = 0;
  41. /**
  42. * Indicates the pagination of the result set.
  43. *
  44. * Child elements indicate the page number that is returned, the maximum
  45. * number of item listings to return per page, total number of pages that
  46. * can be returned, and the total number of listings that match the search
  47. * criteria.
  48. *
  49. * @var Zend_Service_Ebay_Finding_PaginationOutput
  50. */
  51. public $paginationOutput;
  52. /**
  53. * Container for the item listings that matched the search criteria.
  54. *
  55. * The data for each item is returned in individual containers, if any
  56. * matches were found.
  57. *
  58. * @var Zend_Service_Ebay_Finding_Search_Result
  59. */
  60. public $searchResult;
  61. /**
  62. * The total number of items returned in the search response.
  63. *
  64. * This is often equal to the entriesPerPage value. If the count is less
  65. * than the specified entriesPerPage, it indicates the end of the result
  66. * set.
  67. *
  68. * @var integer
  69. */
  70. public $searchResult_count;
  71. /**
  72. * @var Zend_Service_Ebay_Finding_Response_Items[]
  73. */
  74. protected static $_pageCache = array();
  75. /**
  76. * @return void
  77. */
  78. protected function _init()
  79. {
  80. parent::_init();
  81. $ns = Zend_Service_Ebay_Finding::XMLNS_FINDING;
  82. $this->searchResult_count = $this->_query(".//$ns:searchResult[1]/@count[1]", 'string');
  83. $node = $this->_xPath->query(".//$ns:searchResult[1]", $this->_dom)->item(0);
  84. if ($node) {
  85. /**
  86. * @see Zend_Service_Ebay_Finding_Search_Result
  87. */
  88. require_once 'Zend/Service/Ebay/Finding/Search/Result.php';
  89. $this->searchResult = new Zend_Service_Ebay_Finding_Search_Result($node);
  90. }
  91. $node = $this->_xPath->query(".//$ns:paginationOutput[1]", $this->_dom)->item(0);
  92. if ($node) {
  93. /**
  94. * @see Zend_Service_Ebay_Finding_PaginationOutput
  95. */
  96. require_once 'Zend/Service/Ebay/Finding/PaginationOutput.php';
  97. $this->paginationOutput = new Zend_Service_Ebay_Finding_PaginationOutput($node);
  98. }
  99. }
  100. /**
  101. * @param Zend_Service_Ebay_Finding $proxy
  102. * @param integer $number
  103. * @throws Zend_Service_Ebay_Finding_Exception When $number is invalid
  104. * @return Zend_Service_Ebay_Finding_Response_Items
  105. */
  106. public function page(Zend_Service_Ebay_Finding $proxy, $number)
  107. {
  108. // check page number
  109. if ($number < 1 || $number > $this->paginationOutput->totalPages) {
  110. /**
  111. * @see Zend_Service_Ebay_Finding_Exception
  112. */
  113. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  114. throw new Zend_Service_Ebay_Finding_Exception(
  115. "Page number '{$number}' is out of range.");
  116. }
  117. // prepare arguments
  118. $arguments = array();
  119. switch ($this->_operation) {
  120. case 'findItemsAdvanced':
  121. $arguments[] = $this->getOption('keywords');
  122. $arguments[] = $this->getOption('descriptionSearch');
  123. $arguments[] = $this->getOption('categoryId');
  124. break;
  125. case 'findItemsByCategory':
  126. $arguments[] = $this->getOption('categoryId');
  127. break;
  128. case 'findItemsByKeywords':
  129. $arguments[] = $this->getOption('keywords');
  130. break;
  131. case 'findItemsByProduct':
  132. $productId = $this->getOption('productId');
  133. if (!is_array($productId)) {
  134. $productId = array('' => $productId);
  135. }
  136. $arguments[] = array_key_exists('', $productId)
  137. ? $productId['']
  138. : null;
  139. $arguments[] = array_key_exists('type', $productId)
  140. ? $productId['type']
  141. : null;
  142. break;
  143. case 'findItemsIneBayStores':
  144. $arguments[] = $this->getOption('storeName');
  145. break;
  146. default:
  147. /**
  148. * @see Zend_Service_Ebay_Finding_Exception
  149. */
  150. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  151. throw new Zend_Service_Ebay_Finding_Exception(
  152. "Invalid operation '{$this->_operation}'.");
  153. }
  154. // prepare options
  155. // remove every pagination entry from current option list
  156. $options = $this->_options;
  157. foreach (array_keys($options) as $optionName) {
  158. if (substr($optionName, 0, 15) == 'paginationInput') {
  159. unset($options[$optionName]);
  160. }
  161. }
  162. // set new pagination values
  163. // see more at http://developer.ebay.com/DevZone/finding/CallRef/types/PaginationInput.html
  164. $entriesPerPage = $this->paginationOutput->entriesPerPage;
  165. $options['paginationInput'] = array('entriesPerPage' => $entriesPerPage,
  166. 'pageNumber' => $number);
  167. // add current options as last argument
  168. ksort($options);
  169. $arguments[] = $options;
  170. // verify cache
  171. $id = serialize($arguments);
  172. if (!array_key_exists($id, self::$_pageCache)) {
  173. if ($number == $this->paginationOutput->pageNumber) {
  174. // add itself to cache
  175. $new = $this;
  176. } else {
  177. // request new page
  178. $callback = array($proxy, $this->_operation);
  179. $new = call_user_func_array($callback, $arguments);
  180. }
  181. self::$_pageCache[$id] = $new;
  182. }
  183. return self::$_pageCache[$id];
  184. }
  185. /**
  186. * @param Zend_Service_Ebay_Finding $proxy
  187. * @return Zend_Service_Ebay_Finding_Response_Items
  188. */
  189. public function pageFirst(Zend_Service_Ebay_Finding $proxy)
  190. {
  191. return $this->page($proxy, 1);
  192. }
  193. /**
  194. * @param Zend_Service_Ebay_Finding $proxy
  195. * @param integer $max
  196. * @return Zend_Service_Ebay_Finding_Response_Items
  197. */
  198. public function pageLast(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
  199. {
  200. $last = $this->paginationOutput->totalPages;
  201. if ($max > 0 && $last > $max) {
  202. $last = $max;
  203. }
  204. return $this->page($proxy, $last);
  205. }
  206. /**
  207. * @param Zend_Service_Ebay_Finding $proxy
  208. * @param integer $max
  209. * @return Zend_Service_Ebay_Finding_Response_Items
  210. */
  211. public function pageNext(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
  212. {
  213. $next = $this->paginationOutput->pageNumber + 1;
  214. $last = $this->paginationOutput->totalPages;
  215. if (($max > 0 && $next > $max) || $next > $last) {
  216. return null;
  217. }
  218. return $this->page($proxy, $next);
  219. }
  220. /**
  221. * @param Zend_Service_Ebay_Finding $proxy
  222. * @return Zend_Service_Ebay_Finding_Response_Items
  223. */
  224. public function pagePrevious(Zend_Service_Ebay_Finding $proxy)
  225. {
  226. $previous = $this->paginationOutput->pageNumber - 1;
  227. if ($previous < 1) {
  228. return null;
  229. }
  230. return $this->page($proxy, $previous);
  231. }
  232. }