-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from Zhenya-Trainee/feature/DP-2759
Added a new ability to work with tags
- Loading branch information
Showing
8 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/AmoCRM/Models/Interfaces/ComplexTagsManagerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |