Skip to content

Commit

Permalink
Merge pull request #555 from Zhenya-Trainee/feature/DP-2759
Browse files Browse the repository at this point in the history
Added a new ability to work with tags
  • Loading branch information
bessudnov authored Mar 25, 2024
2 parents 8a643e3 + 640ed0a commit 7b93e68
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 5 deletions.
10 changes: 10 additions & 0 deletions examples/leads_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
->setId(19187743)
);

$tagsToAdd = [
0 => [
'name' => 'Тег 123',
],
1 => [
'name' => 'Тег 456'
]
];
$lead->setTagsToAdd($tagsToAdd);

$leadsCollection = new LeadsCollection();
$leadsCollection->add($lead);

Expand Down
2 changes: 1 addition & 1 deletion src/AmoCRM/Client/AmoCRMApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AmoCRMApiRequest
public const CONNECT_TIMEOUT = 5;
public const REQUEST_TIMEOUT = 20;
//TODO Do not forget to change this on each release
public const LIBRARY_VERSION = '1.6.0';
public const LIBRARY_VERSION = '1.6.1';
public const USER_AGENT = 'amoCRM-API-Library/' . self::LIBRARY_VERSION;

public const SUCCESS_STATUSES = [
Expand Down
10 changes: 9 additions & 1 deletion src/AmoCRM/Models/CompanyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmoCRM\Models;

use AmoCRM\Models\Interfaces\ComplexTagsManagerInterface;
use AmoCRM\Models\Traits\MutateTagsTrait;
use AmoCRM\Exceptions\InvalidArgumentException;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Models\Interfaces\CanBeLinkedInterface;
Expand All @@ -24,10 +26,12 @@ class CompanyModel extends BaseApiModel implements
TypeAwareInterface,
CanBeLinkedInterface,
HasIdInterface,
CanReturnDeletedInterface
CanReturnDeletedInterface,
ComplexTagsManagerInterface
{
use RequestIdTrait;
use GetLinkTrait;
use MutateTagsTrait;

public const LEADS = 'leads';
public const CUSTOMERS = 'customers';
Expand Down Expand Up @@ -601,6 +605,10 @@ public function toApi(?string $requestId = "0"): array
$result['custom_fields_values'] = $this->getCustomFieldsValues()->toApi();
}

if (!is_null($this->getTagsToAdd()) || !is_null($this->getTagsToDelete())) {
$result = $this->mutateTags($result);
}

if (!is_null($this->getTags())) {
$result[AmoCRMApiRequest::EMBEDDED]['tags'] = $this->getTags()->toEntityApi();
}
Expand Down
10 changes: 9 additions & 1 deletion src/AmoCRM/Models/ContactModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmoCRM\Models;

use AmoCRM\Models\Interfaces\ComplexTagsManagerInterface;
use AmoCRM\Models\Traits\MutateTagsTrait;
use AmoCRM\Collections\SocialProfiles\SocialProfilesCollection;
use AmoCRM\Exceptions\InvalidArgumentException;
use AmoCRM\Helpers\EntityTypesInterface;
Expand All @@ -24,10 +26,12 @@ class ContactModel extends BaseApiModel implements
TypeAwareInterface,
CanBeLinkedInterface,
HasIdInterface,
CanReturnDeletedInterface
CanReturnDeletedInterface,
ComplexTagsManagerInterface
{
use RequestIdTrait;
use GetLinkTrait;
use MutateTagsTrait;

public const LEADS = 'leads';
public const CUSTOMERS = 'customers';
Expand Down Expand Up @@ -715,6 +719,10 @@ public function toApi(?string $requestId = "0"): array
$result['custom_fields_values'] = $this->getCustomFieldsValues()->toApi();
}

if (!is_null($this->getTagsToAdd()) || !is_null($this->getTagsToDelete())) {
$result = $this->mutateTags($result);
}

if (!is_null($this->getTags())) {
$result[AmoCRMApiRequest::EMBEDDED]['tags'] = $this->getTags()->toEntityApi();
}
Expand Down
13 changes: 12 additions & 1 deletion src/AmoCRM/Models/Customers/CustomerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmoCRM\Models\Customers;

use AmoCRM\Models\Interfaces\ComplexTagsManagerInterface;
use AmoCRM\Models\Traits\MutateTagsTrait;
use AmoCRM\Exceptions\InvalidArgumentException;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Models\Interfaces\CanBeLinkedInterface;
Expand All @@ -26,10 +28,15 @@
*
* @package AmoCRM\Models\Customers
*/
class CustomerModel extends BaseApiModel implements TypeAwareInterface, CanBeLinkedInterface, HasIdInterface
class CustomerModel extends BaseApiModel implements
TypeAwareInterface,
CanBeLinkedInterface,
HasIdInterface,
ComplexTagsManagerInterface
{
use GetLinkTrait;
use RequestIdTrait;
use MutateTagsTrait;

public const CATALOG_ELEMENTS = 'catalog_elements';
public const CONTACTS = 'contacts';
Expand Down Expand Up @@ -835,6 +842,10 @@ public function toApi(?string $requestId = "0"): array
$result['custom_fields_values'] = $this->getCustomFieldsValues()->toApi();
}

if (!is_null($this->getTagsToAdd()) || !is_null($this->getTagsToDelete())) {
$result = $this->mutateTags($result);
}

if (!is_null($this->getTags())) {
$result[AmoCRMApiRequest::EMBEDDED]['tags'] = $this->getTags()->toEntityApi();
}
Expand Down
23 changes: 23 additions & 0 deletions src/AmoCRM/Models/Interfaces/ComplexTagsManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace AmoCRM\Models\Interfaces;

/**
* Interface ComplexTagsManagerInterface
*
* Используется в моделях, которые поддерживают новый формат работы с тегами tags_to_add и tags_to_delete.
* Реализация находится в MutateTagsTraits
*
* @package AmoCRM\Models\Interfaces
*/
interface ComplexTagsManagerInterface
{
/**
* @param array $entity
*
* @return array
*/
public function mutateTags(array $entity): array;
}
10 changes: 9 additions & 1 deletion src/AmoCRM/Models/LeadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmoCRM\Models;

use AmoCRM\Models\Interfaces\ComplexTagsManagerInterface;
use AmoCRM\Models\Traits\MutateTagsTrait;
use AmoCRM\EntitiesServices\Unsorted;
use AmoCRM\Exceptions\InvalidArgumentException;
use AmoCRM\Helpers\EntityTypesInterface;
Expand All @@ -26,10 +28,12 @@ class LeadModel extends BaseApiModel implements
TypeAwareInterface,
CanBeLinkedInterface,
HasIdInterface,
CanReturnDeletedInterface
CanReturnDeletedInterface,
ComplexTagsManagerInterface
{
use RequestIdTrait;
use GetLinkTrait;
use MutateTagsTrait;

public const LOST_STATUS_ID = 143;
public const WON_STATUS_ID = 142;
Expand Down Expand Up @@ -935,6 +939,10 @@ public function toApi(?string $requestId = "0"): array
$result['custom_fields_values'] = $this->getCustomFieldsValues()->toApi();
}

if (!is_null($this->getTagsToAdd()) || !is_null($this->getTagsToDelete())) {
$result = $this->mutateTags($result);
}

if (!is_null($this->getTags())) {
$result[AmoCRMApiRequest::EMBEDDED]['tags'] = $this->getTags()->toEntityApi();
}
Expand Down
89 changes: 89 additions & 0 deletions src/AmoCRM/Models/Traits/MutateTagsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace AmoCRM\Models\Traits;

use AmoCRM\Models\Interfaces\ComplexTagsManagerInterface;

/**
* Trait TagsTrait
*
* @package AmoCRM\Models\Traits
*/
trait MutateTagsTrait
{
/**
* @var array|null
*/
protected $tagsToAdd = null;

/**
* @var array|null
*/
protected $tagsToDelete = null;


/**
* @return array|null
*/
public function getTagsToAdd(): ?array
{
return $this->tagsToAdd;
}

/**
* @param array|null $tagsToAdd
*
* @return self
*/
public function setTagsToAdd(?array $tagsToAdd): self
{
$this->tagsToAdd = $tagsToAdd;

return $this;
}

/**
* @return array|null
*/
public function getTagsToDelete(): ?array
{
return $this->tagsToDelete;
}

/**
* @param array|null $tagsToDelete
*
* @return self
*/
public function setTagsToDelete(?array $tagsToDelete): self
{
$this->tagsToDelete = $tagsToDelete;

return $this;
}

/**
* При получении сущности из API, она приходит вместе с _embedded[tags]. Поэтому если в него засетить
* tagsToDelete или tagsToAdd, то в запросе на обновление оно уйдет вместе с _embedded[tags] и в приоритете
* будут теги из _embedded[tags] и tagsToAdd/tagsToDelete не будут учтены.
*
* @param array $entity
*
* @return array
*/
public function mutateTags(array $entity): array
{
if (!is_null($this->getTagsToAdd())) {
$entity['tags_to_add'] = $this->getTagsToAdd();
}

if (!is_null($this->getTagsToDelete())) {
$entity['tags_to_delete'] = $this->getTagsToDelete();
}
$this->setTags(null);

return $entity;
}
}

0 comments on commit 7b93e68

Please sign in to comment.