Skip to content

Commit cc33d64

Browse files
author
andrey-tech
committed
Initial Commit
0 parents  commit cc33d64

21 files changed

Lines changed: 6642 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
examples/
2+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019-2020 andrey-tech
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Bitrix24 API PHP Wrapper
2+
3+
![Bitrix24 logo](./assets/bitrix24-logo.png)
4+
5+
Обертка на PHP7+ для работы с REST API [Битрикс24](https://dev.1c-bitrix.ru/rest_help/) с использованием механизма входящих вебхуков,
6+
троттлингом запросов к серверу и логированием.
7+
8+
**Документация находится в процессе разработки.**
9+
10+
<!-- MarkdownTOC levels="1,2,3,4,5,6" autoanchor="true" autolink="true" -->
11+
12+
- [Требования](#%D0%A2%D1%80%D0%B5%D0%B1%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)
13+
- [Автор](#%D0%90%D0%B2%D1%82%D0%BE%D1%80)
14+
- [Лицензия](#%D0%9B%D0%B8%D1%86%D0%B5%D0%BD%D0%B7%D0%B8%D1%8F)
15+
16+
<!-- /MarkdownTOC -->
17+
18+
<a id="%D0%A2%D1%80%D0%B5%D0%B1%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F"></a>
19+
## Требования
20+
21+
- PHP >= 7.0.
22+
- Произвольный автозагрузчик классов, реализующий стандарт [PSR-4](https://www.php-fig.org/psr/psr-4/).
23+
24+
25+
26+
27+
28+
<a id="%D0%90%D0%B2%D1%82%D0%BE%D1%80"></a>
29+
## Автор
30+
31+
© 2019-2020 andrey-tech
32+
33+
<a id="%D0%9B%D0%B8%D1%86%D0%B5%D0%BD%D0%B7%D0%B8%D1%8F"></a>
34+
## Лицензия
35+
36+
Данная библиотека распространяется на условиях лицензии [MIT](./LICENSE).

assets/bitrix24-logo-social.png

17.6 KB
Loading

assets/bitrix24-logo.png

10.5 KB
Loading

src/App/AppException.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Обработчик исключений к классах пространства имен \App
5+
*
6+
* @author andrey-tech
7+
* @copyright 2019-2020 andrey-tech
8+
* @see https://github.com/andrey-tech/
9+
* @license MIT
10+
*
11+
* @version 1.0.1
12+
*
13+
* v1.0.0 (28.05.2019) Начальный релиз
14+
* v1.0.1 (26.06.2019) Изменения для App
15+
*
16+
*/
17+
18+
declare(strict_types = 1);
19+
20+
namespace App;
21+
22+
class AppException extends \Exception
23+
{
24+
/**
25+
* Добавляет идентификационную строку App: в сообщение об исключении
26+
* @param string $message Сообщение об исключении
27+
* @param int $code Код исключения
28+
* @param \Exception|null $previous Предыдущее исключение
29+
*/
30+
public function __construct(string $message = '', $code = 0, \Exception $previous = null)
31+
{
32+
parent::__construct("App: " . $message, $code, $previous);
33+
}
34+
}

src/App/Bitrix24/Activity.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* Трейт Actibity. Методы для работы с делами в системе Bitrix24.
5+
*
6+
* @author andrey-tech
7+
* @copyright 2019-2020 andrey-tech
8+
* @see https://github.com/andrey-tech/bitrix24-api-php
9+
* @license MIT
10+
*
11+
* @version 1.0.0
12+
*
13+
* v1.0.0 (02.12.2019) Начальная версия
14+
*
15+
*/
16+
declare(strict_types = 1);
17+
18+
namespace App\Bitrix24;
19+
20+
trait Activity
21+
{
22+
/**
23+
* Возвращает списoк названий полей активности
24+
* @return array
25+
*/
26+
public function getActivityFields()
27+
{
28+
return $this->request('crm.activity.fields');
29+
}
30+
31+
/**
32+
* Возвращает активность по ID
33+
* @param int|string $activityId ID активности
34+
* @return array|null
35+
*/
36+
public function getActivity($activityId)
37+
{
38+
$activity = $this->request(
39+
'crm.activity.get',
40+
[
41+
'id' => $activityId
42+
]
43+
);
44+
45+
return $activity;
46+
}
47+
48+
/**
49+
* Добавляет активность
50+
* @param array $fields Список полей активности
51+
* @return int
52+
*/
53+
public function addActivity(array $fields = [])
54+
{
55+
$result = $this->request(
56+
'crm.activity.add',
57+
[
58+
'fields' => $fields
59+
]
60+
);
61+
62+
return $result;
63+
}
64+
65+
// ------------------------------------------------------------------------
66+
67+
/**
68+
* Пакетно добавляет активности
69+
* @param array $activities Массив параметров активностей
70+
* @return array Массив id активностей
71+
*/
72+
public function addActivities(array $activities = []) :array
73+
{
74+
// Id добавленных активностей
75+
$activityResults = [];
76+
77+
while ($activitiesChunk = array_splice($activities, 0, $this->batchSize)) {
78+
// Формируем массив команд на добавление активностей
79+
$commandParams = [];
80+
foreach ($activitiesChunk as $index => $activity) {
81+
$commandParams[ $index ] = [
82+
'fields' => $activity
83+
];
84+
}
85+
$commands = $this->buildCommands('crm.activity.add', $commandParams);
86+
$activityResult = $this->batchRequest($commands);
87+
88+
// Сравниваем число команд и число id в ответе
89+
$sent = count($commandParams);
90+
$received = count($activityResult);
91+
if ($received != $sent) {
92+
$jsonResponse = $this->toJSON($this->lastResponse);
93+
throw new Bitrix24Exception(
94+
"Невозможно пакетно добавить активности ({$sent}/{$received}): {$jsonResponse}"
95+
);
96+
}
97+
98+
$activityResults = array_merge($activityResults, $activityResult);
99+
}
100+
101+
return $activityResults;
102+
}
103+
}

0 commit comments

Comments
 (0)