1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmoCRM\Models; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Call |
7
|
|
|
* |
8
|
|
|
* Класс модель для работы со Звонками |
9
|
|
|
* |
10
|
|
|
* @package AmoCRM\Models |
11
|
|
|
* @author dotzero <[email protected]> |
12
|
|
|
* @link http://www.dotzero.ru/ |
13
|
|
|
* @link https://github.com/dotzero/amocrm-php |
14
|
|
|
* |
15
|
|
|
* For the full copyright and license information, please view the LICENSE |
16
|
|
|
* file that was distributed with this source code. |
17
|
|
|
*/ |
18
|
|
|
class Call extends AbstractModel |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array Список доступный полей для модели (исключая кастомные поля) |
22
|
|
|
*/ |
23
|
|
|
protected $fields = [ |
24
|
|
|
'account_id', |
25
|
|
|
'uuid', |
26
|
|
|
'caller', |
27
|
|
|
'to', |
28
|
|
|
'date', |
29
|
|
|
'type', |
30
|
|
|
'billsec', |
31
|
|
|
'link', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @const string Тип звонка входящий |
36
|
|
|
*/ |
37
|
|
|
const TYPE_INBOUND = 'inbound'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @const string Тип звонка исходящий |
41
|
|
|
*/ |
42
|
|
|
const TYPE_OUTBOUND = 'outbound'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Сеттер для даты создания контакта |
46
|
|
|
* |
47
|
|
|
* @param string $date Дата в произвольном формате |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
1 |
|
public function setDate($date) |
51
|
|
|
{ |
52
|
1 |
|
$this->values['date'] = strtotime($date); |
53
|
|
|
|
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Добавление звонков |
59
|
|
|
* |
60
|
|
|
* Метод позволяет добавлять звонки по одному или пакетно |
61
|
|
|
* |
62
|
|
|
* @link https://developers.amocrm.ru/rest_api/calls_set.php |
63
|
|
|
* @param string $code Уникальный идентификатор сервиса |
64
|
|
|
* @param string $key Ключ сервиса, который можно получить написав в техническую поддержку amoCRM |
65
|
|
|
* @param array $calls Массив каталогов для пакетного добавления |
66
|
|
|
* @return string|array Уникальный идентификатор звонка или массив при пакетном добавлении |
67
|
|
|
*/ |
68
|
1 |
|
public function apiAdd($code, $key, $calls = []) |
69
|
|
|
{ |
70
|
1 |
|
$this->getParameters()->addGet('code', $code); |
71
|
1 |
|
$this->getParameters()->addGet('key', $key); |
72
|
|
|
|
73
|
1 |
|
if (empty($calls)) { |
74
|
1 |
|
$calls = [$this]; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
$parameters = [ |
78
|
1 |
|
'add' => [], |
79
|
1 |
|
]; |
80
|
|
|
|
81
|
1 |
|
foreach ($calls AS $call) { |
82
|
1 |
|
$parameters['add'][] = $call->getValues(); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
$response = $this->postRequest('/api/calls/add/', $parameters); |
86
|
|
|
|
87
|
1 |
|
if (!isset($response['calls']['add']['errors'])) { |
88
|
|
|
return false; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$result = []; |
92
|
1 |
|
if (isset($response['calls']['add']['success'])) { |
93
|
1 |
|
$result = $response['calls']['add']['success']; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
return count($calls) == 1 ? array_shift($result) : $result; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.