PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Ebay/Abstract.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 309 lines | 124 code | 19 blank | 166 comment | 25 complexity | ebd3708f81ee6ae978e00cb8aaad077e MD5 | raw file
  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: Abstract.php 22824 2010-08-09 18:59:54Z renanbr $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Ebay
  26. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_Service_Ebay_Abstract
  30. {
  31. const OPTION_APP_ID = 'app_id';
  32. const OPTION_GLOBAL_ID = 'global_id';
  33. /**
  34. * @var array
  35. */
  36. protected $_options = array();
  37. /**
  38. * @var mixed
  39. */
  40. protected $_client;
  41. /**
  42. * @param Zend_Config|array $options
  43. * @return void
  44. */
  45. public function __construct($options = null)
  46. {
  47. $options = self::optionsToArray($options);
  48. $this->setOption($options);
  49. }
  50. /**
  51. * @param string|Zend_Config|array $name
  52. * @param mixed $value
  53. * @return Zend_Service_Ebay_Abstract Provides a fluent interface
  54. */
  55. public function setOption($name, $value = null)
  56. {
  57. if ($name instanceof Zend_Config) {
  58. $name = $name->toArray();
  59. }
  60. if (is_array($name)) {
  61. $this->_options = $name + $this->_options;
  62. } else {
  63. $this->_options[$name] = $value;
  64. }
  65. return $this;
  66. }
  67. /**
  68. * @param string $name
  69. * @return mixed
  70. */
  71. public function getOption($name = null)
  72. {
  73. if (null === $name) {
  74. return $this->_options;
  75. }
  76. if ($this->hasOption($name)) {
  77. return $this->_options[$name];
  78. }
  79. return null;
  80. }
  81. /**
  82. * @param string $name
  83. * @return boolean
  84. */
  85. public function hasOption($name)
  86. {
  87. return array_key_exists($name, $this->_options);
  88. }
  89. /**
  90. * @param mixed $client
  91. * @return Zend_Service_Ebay_Abstract Provides a fluent interface
  92. */
  93. abstract public function setClient($client);
  94. /**
  95. * @return mixed
  96. */
  97. abstract public function getClient();
  98. /**
  99. * @param Zend_Config|array $options
  100. * @throws Zend_Service_Ebay_Finding_Exception When $options is not an array neither a Zend_Config object
  101. * @return array
  102. */
  103. public static function optionsToArray($options)
  104. {
  105. if (null === $options) {
  106. $options = array();
  107. } else if ($options instanceof Zend_Config) {
  108. $options = $options->toArray();
  109. }
  110. if (!is_array($options)) {
  111. /**
  112. * @see Zend_Service_Ebay_Exception
  113. */
  114. require_once 'Zend/Service/Ebay/Exception.php';
  115. throw new Zend_Service_Ebay_Exception('Invalid options provided.');
  116. }
  117. return $options;
  118. }
  119. /**
  120. * Implements Name-value Syntax translator.
  121. *
  122. * Example:
  123. *
  124. * array(
  125. * 'paginationInput' => array(
  126. * 'entriesPerPage' => 5,
  127. * 'pageNumber' => 2
  128. * ),
  129. * 'itemFilter' => array(
  130. * array(
  131. * 'name' => 'MaxPrice',
  132. * 'value' => 25,
  133. * 'paramName' => 'Currency',
  134. * 'paramValue' => 'USD'
  135. * ),
  136. * array(
  137. * 'name' => 'FreeShippingOnly',
  138. * 'value' => true
  139. * ),
  140. * array(
  141. * 'name' => 'ListingType',
  142. * 'value' => array(
  143. * 'AuctionWithBIN',
  144. * 'FixedPrice',
  145. * 'StoreInventory'
  146. * )
  147. * )
  148. * ),
  149. * 'productId' => array(
  150. * '' => 123,
  151. * 'type' => 'UPC'
  152. * )
  153. * )
  154. *
  155. * this above is translated to
  156. *
  157. * array(
  158. * 'paginationInput.entriesPerPage' => '5',
  159. * 'paginationInput.pageNumber' => '2',
  160. * 'itemFilter(0).name' => 'MaxPrice',
  161. * 'itemFilter(0).value' => '25',
  162. * 'itemFilter(0).paramName' => 'Currency',
  163. * 'itemFilter(0).paramValue' => 'USD',
  164. * 'itemFilter(1).name' => 'FreeShippingOnly',
  165. * 'itemFilter(1).value' => '1',
  166. * 'itemFilter(2).name' => 'ListingType',
  167. * 'itemFilter(2).value(0)' => 'AuctionWithBIN',
  168. * 'itemFilter(2).value(1)' => 'FixedPrice',
  169. * 'itemFilter(2).value(2)' => 'StoreInventory',
  170. * 'productId' => '123',
  171. * 'productId.@type' => 'UPC'
  172. * )
  173. *
  174. * @param Zend_Config|array $options
  175. * @link http://developer.ebay.com/DevZone/finding/Concepts/MakingACall.html#nvsyntax
  176. * @return array A simple array of strings
  177. */
  178. protected function _optionsToNameValueSyntax($options)
  179. {
  180. $options = self::optionsToArray($options);
  181. ksort($options);
  182. $new = array();
  183. $runAgain = false;
  184. foreach ($options as $name => $value) {
  185. if (is_array($value)) {
  186. // parse an array value, check if it is associative
  187. $keyRaw = array_keys($value);
  188. $keyNumber = range(0, count($value) - 1);
  189. $isAssoc = count(array_diff($keyRaw, $keyNumber)) > 0;
  190. // check for tag representation, like <name att="sometinhg"></value>
  191. // empty key refers to text value
  192. // when there is a root tag, attributes receive flags
  193. $hasAttribute = array_key_exists('', $value);
  194. foreach ($value as $subName => $subValue) {
  195. // generate new key name
  196. if ($isAssoc) {
  197. // named keys
  198. $newName = $name;
  199. if ($subName !== '') {
  200. // when $subName is empty means that current value
  201. // is the main value for the main key
  202. $glue = $hasAttribute ? '.@' : '.';
  203. $newName .= $glue . $subName;
  204. }
  205. } else {
  206. // numeric keys
  207. $newName = $name . '(' . $subName . ')';
  208. }
  209. // save value
  210. if (is_array($subValue)) {
  211. // it is necessary run this again, value is an array
  212. $runAgain = true;
  213. } else {
  214. // parse basic type
  215. $subValue = self::toEbayValue($subValue);
  216. }
  217. $new[$newName] = $subValue;
  218. }
  219. } else {
  220. // parse basic type
  221. $new[$name] = self::toEbayValue($value);
  222. }
  223. }
  224. if ($runAgain) {
  225. // this happens if any $subValue found is an array
  226. $new = $this->_optionsToNameValueSyntax($new);
  227. }
  228. return $new;
  229. }
  230. /**
  231. * Translate native PHP values format to ebay format for request.
  232. *
  233. * Boolean is translated to "0" or "1", date object generates ISO 8601,
  234. * everything else is translated to string.
  235. *
  236. * @param mixed $value
  237. * @return string
  238. */
  239. public static function toEbayValue($value)
  240. {
  241. if (is_bool($value)) {
  242. $value = $value ? '1' : '0';
  243. } else if ($value instanceof Zend_Date) {
  244. $value = $value->getIso();
  245. } else if ($value instanceof DateTime) {
  246. $value = $value->format(DateTime::ISO8601);
  247. } else {
  248. $value = (string) $value;
  249. }
  250. return $value;
  251. }
  252. /**
  253. * Translate an ebay value format to native PHP type.
  254. *
  255. * @param string $value
  256. * @param string $type
  257. * @see http://developer.ebay.com/DevZone/finding/CallRef/types/simpleTypes.html
  258. * @throws Zend_Service_Ebay_Finding_Exception When $type is not valid
  259. * @return mixed
  260. */
  261. public static function toPhpValue($value, $type)
  262. {
  263. switch ($type) {
  264. // cast for: boolean
  265. case 'boolean':
  266. $value = (string) $value == 'true';
  267. break;
  268. // cast for: Amount, decimal, double, float, MeasureType
  269. case 'float':
  270. $value = floatval((string) $value);
  271. break;
  272. // cast for: int, long
  273. // integer type generates a string value, because 32 bit systems
  274. // have an integer range of -2147483648 to 2147483647
  275. case 'integer':
  276. // break intentionally omitted
  277. // cast for: anyURI, base64Binary, dateTime, duration, string, token
  278. case 'string':
  279. $value = (string) $value;
  280. break;
  281. default:
  282. /**
  283. * @see Zend_Service_Ebay_Exception
  284. */
  285. require_once 'Zend/Service/Ebay/Exception.php';
  286. throw new Zend_Service_Ebay_Exception("Invalid type '{$type}'.");
  287. }
  288. return $value;
  289. }
  290. }