PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/decorators/ProductAPIParser.php

https://github.com/octolab/Ecwid
PHP | 238 lines | 144 code | 14 blank | 80 comment | 22 complexity | 91037c6998df3ee2aeabed1502c9d510 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Samigullin Kamil <feedback@kamilsk.com>
  4. * @link http://www.kamilsk.com/
  5. */
  6. namespace Ecwid\decorators;
  7. use Ecwid\interfaces\iProductAPI,
  8. \Exception;
  9. /**
  10. * @package Ecwid.decorators
  11. * @since 1.0
  12. */
  13. class ProductAPIParser extends ProductAPIDecorator
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected static $_defaultConfig = array(
  19. 'assoc' => true,
  20. );
  21. /**
  22. * Constructor.
  23. *
  24. * @param iProductAPI $product_api
  25. * @param array $config
  26. * <code>
  27. * array(
  28. * 'assoc' => %bool,
  29. * )
  30. * </code>
  31. * @throws Exception
  32. */
  33. public function __construct(iProductAPI $product_api, array $config = array())
  34. {
  35. if ($this->_initConfig(array_merge(self::$_defaultConfig, $config))) {
  36. parent::__construct($product_api);
  37. } else {
  38. throw new Exception('_init');
  39. }
  40. }
  41. /**
  42. * @return bool
  43. */
  44. public function isAssoc()
  45. {
  46. return $this->_config['assoc'];
  47. }
  48. /**
  49. * @param bool $assoc
  50. * @return self
  51. * @throws Exception
  52. */
  53. public function setAssoc($assoc)
  54. {
  55. if ( ! is_bool($assoc)) {
  56. throw new Exception('_invalid_type');
  57. }
  58. $this->_config['assoc'] = $assoc;
  59. return $this;
  60. }
  61. /**
  62. * @param array $config
  63. * @return bool
  64. * @throws Exception
  65. */
  66. protected function _initConfig(array $config)
  67. {
  68. $map = array(
  69. 'assoc' => 'setAssoc',
  70. );
  71. $this->_silent = true;
  72. foreach ($config as $key => $value) {
  73. if (array_key_exists($key, $map)) {
  74. $set = $map[$key];
  75. $this->$set($value);
  76. }
  77. }
  78. $this->_silent = false;
  79. return true;
  80. }
  81. /* iProductAPI */
  82. /**
  83. * @param int|null $parent
  84. * @return mixed
  85. * @throws Exception
  86. */
  87. public function getCategories($parent = null)
  88. {
  89. $response = $this->_product_api->getCategories($parent);
  90. if (is_string($response)) {
  91. $response = json_decode($response, $this->isAssoc());
  92. if (is_null($response)) {
  93. throw new Exception('_json', json_last_error());
  94. }
  95. }
  96. return $response;
  97. }
  98. /**
  99. * @param int $id
  100. * @return mixed
  101. * @throws Exception
  102. */
  103. public function getCategory($id)
  104. {
  105. $response = $this->_product_api->getCategory($id);
  106. if (is_string($response)) {
  107. $response = json_decode($response, $this->isAssoc());
  108. if (is_null($response)) {
  109. throw new Exception('_json', json_last_error());
  110. }
  111. }
  112. return $response;
  113. }
  114. /**
  115. * @param int|null $category
  116. * @return mixed
  117. * @throws Exception
  118. */
  119. public function getProducts($category = null)
  120. {
  121. $response = $this->_product_api->getProducts($category);
  122. if (is_string($response)) {
  123. $response = json_decode($response, $this->isAssoc());
  124. if (is_null($response)) {
  125. throw new Exception('_json', json_last_error());
  126. }
  127. }
  128. return $response;
  129. }
  130. /**
  131. * @param int $id
  132. * @return mixed
  133. * @throws Exception
  134. */
  135. public function getProduct($id)
  136. {
  137. $response = $this->_product_api->getProduct($id);
  138. if (is_string($response)) {
  139. $response = json_decode($response, $this->isAssoc());
  140. if (is_null($response)) {
  141. throw new Exception('_json', json_last_error());
  142. }
  143. }
  144. return $response;
  145. }
  146. /**
  147. * @param int $count
  148. * @return mixed
  149. * @throws Exception
  150. */
  151. public function getRandomProducts($count)
  152. {
  153. $response = $this->_product_api->getRandomProducts($count);
  154. if (is_string($response)) {
  155. $response = json_decode($response, $this->isAssoc());
  156. if (is_null($response)) {
  157. throw new Exception('_json', json_last_error());
  158. }
  159. }
  160. return $response;
  161. }
  162. /**
  163. * @return mixed
  164. * @throws Exception
  165. */
  166. public function getClasses()
  167. {
  168. $response = $this->_product_api->getClasses();
  169. if (is_string($response)) {
  170. $response = json_decode($response, $this->isAssoc());
  171. if (is_null($response)) {
  172. throw new Exception('_json', json_last_error());
  173. }
  174. }
  175. return $response;
  176. }
  177. /**
  178. * @param int $id
  179. * @return mixed
  180. * @throws Exception
  181. */
  182. public function getClass($id)
  183. {
  184. $response = $this->_product_api->getClass($id);
  185. if (is_string($response)) {
  186. $response = json_decode($response, $this->isAssoc());
  187. if (is_null($response)) {
  188. throw new Exception('_json', json_last_error());
  189. }
  190. }
  191. return $response;
  192. }
  193. /**
  194. * @return mixed
  195. * @throws Exception
  196. */
  197. public function getProfile()
  198. {
  199. $response = $this->_product_api->getProfile();
  200. if (is_string($response)) {
  201. $response = json_decode($response, $this->isAssoc());
  202. if (is_null($response)) {
  203. throw new Exception('_json', json_last_error());
  204. }
  205. }
  206. return $response;
  207. }
  208. /**
  209. * @param array $data
  210. * @return mixed
  211. * @throws Exception
  212. */
  213. public function runBatch(array $data)
  214. {
  215. $response = $this->_product_api->runBatch($data);
  216. if (is_string($response)) {
  217. $response = json_decode($response, $this->isAssoc());
  218. if (is_null($response)) {
  219. throw new Exception('_json', json_last_error());
  220. }
  221. }
  222. return $response;
  223. }
  224. }