1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mb24dev\AmoCRM\Method; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use mb24dev\AmoCRM\Entity\AmoEntityInterface; |
7
|
|
|
use mb24dev\AmoCRM\Entity\AmoIdentityInterface; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LeadsSet |
12
|
|
|
* |
13
|
|
|
* @package mb24dev\AmoCRM\Method |
14
|
|
|
*/ |
15
|
|
|
class LeadsSet extends BaseMethod |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AmoEntityInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $addEntities = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AmoEntityInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $updateEntities = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return RequestInterface |
29
|
|
|
*/ |
30
|
|
|
public function buildRequest() |
31
|
|
|
{ |
32
|
|
|
$addEntities = []; |
33
|
|
|
$updateEntities = []; |
34
|
|
|
|
35
|
|
|
foreach ($this->addEntities as $entity) { |
36
|
|
|
$addEntities[] = $entity->toAmoArray(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($this->updateEntities as $entity) { |
40
|
|
|
$updateEntities[] = $entity->toAmoArray(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$body = json_encode( |
44
|
|
|
[ |
45
|
|
|
'request' => [ |
46
|
|
|
'leads' => [ |
47
|
|
|
'add' => $addEntities, |
48
|
|
|
'update' => $updateEntities, |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$request = new Request( |
55
|
|
|
'POST', $this->getUser()->getAmoCRMDomain() . 'private/api/v2/json/leads/set', [], $body |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return $request; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param AmoIdentityInterface[] $entities |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setLeads($entities) |
66
|
|
|
{ |
67
|
|
|
if (!is_array($entities)) { |
68
|
|
|
$entities = [$entities]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($entities as $entity) { |
72
|
|
|
if ($entity->getAmoID()) { |
73
|
|
|
$this->updateEntities[] = $entity; |
74
|
|
|
} else { |
75
|
|
|
$this->addEntities[] = $entity; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|