Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@
}
],
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"php": ">=8.2",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
"AmoCRM\\": "src"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
}
}
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct($domain, $login, $apikey, $proxy = null)
* @return ModelInterface
* @throws ModelException
*/
public function __get($name)
public function __get($name): ModelInterface
{
$classname = '\\AmoCRM\\Models\\' . Format::camelCase($name);

Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Exception extends \Exception
/**
* @var array Справочник ошибок и ответов amoCRM API
*/
protected $errors = [
protected array $errors = [
'101' => 'Аккаунт не найден',
'102' => 'POST-параметры должны передаваться в формате JSON',
'103' => 'Параметры не переданы',
Expand Down
20 changes: 10 additions & 10 deletions src/Helpers/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Fields implements \IteratorAggregate, \ArrayAccess, \Countable
* @param mixed $name Название поля
* @param mixed $value Значение поля
*/
public function __set($name, $value)
public function __set($name, $value): void
{
$this->offsetSet($name, $value);
}
Expand All @@ -39,7 +39,7 @@ public function __set($name, $value)
* @param mixed $name Название поля
* @return mixed Значение поля
*/
public function __get($name)
public function __get($name): mixed
{
return $this->offsetGet($name);
}
Expand All @@ -50,7 +50,7 @@ public function __get($name)
* @param mixed $key Название поля
* @param mixed $value Значение поля
*/
public function add($key, $value = null)
public function add($key, $value = null): void
{
$this->offsetSet($key, $value);
}
Expand All @@ -61,7 +61,7 @@ public function add($key, $value = null)
* @param mixed $key Название поля
* @return mixed Значение поля
*/
public function get($key)
public function get($key): mixed
{
return $this->offsetGet($key);
}
Expand All @@ -73,7 +73,7 @@ public function get($key)
* @param mixed $offset Смещение (ключ) для проверки
* @return boolean Возвращает true или false
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->fields[$offset]);
}
Expand All @@ -85,7 +85,7 @@ public function offsetExists($offset)
* @param mixed $offset Смещение (ключ) для возврата
* @return mixed Значение смещения (ключа)
*/
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
if (isset($this->fields[$offset])) {
return $this->fields[$offset];
Expand All @@ -101,7 +101,7 @@ public function offsetGet($offset)
* @param mixed $offset Смещение (ключ), которому будет присваиваться значение
* @param mixed $value Значение для присвоения
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->fields[$offset] = $value;
}
Expand All @@ -112,7 +112,7 @@ public function offsetSet($offset, $value)
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset Смещение для удаления
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
if (isset($this->fields[$offset])) {
unset($this->fields[$offset]);
Expand All @@ -125,7 +125,7 @@ public function offsetUnset($offset)
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return \Traversable Экземпляр объекта, использующего Iterator или Traversable
*/
public function getIterator()
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->fields);
}
Expand All @@ -136,7 +136,7 @@ public function getIterator()
* @link http://php.net/manual/en/countable.count.php
* @return int Количество элементов объекта
*/
public function count()
public function count(): int
{
return count($this->fields);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Models/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractModel extends Request implements ArrayAccess, ModelInterf
/**
* @var array Список значений полей для модели
*/
protected $values = [];
protected array $values = [];

/**
* Возвращает называние Модели
Expand All @@ -49,7 +49,7 @@ public function __toString()
* @param mixed $offset Название поля для проверки
* @return boolean Возвращает true или false
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->values[$offset]);
}
Expand All @@ -61,7 +61,7 @@ public function offsetExists($offset)
* @param mixed $offset Название поля для возврата
* @return mixed Значение поля
*/
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
if (isset($this->values[$offset])) {
return $this->values[$offset];
Expand All @@ -79,12 +79,12 @@ public function offsetGet($offset)
* @param mixed $offset Название поля, которому будет присваиваться значение
* @param mixed $value Значение для присвоения
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$setter = 'set' . Format::camelCase($offset);

if (method_exists($this, $setter)) {
return $this->$setter($value);
$this->$setter($value);
} elseif (in_array($offset, $this->fields)) {
$this->values[$offset] = $value;
}
Expand All @@ -96,7 +96,7 @@ public function offsetSet($offset, $value)
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset Название поля для удаления
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
if (isset($this->values[$offset])) {
unset($this->values[$offset]);
Expand All @@ -108,7 +108,7 @@ public function offsetUnset($offset)
*
* @return array Список значений полей модели
*/
public function getValues()
public function getValues(): array
{
return $this->values;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public function addCustomMultiField($id, $values)
*/
protected function checkId($id)
{
if (intval($id) != $id || $id < 1) {
if (filter_var($id, FILTER_VALIDATE_INT) === false || $id < 1) {
throw new Exception('Id must be integer and positive');
}

Expand Down
36 changes: 31 additions & 5 deletions src/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,43 @@ public function apiUpdate($id, $modified = 'now')
{
$this->checkId($id);

$company = $this->getValues();
$company['id'] = $id;
$company['last_modified'] = strtotime($modified);

return $this->apiUpdateList([$company]);
}

/**
* Пакетное обновление компаний
*
* @link https://web.archive.org/web/20140903201932/https://developers.amocrm.ru/rest_api/company_set.php
* @param \AmoCRM\Models\Company[]|array[] $companies Уникальный идентификатор компании
* @param string $modified Дата последнего изменения данной сущности
* @return bool Флаг успешности выполнения запроса
* @throws \AmoCRM\Exception
*/
public function apiUpdateList($companies = [], $modified = 'now')
{
$parameters = [
'contacts' => [
'update' => [],
],
];

$company = $this->getValues();
$company['id'] = $id;
$company['last_modified'] = strtotime($modified);

$parameters['contacts']['update'][] = $company;
/** @var \AmoCRM\Models\Company|array $company */
foreach ($companies as $company) {
if ($company instanceof Company) {
$company = $company->getValues();
}
$company['id'] = isset($company['id']) ? $company['id'] : null;
$company['last_modified'] = isset($company['last_modified'])
? $company['last_modified']
: strtotime($modified);

$this->checkId($company['id']);
$parameters['contacts']['update'][] = $company;
}

$response = $this->postRequest('/private/api/v2/json/company/set', $parameters);

Expand Down
40 changes: 34 additions & 6 deletions src/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function apiAdd($contacts = [])
*
* Метод позволяет обновлять данные по уже существующим контактам
*
* @link https://developers.amocrm.ru/rest_api/contacts_set.php
* @link https://web.archive.org/web/20140903111912/https://developers.amocrm.ru/rest_api/contacts_set.php
* @param int $id Уникальный идентификатор контакта
* @param string $modified Дата последнего изменения данной сущности
* @return bool Флаг успешности выполнения запроса
Expand All @@ -114,23 +114,51 @@ public function apiUpdate($id, $modified = 'now')
{
$this->checkId($id);

$contact = $this->getValues();
$contact['id'] = $id;
$contact['last_modified'] = strtotime($modified);

return $this->apiUpdateList([$contact]);
}

/**
* Пакетное обновление контактов
*
* @link https://web.archive.org/web/20140903111912/https://developers.amocrm.ru/rest_api/contacts_set.php
* @param \AmoCRM\Models\Contact[]|array[] $contacts массив контактов для обновления
* @param string $modified
* @return bool
* @throws \AmoCRM\Exception
* @throws \AmoCRM\NetworkException
*/
public function apiUpdateList($contacts = [], $modified = 'now')
{
$parameters = [
'contacts' => [
'update' => [],
],
];

$contact = $this->getValues();
$contact['id'] = $id;
$contact['last_modified'] = strtotime($modified);

$parameters['contacts']['update'][] = $contact;
/** @var \AmoCRM\Models\Contact|array $contact */
foreach ($contacts as $contact) {
if ($contact instanceof Contact) {
$contact = $contact->getValues();
}
$contact['id'] = isset($contact['id']) ? $contact['id'] : null;
$contact['last_modified'] = isset($contact['last_modified'])
? $contact['last_modified']
: strtotime($modified);

$this->checkId($contact['id']);
$parameters['contacts']['update'][] = $contact;
}

$response = $this->postRequest('/private/api/v2/json/contacts/set', $parameters);

return empty($response['contacts']['update']['errors']);
}


/**
* Связи между сделками и контактами
*
Expand Down
39 changes: 33 additions & 6 deletions src/Models/Lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function apiAdd($leads = [])
*
* Метод позволяет обновлять данные по уже существующим сделкам
*
* @link https://developers.amocrm.ru/rest_api/leads_set.php
* @link https://web.archive.org/web/20140903201937/https://developers.amocrm.ru/rest_api/leads_set.php
* @param int $id Уникальный идентификатор сделки
* @param string $modified Дата последнего изменения данной сущности
* @return bool Флаг успешности выполнения запроса
Expand All @@ -116,17 +116,44 @@ public function apiUpdate($id, $modified = 'now')
{
$this->checkId($id);

$lead = $this->getValues();
$lead['id'] = $id;
$lead['last_modified'] = strtotime($modified);

return $this->apiUpdateList([$lead]);
}

/**
* Пакетное обновление сделок
*
* @link https://web.archive.org/web/20140903201937/https://developers.amocrm.ru/rest_api/leads_set.php
* @param \AmoCRM\Models\Lead[]|array[] $leads массив сделок для обновления
* @param string $modified
* @return bool
* @throws \AmoCRM\Exception
* @throws \AmoCRM\NetworkException
*/
public function apiUpdateList($leads = [], $modified = 'now')
{
$parameters = [
'leads' => [
'update' => [],
],
];

$lead = $this->getValues();
$lead['id'] = $id;
$lead['last_modified'] = strtotime($modified);

$parameters['leads']['update'][] = $lead;
/** @var \AmoCRM\Models\Lead|array $lead */
foreach ($leads as $lead) {
if ($lead instanceof Lead) {
$lead = $lead->getValues();
}
$lead['id'] = isset($lead['id']) ? $lead['id'] : null;
$lead['last_modified'] = isset($lead['last_modified'])
? $lead['last_modified']
: strtotime($modified);

$this->checkId($lead['id']);
$parameters['leads']['update'][] = $lead;
}

$response = $this->postRequest('/private/api/v2/json/leads/set', $parameters);

Expand Down
Loading