1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM\services; |
4
|
|
|
|
5
|
|
|
use linkprofit\AmoCRM\entities\CatalogElement; |
6
|
|
|
use linkprofit\AmoCRM\entities\CustomField; |
7
|
|
|
use linkprofit\AmoCRM\entities\EntityInterface; |
8
|
|
|
use linkprofit\AmoCRM\entities\Value; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CatalogElementService |
12
|
|
|
* |
13
|
|
|
* @package linkprofit\AmoCRM\services |
14
|
|
|
*/ |
15
|
|
|
class CatalogElementService extends BaseService |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CatalogElement[] |
19
|
|
|
*/ |
20
|
|
|
protected $entities = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param EntityInterface|CatalogElement $catalogElement |
24
|
|
|
*/ |
25
|
8 |
|
public function add(EntityInterface $catalogElement) |
26
|
|
|
{ |
27
|
8 |
|
if ($catalogElement instanceof CatalogElement) { |
28
|
8 |
|
$this->entities[] = $catalogElement; |
29
|
8 |
|
} |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $page |
34
|
|
|
* @param string|null $query |
35
|
|
|
* @param array $params |
36
|
|
|
* |
37
|
|
|
* @return array|bool |
38
|
|
|
*/ |
39
|
4 |
|
public function lists($page = 1, $query = null, array $params = null) |
40
|
|
|
{ |
41
|
4 |
|
$queryParams = []; |
42
|
4 |
|
$queryParams['PAGEN_1'] = $page; |
43
|
|
|
|
44
|
4 |
|
if (!empty($query)) { |
45
|
2 |
|
$queryParams['term'] = $query; |
46
|
2 |
|
} |
47
|
|
|
|
48
|
4 |
|
if ($params) { |
49
|
1 |
|
$queryParams = array_merge($queryParams, $params); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
4 |
|
$link = $this->getLink() . '?' . http_build_query($queryParams); |
53
|
|
|
|
54
|
4 |
|
$this->request->performRequest($link, [], 'application/json', 'GET'); |
55
|
4 |
|
$this->response = $this->request->getResponse(); |
56
|
|
|
|
57
|
4 |
|
return $this->parseResponseToEntities(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $array |
62
|
|
|
* |
63
|
|
|
* @return CatalogElement |
64
|
|
|
*/ |
65
|
8 |
|
public function parseArrayToEntity($array) |
66
|
|
|
{ |
67
|
8 |
|
$element = new CatalogElement(); |
68
|
8 |
|
$element->set($array); |
69
|
|
|
|
70
|
8 |
|
if (isset($array['custom_fields'])) { |
71
|
4 |
|
foreach ($array['custom_fields'] as $customFieldArray) { |
72
|
4 |
|
$customField = new CustomField($customFieldArray['id']); |
73
|
4 |
|
$customField->set($customFieldArray); |
74
|
|
|
|
75
|
4 |
|
if (isset($customFieldArray['values'])) { |
76
|
4 |
|
foreach ($customFieldArray['values'] as $value) { |
77
|
4 |
|
$value = new Value($value); |
78
|
4 |
|
$customField->addValue($value); |
79
|
4 |
|
} |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
$element->addCustomField($customField); |
83
|
4 |
|
} |
84
|
4 |
|
} |
85
|
|
|
|
86
|
8 |
|
return $element; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
8 |
|
protected function getLink() |
93
|
|
|
{ |
94
|
8 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/catalog_elements'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |