PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Service/Ebay/Finding/Response/Items.php

http://github.com/michael-romer/zf-boilerplate
PHP | 249 lines | 120 code | 25 blank | 104 comment | 18 complexity | 8ca2faefec0632c9cd8959b928cb8122 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0
  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-2011 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-2011 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. * @var Zend_Service_Ebay_Finding_Response_Items[]
  63. */
  64. protected static $_pageCache = array();
  65. /**
  66. * @return void
  67. */
  68. protected function _init()
  69. {
  70. parent::_init();
  71. $ns = Zend_Service_Ebay_Finding::XMLNS_FINDING;
  72. $this->_attributes['searchResult'] = array(
  73. 'count' => $this->_query(".//$ns:searchResult[1]/@count[1]", 'string')
  74. );
  75. $node = $this->_xPath->query(".//$ns:searchResult[1]", $this->_dom)->item(0);
  76. if ($node) {
  77. /**
  78. * @see Zend_Service_Ebay_Finding_Search_Result
  79. */
  80. require_once 'Zend/Service/Ebay/Finding/Search/Result.php';
  81. $this->searchResult = new Zend_Service_Ebay_Finding_Search_Result($node);
  82. }
  83. $node = $this->_xPath->query(".//$ns:paginationOutput[1]", $this->_dom)->item(0);
  84. if ($node) {
  85. /**
  86. * @see Zend_Service_Ebay_Finding_PaginationOutput
  87. */
  88. require_once 'Zend/Service/Ebay/Finding/PaginationOutput.php';
  89. $this->paginationOutput = new Zend_Service_Ebay_Finding_PaginationOutput($node);
  90. }
  91. }
  92. /**
  93. * @param Zend_Service_Ebay_Finding $proxy
  94. * @param integer $number
  95. * @throws Zend_Service_Ebay_Finding_Exception When $number is invalid
  96. * @return Zend_Service_Ebay_Finding_Response_Items
  97. */
  98. public function page(Zend_Service_Ebay_Finding $proxy, $number)
  99. {
  100. // check page number
  101. if ($number < 1 || $number > $this->paginationOutput->totalPages) {
  102. /**
  103. * @see Zend_Service_Ebay_Finding_Exception
  104. */
  105. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  106. throw new Zend_Service_Ebay_Finding_Exception(
  107. "Page number '{$number}' is out of range.");
  108. }
  109. // prepare arguments
  110. $arguments = array();
  111. switch ($this->_operation) {
  112. case 'findItemsAdvanced':
  113. $arguments[] = $this->getOption('keywords');
  114. $arguments[] = $this->getOption('descriptionSearch');
  115. $arguments[] = $this->getOption('categoryId');
  116. break;
  117. case 'findItemsByCategory':
  118. $arguments[] = $this->getOption('categoryId');
  119. break;
  120. case 'findItemsByKeywords':
  121. $arguments[] = $this->getOption('keywords');
  122. break;
  123. case 'findItemsByProduct':
  124. $productId = $this->getOption('productId');
  125. if (!is_array($productId)) {
  126. $productId = array('' => $productId);
  127. }
  128. $arguments[] = array_key_exists('', $productId)
  129. ? $productId['']
  130. : null;
  131. $arguments[] = array_key_exists('type', $productId)
  132. ? $productId['type']
  133. : null;
  134. break;
  135. case 'findItemsIneBayStores':
  136. $arguments[] = $this->getOption('storeName');
  137. break;
  138. default:
  139. /**
  140. * @see Zend_Service_Ebay_Finding_Exception
  141. */
  142. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  143. throw new Zend_Service_Ebay_Finding_Exception(
  144. "Invalid operation '{$this->_operation}'.");
  145. }
  146. // prepare options
  147. // remove every pagination entry from current option list
  148. $options = $this->_options;
  149. foreach (array_keys($options) as $optionName) {
  150. if (substr($optionName, 0, 15) == 'paginationInput') {
  151. unset($options[$optionName]);
  152. }
  153. }
  154. // set new pagination values
  155. // see more at http://developer.ebay.com/DevZone/finding/CallRef/types/PaginationInput.html
  156. $entriesPerPage = $this->paginationOutput->entriesPerPage;
  157. $options['paginationInput'] = array('entriesPerPage' => $entriesPerPage,
  158. 'pageNumber' => $number);
  159. // add current options as last argument
  160. ksort($options);
  161. $arguments[] = $options;
  162. // verify cache
  163. $id = serialize($arguments);
  164. if (!array_key_exists($id, self::$_pageCache)) {
  165. if ($number == $this->paginationOutput->pageNumber) {
  166. // add itself to cache
  167. $new = $this;
  168. } else {
  169. // request new page
  170. $callback = array($proxy, $this->_operation);
  171. $new = call_user_func_array($callback, $arguments);
  172. }
  173. self::$_pageCache[$id] = $new;
  174. }
  175. return self::$_pageCache[$id];
  176. }
  177. /**
  178. * @param Zend_Service_Ebay_Finding $proxy
  179. * @return Zend_Service_Ebay_Finding_Response_Items
  180. */
  181. public function pageFirst(Zend_Service_Ebay_Finding $proxy)
  182. {
  183. return $this->page($proxy, 1);
  184. }
  185. /**
  186. * @param Zend_Service_Ebay_Finding $proxy
  187. * @param integer $max
  188. * @return Zend_Service_Ebay_Finding_Response_Items
  189. */
  190. public function pageLast(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
  191. {
  192. $last = $this->paginationOutput->totalPages;
  193. if ($max > 0 && $last > $max) {
  194. $last = $max;
  195. }
  196. return $this->page($proxy, $last);
  197. }
  198. /**
  199. * @param Zend_Service_Ebay_Finding $proxy
  200. * @param integer $max
  201. * @return Zend_Service_Ebay_Finding_Response_Items
  202. */
  203. public function pageNext(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
  204. {
  205. $next = $this->paginationOutput->pageNumber + 1;
  206. $last = $this->paginationOutput->totalPages;
  207. if (($max > 0 && $next > $max) || $next > $last) {
  208. return null;
  209. }
  210. return $this->page($proxy, $next);
  211. }
  212. /**
  213. * @param Zend_Service_Ebay_Finding $proxy
  214. * @return Zend_Service_Ebay_Finding_Response_Items
  215. */
  216. public function pagePrevious(Zend_Service_Ebay_Finding $proxy)
  217. {
  218. $previous = $this->paginationOutput->pageNumber - 1;
  219. if ($previous < 1) {
  220. return null;
  221. }
  222. return $this->page($proxy, $previous);
  223. }
  224. }