Ecwid /api/BatchBuilder.php

Language PHP Lines 208
MD5 Hash 7b6bc43d7f3162bbb5d3323dfec0b4ee
Repository https://github.com/octolab/Ecwid.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
 * @author Samigullin Kamil <feedback@kamilsk.com>
 * @link http://www.kamilsk.com/
 */
namespace Ecwid\api;
use Ecwid\interfaces\iBatchBuilder,
	Ecwid\interfaces\iProductAPI,
	\Exception;
/**
 * @package Ecwid.api
 * @since 1.0
 */
class BatchBuilder implements iBatchBuilder
{
	/**
	 * @var iProductAPI
	 */
	protected $_product_api;
	/**
	 * @var array
	 */
	protected $_queries = array();

	/**
	 * Constructor.
	 *
	 * @param iProductAPI $product_api
	 */
	public function __construct(iProductAPI $product_api)
	{
		$this->_product_api = $product_api;
	}

	/* iBatchBuilder */

	/**
	 * @return iProductAPI
	 */
	public function getProductAPI()
	{
		return $this->_product_api;
	}

	/**
	 * @param iProductAPI $product_api
	 * @return self
	 */
	public function setProductAPI(iProductAPI $product_api)
	{
		$this->_product_api = $product_api;
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int|null $parent
	 * @return self
	 * @throws Exception
	 */
	public function getCategories($alias, $parent = null)
	{
		$query = 'categories';
		if ( ! is_null($parent)) {
			if ( ! is_int($parent) or $parent < 0) {
				throw new Exception('_invalid_type');
			}
			$query .= "?parent={$parent}";
		}
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int $id
	 * @return self
	 * @throws Exception
	 */
	public function getCategory($alias, $id)
	{
		if ( ! is_int($id) or $id < 1) {
			throw new Exception('_invalid_type');
		}
		$query = "category?id={$id}";
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int|null $category
	 * @return self
	 * @throws Exception
	 */
	public function getProducts($alias, $category = null)
	{
		$query = 'products';
		if ( ! is_null($category)) {
			if ( ! is_int($category) or $category < 0) {
				throw new Exception('_invalid_type');
			}
			$query .= "?category={$category}";
		}
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int $id
	 * @return self
	 * @throws Exception
	 */
	public function getProduct($alias, $id)
	{
		if ( ! is_int($id) or $id < 1) {
			throw new Exception('_invalid_type');
		}
		$query = "product?id={$id}";
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int $count
	 * @return self
	 * @throws Exception
	 */
	public function getRandomProducts($alias, $count)
	{
		if ( ! is_int($count) or $count < 0) {
			throw new Exception('_invalid_type');
		}
		$query = "random_products?count={$count}";
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @return self
	 * @throws Exception
	 */
	public function getClasses($alias)
	{
		$query = 'classes';
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @param int $id
	 * @return self
	 * @throws Exception
	 */
	public function getClass($alias, $id)
	{
		if ( ! is_int($id) or $id < 0) {
			throw new Exception('_invalid_type');
		}
		$query = "classes?id={$id}";
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @param string $alias
	 * @return self
	 * @throws Exception
	 */
	public function getProfile($alias)
	{
		$query = 'profile';
		$this->_addQuery($alias, $query);
		return $this;
	}

	/**
	 * @return mixed
	 * @throws Exception
	 */
	public function run()
	{
		$result = $this->_product_api->runBatch($this->_queries);
		$this->_queries = array();
		return $result;
	}

	/**
	 * @param string $alias
	 * @param string $query
	 * @return void
	 * @throws Exception
	 */
	protected function _addQuery($alias, $query)
	{
		if ( ! is_string($alias)) {
			throw new Exception('_invalid_type');
		}
		if (isset($this->_queries[$alias])) {
			throw new Exception('_alias_exists');
		}
		$this->_queries[$alias] = urlencode($query);
	}
}
Back to Top