/decorators/ProductAPIParser.php
PHP | 238 lines | 144 code | 14 blank | 80 comment | 22 complexity | 91037c6998df3ee2aeabed1502c9d510 MD5 | raw file
- <?php
- /**
- * @author Samigullin Kamil <feedback@kamilsk.com>
- * @link http://www.kamilsk.com/
- */
- namespace Ecwid\decorators;
- use Ecwid\interfaces\iProductAPI,
- \Exception;
- /**
- * @package Ecwid.decorators
- * @since 1.0
- */
- class ProductAPIParser extends ProductAPIDecorator
- {
- /**
- * @var array
- */
- protected static $_defaultConfig = array(
- 'assoc' => true,
- );
- /**
- * Constructor.
- *
- * @param iProductAPI $product_api
- * @param array $config
- * <code>
- * array(
- * 'assoc' => %bool,
- * )
- * </code>
- * @throws Exception
- */
- public function __construct(iProductAPI $product_api, array $config = array())
- {
- if ($this->_initConfig(array_merge(self::$_defaultConfig, $config))) {
- parent::__construct($product_api);
- } else {
- throw new Exception('_init');
- }
- }
- /**
- * @return bool
- */
- public function isAssoc()
- {
- return $this->_config['assoc'];
- }
- /**
- * @param bool $assoc
- * @return self
- * @throws Exception
- */
- public function setAssoc($assoc)
- {
- if ( ! is_bool($assoc)) {
- throw new Exception('_invalid_type');
- }
- $this->_config['assoc'] = $assoc;
- return $this;
- }
- /**
- * @param array $config
- * @return bool
- * @throws Exception
- */
- protected function _initConfig(array $config)
- {
- $map = array(
- 'assoc' => 'setAssoc',
- );
- $this->_silent = true;
- foreach ($config as $key => $value) {
- if (array_key_exists($key, $map)) {
- $set = $map[$key];
- $this->$set($value);
- }
- }
- $this->_silent = false;
- return true;
- }
- /* iProductAPI */
- /**
- * @param int|null $parent
- * @return mixed
- * @throws Exception
- */
- public function getCategories($parent = null)
- {
- $response = $this->_product_api->getCategories($parent);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getCategory($id)
- {
- $response = $this->_product_api->getCategory($id);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param int|null $category
- * @return mixed
- * @throws Exception
- */
- public function getProducts($category = null)
- {
- $response = $this->_product_api->getProducts($category);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getProduct($id)
- {
- $response = $this->_product_api->getProduct($id);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param int $count
- * @return mixed
- * @throws Exception
- */
- public function getRandomProducts($count)
- {
- $response = $this->_product_api->getRandomProducts($count);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @return mixed
- * @throws Exception
- */
- public function getClasses()
- {
- $response = $this->_product_api->getClasses();
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getClass($id)
- {
- $response = $this->_product_api->getClass($id);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @return mixed
- * @throws Exception
- */
- public function getProfile()
- {
- $response = $this->_product_api->getProfile();
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- /**
- * @param array $data
- * @return mixed
- * @throws Exception
- */
- public function runBatch(array $data)
- {
- $response = $this->_product_api->runBatch($data);
- if (is_string($response)) {
- $response = json_decode($response, $this->isAssoc());
- if (is_null($response)) {
- throw new Exception('_json', json_last_error());
- }
- }
- return $response;
- }
- }