|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lifeboat\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Lifeboat\Exceptions\ApiException; |
|
6
|
|
|
use Lifeboat\Exceptions\OAuthException; |
|
7
|
|
|
use Lifeboat\Models\Product; |
|
8
|
|
|
use Lifeboat\Resource\ListResource; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Product |
|
12
|
|
|
* @package Lifeboat\Services |
|
13
|
|
|
*/ |
|
14
|
|
|
class Products extends ApiService { |
|
15
|
|
|
|
|
16
|
|
|
const LIST_DETAILED = 'detailed'; |
|
17
|
|
|
const LIST_SIMPLE = 'simple'; |
|
18
|
|
|
const LIST_BARE = 'bare'; |
|
19
|
|
|
const SORT_TITLE_ASC = 'title_az'; |
|
20
|
|
|
const SORT_TITLE_DESC = 'title_za'; |
|
21
|
|
|
const SORT_SKU_ASC = 'title_az'; |
|
22
|
|
|
const SORT_SKU_DESC = 'title_za'; |
|
23
|
|
|
const SORT_PRICE_ASC = 'title_az'; |
|
24
|
|
|
const SORT_PRICE_DESC = 'title_za'; |
|
25
|
|
|
const SORT_CREATED_ASC = 'title_az'; |
|
26
|
|
|
const SORT_CREATED_DESC = 'title_za'; |
|
27
|
|
|
const SORT_EDITED_ASC = 'title_az'; |
|
28
|
|
|
const SORT_EDITED_DESC = 'title_za'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $id |
|
32
|
|
|
* @return Product|null |
|
33
|
|
|
* @throws ApiException |
|
34
|
|
|
* @throws OAuthException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function fetch(int $id): ?Product |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var Product|null $fetch */ |
|
39
|
|
|
$fetch = $this->_get('api/products/product/' . $id); |
|
40
|
|
|
return $fetch; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
* @return Product|null |
|
46
|
|
|
* @throws ApiException |
|
47
|
|
|
* @throws OAuthException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function create(array $data): ?Product |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var Product|null $create */ |
|
52
|
|
|
$create = $this->_post('api/products/product/', $data); |
|
53
|
|
|
return $create; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param int $id |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* @return Product|null |
|
60
|
|
|
* @throws ApiException |
|
61
|
|
|
* @throws OAuthException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function update(int $id, array $data): ?Product |
|
64
|
|
|
{ |
|
65
|
|
|
/** @var Product|null $post */ |
|
66
|
|
|
$post = $this->_post('api/products/product' . $id, $data); |
|
67
|
|
|
return $post; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $search |
|
72
|
|
|
* @param string $sort |
|
73
|
|
|
* @param string $type |
|
74
|
|
|
* @return ListResource |
|
75
|
|
|
*/ |
|
76
|
|
|
public function all( |
|
77
|
|
|
string $search = '', |
|
78
|
|
|
string $sort = self::SORT_CREATED_DESC, |
|
79
|
|
|
string $type = self::LIST_BARE |
|
80
|
|
|
): ListResource { |
|
81
|
|
|
return new ListResource($this->getClient(), 'api/products/all', [ |
|
82
|
|
|
'search' => $search, |
|
83
|
|
|
'sort' => $sort, |
|
84
|
|
|
'data' => $type |
|
85
|
|
|
], 20); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|