Completed
Pull Request — master (#11)
by
unknown
04:13
created

CustomField::apiDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 2
1
<?php
2
3
namespace AmoCRM\Models;
4
5
/**
6
 * Class CustomField
7
 *
8
 * Класс модель для работы с Дополнительными полями
9
 *
10
 * @package AmoCRM\Models
11
 * @author mihasichechek <[email protected]>
12
 * @link https://github.com/dotzero/amocrm-php
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
class CustomField extends AbstractModel
18
{
19
20
    const TYPE_TEXT = 1;
21
    const TYPE_NUMERIC = 2;
22
    const TYPE_CHECKBOX = 3;
23
    const TYPE_SELECT = 4;
24
    const TYPE_MULTISELECT = 5;
25
    const TYPE_DATE = 6;
26
    const TYPE_URL = 7;
27
    const TYPE_MULTITEXT = 8;
28
    const TYPE_TEXTAREA = 9;
29
    const TYPE_RADIOBUTTON = 10;
30
31
32
    const ENTITY_CONTACT = 1;
33
    const ENTITY_LEAD = 2;
34
    const ENTITY_COMPANY = 3;
35
36
37
    /**
38
     * @var array Список доступный полей для модели (исключая кастомные поля)
39
     */
40
    protected $fields = [
41
        'name',
42
        'request_id',
43
        'disabled',
44
        'type',
45
        'element_type',
46
        'origin'
47
    ];
48
49
50
    /**
51
     * Добавление дополнительных полей
52
     *
53
     * Метод позволяет добавлять дополнительные поля по одному или пакетно
54
     *
55
     * @link https://developers.amocrm.ru/rest_api/fields_set.php
56
     *
57
     * @param $fields array Массив дополнительных полей для пакетного добавления
58
     *
59
     * @return int|array Уникальный идентификатор контакта или массив при пакетном добавлении
60
     */
61
    public function apiAdd($fields = [])
62
    {
63
        if (empty($fields)) {
64
            $fields = [$this];
65
        }
66
67
        $parameters = [
68
            'fields' => [
69
                'add' => [],
70
            ],
71
        ];
72
73
        foreach ($fields AS $field) {
74
            $parameters['fields']['add'][] = $field->getValues();
75
        }
76
77
        $response = $this->postRequest('/private/api/v2/json/fields/set', $parameters);
78
79
        if (isset($response['fields']['add'])) {
80
            $result = array_map(function ($item) {
81
                return $item['id'];
82
            }, $response['fields']['add']);
83
        } else {
84
            return [];
85
        }
86
87
        return count($fields) == 1 ? array_shift($result) : $result;
88
    }
89
90
    /**
91
     * Удаление дополнительных полей
92
     *
93
     * Метод позволяет удалять дополнительные поля по одной
94
     *
95
     * @link https://developers.amocrm.ru/rest_api/fields_set.php
96
     *
97
     * @param $id int Уникальный идентификатор воронки
98
     *
99
     * @param $origin string Уникальный идентификатор сервиса заданный при создании параметром origin
100
     *
101
     * @return array Ответ amoCRM API
102
     */
103
    public function apiDelete($id, $origin)
104
    {
105
        $parameters = [
106
            'fields' => [
107
                'delete' => [
108
                    [
109
                        'id' => (int)$id,
110
                        'origin' => $origin
111
                    ]
112
                ]
113
            ]
114
        ];
115
116
        $response = $this->postRequest('/private/api/v2/json/fields/set', $parameters);
117
118
        return $response;
119
    }
120
}