File indexing completed on 2024-12-29 05:28:01

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage Ebay
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id: Items.php 22804 2010-08-08 05:08:05Z renanbr $
0021  */
0022 
0023 /**
0024  * @see Zend_Service_Ebay_Finding_Response_Histograms
0025  */
0026 // require_once 'Zend/Service/Ebay/Finding/Response/Histograms.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Service
0031  * @subpackage Ebay
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  * @uses       Zend_Service_Ebay_Finding_Response_Histograms
0035  */
0036 class Zend_Service_Ebay_Finding_Response_Items extends Zend_Service_Ebay_Finding_Response_Histograms
0037 {
0038     /**
0039      * @link http://developer.ebay.com/DevZone/finding/CallRef/types/PaginationInput.html
0040      */
0041     const PAGE_MAX_DEFAULT  = 100;
0042     const PAGE_MAX_INFINITY = 0;
0043 
0044     /**
0045      * Indicates the pagination of the result set.
0046      *
0047      * Child elements indicate the page number that is returned, the maximum
0048      * number of item listings to return per page, total number of pages that
0049      * can be returned, and the total number of listings that match the search
0050      * criteria.
0051      *
0052      * @var Zend_Service_Ebay_Finding_PaginationOutput
0053      */
0054     public $paginationOutput;
0055 
0056     /**
0057      * Container for the item listings that matched the search criteria.
0058      *
0059      * The data for each item is returned in individual containers, if any
0060      * matches were found.
0061      *
0062      * @var Zend_Service_Ebay_Finding_Search_Result
0063      */
0064     public $searchResult;
0065 
0066     /**
0067      * @var Zend_Service_Ebay_Finding_Response_Items[]
0068      */
0069     protected static $_pageCache = array();
0070 
0071     /**
0072      * @return void
0073      */
0074     protected function _init()
0075     {
0076         parent::_init();
0077         $ns = Zend_Service_Ebay_Finding::XMLNS_FINDING;
0078 
0079         $this->_attributes['searchResult'] = array(
0080             'count' => $this->_query(".//$ns:searchResult[1]/@count[1]", 'string')
0081         );
0082 
0083         $node = $this->_xPath->query(".//$ns:searchResult[1]", $this->_dom)->item(0);
0084         if ($node) {
0085             /**
0086              * @see Zend_Service_Ebay_Finding_Search_Result
0087              */
0088             // require_once 'Zend/Service/Ebay/Finding/Search/Result.php';
0089             $this->searchResult = new Zend_Service_Ebay_Finding_Search_Result($node);
0090         }
0091 
0092         $node = $this->_xPath->query(".//$ns:paginationOutput[1]", $this->_dom)->item(0);
0093         if ($node) {
0094             /**
0095              * @see Zend_Service_Ebay_Finding_PaginationOutput
0096              */
0097             // require_once 'Zend/Service/Ebay/Finding/PaginationOutput.php';
0098             $this->paginationOutput = new Zend_Service_Ebay_Finding_PaginationOutput($node);
0099         }
0100     }
0101 
0102     /**
0103      * @param  Zend_Service_Ebay_Finding $proxy
0104      * @param  integer                   $number
0105      * @throws Zend_Service_Ebay_Finding_Exception When $number is invalid
0106      * @return Zend_Service_Ebay_Finding_Response_Items
0107      */
0108     public function page(Zend_Service_Ebay_Finding $proxy, $number)
0109     {
0110         // check page number
0111         if ($number < 1 || $number > $this->paginationOutput->totalPages) {
0112             /**
0113              * @see Zend_Service_Ebay_Finding_Exception
0114              */
0115             // require_once 'Zend/Service/Ebay/Finding/Exception.php';
0116             throw new Zend_Service_Ebay_Finding_Exception(
0117                 "Page number '{$number}' is out of range.");
0118         }
0119 
0120         // prepare arguments
0121         $arguments = array();
0122         switch ($this->_operation) {
0123             case 'findItemsAdvanced':
0124                 $arguments[] = $this->getOption('keywords');
0125                 $arguments[] = $this->getOption('descriptionSearch');
0126                 $arguments[] = $this->getOption('categoryId');
0127                 break;
0128 
0129             case 'findItemsByCategory':
0130                 $arguments[] = $this->getOption('categoryId');
0131                 break;
0132 
0133             case 'findItemsByKeywords':
0134                 $arguments[] = $this->getOption('keywords');
0135                 break;
0136 
0137             case 'findItemsByProduct':
0138                 $productId = $this->getOption('productId');
0139                 if (!is_array($productId)) {
0140                     $productId = array('' => $productId);
0141                 }
0142                 $arguments[] = array_key_exists('', $productId)
0143                              ? $productId['']
0144                              : null;
0145                 $arguments[] = array_key_exists('type', $productId)
0146                              ? $productId['type']
0147                              : null;
0148                 break;
0149 
0150             case 'findItemsIneBayStores':
0151                 $arguments[] = $this->getOption('storeName');
0152                 break;
0153 
0154             default:
0155                 /**
0156                  * @see Zend_Service_Ebay_Finding_Exception
0157                  */
0158                 // require_once 'Zend/Service/Ebay/Finding/Exception.php';
0159                 throw new Zend_Service_Ebay_Finding_Exception(
0160                     "Invalid operation '{$this->_operation}'.");
0161         }
0162 
0163         // prepare options
0164         // remove every pagination entry from current option list
0165         $options = $this->_options;
0166         foreach (array_keys($options) as $optionName) {
0167             if (substr($optionName, 0, 15) == 'paginationInput') {
0168                 unset($options[$optionName]);
0169             }
0170         }
0171 
0172         // set new pagination values
0173         // see more at http://developer.ebay.com/DevZone/finding/CallRef/types/PaginationInput.html
0174         $entriesPerPage             = $this->paginationOutput->entriesPerPage;
0175         $options['paginationInput'] = array('entriesPerPage' => $entriesPerPage,
0176                                             'pageNumber'     => $number);
0177 
0178         // add current options as last argument
0179         ksort($options);
0180         $arguments[] = $options;
0181 
0182         // verify cache
0183         $id = serialize($arguments);
0184         if (!array_key_exists($id, self::$_pageCache)) {
0185             if ($number == $this->paginationOutput->pageNumber) {
0186                 // add itself to cache
0187                 $new = $this;
0188             } else {
0189                 // request new page
0190                 $callback = array($proxy, $this->_operation);
0191                 $new      = call_user_func_array($callback, $arguments);
0192             }
0193             self::$_pageCache[$id] = $new;
0194         }
0195 
0196         return self::$_pageCache[$id];
0197     }
0198 
0199     /**
0200      * @param  Zend_Service_Ebay_Finding $proxy
0201      * @return Zend_Service_Ebay_Finding_Response_Items
0202      */
0203     public function pageFirst(Zend_Service_Ebay_Finding $proxy)
0204     {
0205         return $this->page($proxy, 1);
0206     }
0207 
0208     /**
0209      * @param  Zend_Service_Ebay_Finding $proxy
0210      * @param  integer                   $max
0211      * @return Zend_Service_Ebay_Finding_Response_Items
0212      */
0213     public function pageLast(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
0214     {
0215         $last = $this->paginationOutput->totalPages;
0216         if ($max > 0 && $last > $max) {
0217             $last = $max;
0218         }
0219         return $this->page($proxy, $last);
0220     }
0221 
0222     /**
0223      * @param  Zend_Service_Ebay_Finding $proxy
0224      * @param  integer                   $max
0225      * @return Zend_Service_Ebay_Finding_Response_Items
0226      */
0227     public function pageNext(Zend_Service_Ebay_Finding $proxy, $max = self::PAGE_MAX_DEFAULT)
0228     {
0229         $next = $this->paginationOutput->pageNumber + 1;
0230         $last = $this->paginationOutput->totalPages;
0231         if (($max > 0 && $next > $max) || $next > $last) {
0232             return null;
0233         }
0234         return $this->page($proxy, $next);
0235     }
0236 
0237     /**
0238      * @param  Zend_Service_Ebay_Finding $proxy
0239      * @return Zend_Service_Ebay_Finding_Response_Items
0240      */
0241     public function pagePrevious(Zend_Service_Ebay_Finding $proxy)
0242     {
0243         $previous = $this->paginationOutput->pageNumber - 1;
0244         if ($previous < 1) {
0245             return null;
0246         }
0247         return $this->page($proxy, $previous);
0248     }
0249 }