File indexing completed on 2024-12-22 05:37:02

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: Abstract.php 22824 2010-08-09 18:59:54Z renanbr $
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Service
0026  * @subpackage Ebay
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 abstract class Zend_Service_Ebay_Abstract
0031 {
0032     const OPTION_APP_ID    = 'app_id';
0033     const OPTION_GLOBAL_ID = 'global_id';
0034 
0035     /**
0036      * @var array
0037      */
0038     protected $_options = array();
0039 
0040     /**
0041      * @var mixed
0042      */
0043     protected $_client;
0044 
0045     /**
0046      * @param  Zend_Config|array $options
0047      * @return void
0048      */
0049     public function __construct($options = null)
0050     {
0051         $options = self::optionsToArray($options);
0052         $this->setOption($options);
0053     }
0054 
0055     /**
0056      * @param  string|Zend_Config|array $name
0057      * @param  mixed                    $value
0058      * @return Zend_Service_Ebay_Abstract Provides a fluent interface
0059      */
0060     public function setOption($name, $value = null)
0061     {
0062         if ($name instanceof Zend_Config) {
0063             $name = $name->toArray();
0064         }
0065         if (is_array($name)) {
0066             $this->_options = $name + $this->_options;
0067         } else {
0068             $this->_options[$name] = $value;
0069         }
0070         return $this;
0071     }
0072 
0073     /**
0074      * @param  string $name
0075      * @return mixed
0076      */
0077     public function getOption($name = null)
0078     {
0079         if (null === $name) {
0080             return $this->_options;
0081         }
0082         if ($this->hasOption($name)) {
0083             return $this->_options[$name];
0084         }
0085         return null;
0086     }
0087 
0088     /**
0089      * @param string $name
0090      * @return boolean
0091      */
0092     public function hasOption($name)
0093     {
0094         return array_key_exists($name, $this->_options);
0095     }
0096 
0097     /**
0098      * @param  mixed $client
0099      * @return Zend_Service_Ebay_Abstract Provides a fluent interface
0100      */
0101     abstract public function setClient($client);
0102 
0103     /**
0104      * @return mixed
0105      */
0106     abstract public function getClient();
0107 
0108     /**
0109      * @param  Zend_Config|array $options
0110      * @throws Zend_Service_Ebay_Finding_Exception When $options is not an array neither a Zend_Config object
0111      * @return array
0112      */
0113     public static function optionsToArray($options)
0114     {
0115         if (null === $options) {
0116             $options = array();
0117         } else if ($options instanceof Zend_Config) {
0118             $options = $options->toArray();
0119         }
0120 
0121         if (!is_array($options)) {
0122             /**
0123              * @see Zend_Service_Ebay_Exception
0124              */
0125             // require_once 'Zend/Service/Ebay/Exception.php';
0126             throw new Zend_Service_Ebay_Exception('Invalid options provided.');
0127         }
0128 
0129         return $options;
0130     }
0131 
0132     /**
0133      * Implements Name-value Syntax translator.
0134      *
0135      * Example:
0136      *
0137      * array(
0138      *     'paginationInput' => array(
0139      *         'entriesPerPage' => 5,
0140      *         'pageNumber'     => 2
0141      *     ),
0142      *     'itemFilter' => array(
0143      *         array(
0144      *             'name'       => 'MaxPrice',
0145      *             'value'      => 25,
0146      *             'paramName'  => 'Currency',
0147      *             'paramValue' => 'USD'
0148      *         ),
0149      *         array(
0150      *             'name'  => 'FreeShippingOnly',
0151      *             'value' => true
0152      *         ),
0153      *         array(
0154      *             'name'  => 'ListingType',
0155      *             'value' => array(
0156      *                 'AuctionWithBIN',
0157      *                 'FixedPrice',
0158      *                 'StoreInventory'
0159      *             )
0160      *         )
0161      *     ),
0162      *     'productId' => array(
0163      *         ''     => 123,
0164      *         'type' => 'UPC'
0165      *     )
0166      * )
0167      *
0168      * this above is translated to
0169      *
0170      * array(
0171      *     'paginationInput.entriesPerPage' => '5',
0172      *     'paginationInput.pageNumber'     => '2',
0173      *     'itemFilter(0).name'             => 'MaxPrice',
0174      *     'itemFilter(0).value'            => '25',
0175      *     'itemFilter(0).paramName'        => 'Currency',
0176      *     'itemFilter(0).paramValue'       => 'USD',
0177      *     'itemFilter(1).name'             => 'FreeShippingOnly',
0178      *     'itemFilter(1).value'            => '1',
0179      *     'itemFilter(2).name'             => 'ListingType',
0180      *     'itemFilter(2).value(0)'         => 'AuctionWithBIN',
0181      *     'itemFilter(2).value(1)'         => 'FixedPrice',
0182      *     'itemFilter(2).value(2)'         => 'StoreInventory',
0183      *     'productId'                      => '123',
0184      *     'productId.@type'                => 'UPC'
0185      * )
0186      *
0187      * @param  Zend_Config|array $options
0188      * @link   http://developer.ebay.com/DevZone/finding/Concepts/MakingACall.html#nvsyntax
0189      * @return array A simple array of strings
0190      */
0191     protected function _optionsToNameValueSyntax($options)
0192     {
0193         $options  = self::optionsToArray($options);
0194         ksort($options);
0195         $new      = array();
0196         $runAgain = false;
0197         foreach ($options as $name => $value) {
0198             if (is_array($value)) {
0199                 // parse an array value, check if it is associative
0200                 $keyRaw    = array_keys($value);
0201                 $keyNumber = range(0, count($value) - 1);
0202                 $isAssoc   = count(array_diff($keyRaw, $keyNumber)) > 0;
0203                 // check for tag representation, like <name att="sometinhg"></value>
0204                 // empty key refers to text value
0205                 // when there is a root tag, attributes receive flags
0206                 $hasAttribute = array_key_exists('', $value);
0207                 foreach ($value as $subName => $subValue) {
0208                     // generate new key name
0209                     if ($isAssoc) {
0210                         // named keys
0211                         $newName = $name;
0212                         if ($subName !== '') {
0213                             // when $subName is empty means that current value
0214                             // is the main value for the main key
0215                             $glue     = $hasAttribute ? '.@' : '.';
0216                             $newName .= $glue . $subName;
0217                         }
0218                     } else {
0219                         // numeric keys
0220                         $newName = $name . '(' . $subName . ')';
0221                     }
0222                     // save value
0223                     if (is_array($subValue)) {
0224                         // it is necessary run this again, value is an array
0225                         $runAgain = true;
0226                     } else {
0227                         // parse basic type
0228                         $subValue = self::toEbayValue($subValue);
0229                     }
0230                     $new[$newName] = $subValue;
0231                 }
0232             } else {
0233                 // parse basic type
0234                 $new[$name] = self::toEbayValue($value);
0235             }
0236         }
0237         if ($runAgain) {
0238             // this happens if any $subValue found is an array
0239             $new = $this->_optionsToNameValueSyntax($new);
0240         }
0241         return $new;
0242     }
0243 
0244     /**
0245      * Translate native PHP values format to ebay format for request.
0246      *
0247      * Boolean is translated to "0" or "1", date object generates ISO 8601,
0248      * everything else is translated to string.
0249      *
0250      * @param  mixed $value
0251      * @return string
0252      */
0253     public static function toEbayValue($value)
0254     {
0255         if (is_bool($value)) {
0256             $value = $value ? '1' : '0';
0257         } else if ($value instanceof Zend_Date) {
0258             $value = $value->getIso();
0259         } else if ($value instanceof DateTime) {
0260             $value = $value->format(DateTime::ISO8601);
0261         } else {
0262             $value = (string) $value;
0263         }
0264         return $value;
0265     }
0266 
0267     /**
0268      * Translate an ebay value format to native PHP type.
0269      *
0270      * @param  string $value
0271      * @param  string $type
0272      * @see    http://developer.ebay.com/DevZone/finding/CallRef/types/simpleTypes.html
0273      * @throws Zend_Service_Ebay_Finding_Exception When $type is not valid
0274      * @return mixed
0275      */
0276     public static function toPhpValue($value, $type)
0277     {
0278         switch ($type) {
0279             // cast for: boolean
0280             case 'boolean':
0281                 $value = (string) $value == 'true';
0282                 break;
0283 
0284             // cast for: Amount, decimal, double, float, MeasureType
0285             case 'float':
0286                 $value = floatval((string) $value);
0287                 break;
0288 
0289             // cast for: int, long
0290             // integer type generates a string value, because 32 bit systems
0291             // have an integer range of -2147483648 to 2147483647
0292             case 'integer':
0293                 // break intentionally omitted
0294 
0295             // cast for: anyURI, base64Binary, dateTime, duration, string, token
0296             case 'string':
0297                 $value = (string) $value;
0298                 break;
0299 
0300             default:
0301                 /**
0302                  * @see Zend_Service_Ebay_Exception
0303                  */
0304                 // require_once 'Zend/Service/Ebay/Exception.php';
0305                 throw new Zend_Service_Ebay_Exception("Invalid type '{$type}'.");
0306         }
0307         return $value;
0308     }
0309 }