Skip to content

Commit e765e38

Browse files
authored
Merge pull request #612 from aleekseei/feature/INTEGR-2619
(INTEGR-2619) добавил FilesFilter
2 parents 4d6fc8e + c748327 commit e765e38

File tree

4 files changed

+278
-8
lines changed

4 files changed

+278
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ $leadsService = $apiClient->leads();
656656
| ```unsorted``` | ```\AmoCRM\Filters\UnsortedFilter``` | Фильтр для метода `\AmoCRM\EntitiesServices\Unsorted::get` | ✅ |
657657
| ```unsorted``` | ```\AmoCRM\Filters\UnsortedSummaryFilter``` | Фильтр для метода `\AmoCRM\EntitiesServices\Unsorted::summary` | ❌ |
658658
| ```webhooks``` | ```\AmoCRM\Filters\WebhooksFilter``` | Фильтр для метода получения хуков | ❌ |
659+
| ```files``` | ```\AmoCRM\Filters\FilesFilter``` | Фильтр для метода получения файлов `\AmoCRM\EntitiesServices\Files::get` | ❌ |
659660
| ```sources``` | ```\AmoCRM\Filters\SourcesFilter``` | Фильтр для метода получения источников `\AmoCRM\EntitiesServices\Sources::get` | ❌ |
660661
| ```chatTemplates``` | ```\AmoCRM\Filters\Chats\TemplatesFilter``` | Фильтр для метода получения шаблонов чатов `\AmoCRM\EntitiesServices\Chats\Templates::get` | ❌ |
661662
| Сервисы, где необходима постраничная навигация | ```\AmoCRM\Filters\PagesFilter``` | Фильтр, который подходит для любого сервиса, где есть постраничная навигация | ❌ |

examples/files_actions.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use AmoCRM\Collections\FileLinksCollection;
4+
use AmoCRM\Filters\FilesFilter;
45
use AmoCRM\Models\FileLinkModel;
56
use AmoCRM\Models\Files\FileModel;
67
use AmoCRM\Models\Files\FileUploadModel;
@@ -36,16 +37,20 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
3637
);
3738

3839

39-
//Получим список файлов
40+
// Получим список файлов
41+
$filter = new FilesFilter();
42+
$filter->setExtensions(['jpg', 'jpeg', 'png']); // фильтр, который ищет все изображения
43+
//$filter->setDeleted(true); // фильтр, который ищет только среди удаленных файлов
44+
4045
try {
41-
$files = $apiClient->files()->get();
46+
$files = $apiClient->files()->get($filter);
4247
var_dump($files->toArray());
4348
} catch (AmoCRMApiException $e) {
4449
printError($e);
4550
}
4651

4752

48-
//Получим модель файла
53+
// Получим модель файла
4954
try {
5055
$file = $apiClient->files()->getOne('83bd6974-d54e-4fed-9fec-a3e5a31220db');
5156
var_dump($file->toArray());
@@ -54,7 +59,7 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
5459
}
5560

5661

57-
//Освежим модель файла
62+
// Освежим модель файла
5863
try {
5964
$file = (new FileModel())->setUuid('6639ccbf-4bc6-4a08-8537-65adb868967d');
6065
$file = $apiClient->files()->syncOne($file);
@@ -74,7 +79,7 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
7479
}
7580

7681

77-
//Загрузим файл
82+
// Загрузим файл
7883
$uploadModel = new FileUploadModel();
7984
$uploadModel->setName('Название файла123.txt')
8085
->setLocalPath('/tmp/123');
@@ -142,7 +147,7 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
142147
}
143148

144149

145-
//Создадим примечание с файлом
150+
// Создадим примечание с файлом
146151
$noteModel = new AttachmentNote();
147152
$noteModel->setEntityId(20285255)
148153
->setFileName($file->getNameWithExtension()) // название файла, которое будет отображаться в примечании
@@ -168,7 +173,7 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
168173
//}
169174

170175

171-
//Привяжем файл к сделке с ID 21825653, чтобы он отображался во вкладке файлы
176+
// Привяжем файл к сделке с ID 21825653, чтобы он отображался во вкладке файлы
172177
try {
173178
$result = $apiClient->entityFiles(EntityTypesInterface::LEADS, 21825653)->add(
174179
(new FileLinksCollection())
@@ -190,7 +195,7 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
190195
printError($e);
191196
}
192197

193-
//Отвяжем файл от сделки с ID 21825653
198+
// Отвяжем файл от сделки с ID 21825653
194199
try {
195200
$result = $apiClient->entityFiles(EntityTypesInterface::LEADS, 21825653)->delete(
196201
(new FileLinksCollection())

src/AmoCRM/EntitiesServices/Files.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AmoCRM\EntitiesServices;
44

5+
use AmoCRM\Filters\FilesFilter;
56
use AmoCRM\Models\Files\FileModel;
67
use AmoCRM\Models\Files\FileUploadModel;
78
use AmoCRM\Client\AmoCRMApiClient;
@@ -22,6 +23,7 @@
2223
* Class Files
2324
*
2425
* @package AmoCRM\EntitiesServices
26+
* @method null|FilesCollection get(?FilesFilter $filter = null, array $with = [])
2527
* @method null|FileModel getOne($uuid, array $with = [])
2628
*/
2729
class Files extends BaseEntity implements HasPageMethodsInterface, HasDeleteMethodInterface

src/AmoCRM/Filters/FilesFilter.php

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AmoCRM\Filters;
6+
7+
use AmoCRM\Filters\Interfaces\HasPagesInterface;
8+
use AmoCRM\Filters\Traits\ArrayOrNumericFilterTrait;
9+
use AmoCRM\Filters\Traits\ArrayOrStringFilterTrait;
10+
use AmoCRM\Filters\Traits\IntOrIntRangeFilterTrait;
11+
use AmoCRM\Filters\Traits\PagesFilterTrait;
12+
13+
use function implode;
14+
use function is_null;
15+
16+
class FilesFilter extends BaseEntityFilter implements HasPagesInterface
17+
{
18+
use ArrayOrNumericFilterTrait;
19+
use ArrayOrStringFilterTrait;
20+
use IntOrIntRangeFilterTrait;
21+
use PagesFilterTrait;
22+
23+
/** @var array|null */
24+
private $uuid;
25+
26+
/** @var string|null */
27+
private $name;
28+
29+
/** @var array|null */
30+
private $extensions;
31+
32+
/** @var string|null */
33+
private $term;
34+
35+
/** @var int|null */
36+
private $sourceId;
37+
38+
/** @var bool */
39+
private $deleted = false;
40+
41+
/** @var array|null */
42+
private $size;
43+
44+
/** @var array|null */
45+
private $date;
46+
47+
/** @var array|null */
48+
private $createdBy;
49+
50+
/** @var array|null */
51+
private $updatedBy;
52+
53+
public function getUuid(): ?array
54+
{
55+
return $this->uuid;
56+
}
57+
58+
/**
59+
* @param array|string|null $uuid
60+
* @return FilesFilter
61+
*/
62+
public function setUuid($uuid): self
63+
{
64+
$this->uuid = $this->parseArrayOrStringFilter($uuid);
65+
66+
return $this;
67+
}
68+
69+
public function getName(): ?string
70+
{
71+
return $this->name;
72+
}
73+
74+
public function setName(?string $name): self
75+
{
76+
$this->name = $name;
77+
78+
return $this;
79+
}
80+
81+
public function getExtensions(): ?array
82+
{
83+
return $this->extensions;
84+
}
85+
86+
/**
87+
* @param array|string|null $extensions
88+
* @return FilesFilter
89+
*/
90+
public function setExtensions($extensions): self
91+
{
92+
$this->extensions = $this->parseArrayOrStringFilter($extensions);
93+
94+
return $this;
95+
}
96+
97+
public function getTerm(): ?string
98+
{
99+
return $this->term;
100+
}
101+
102+
public function setTerm(?string $term): self
103+
{
104+
$this->term = $term;
105+
106+
return $this;
107+
}
108+
109+
public function getSourceId(): ?int
110+
{
111+
return $this->sourceId;
112+
}
113+
114+
public function setSourceId(?int $sourceId): self
115+
{
116+
$this->sourceId = $sourceId;
117+
118+
return $this;
119+
}
120+
121+
public function getDeleted(): bool
122+
{
123+
return $this->deleted;
124+
}
125+
126+
public function setDeleted(bool $deleted): self
127+
{
128+
$this->deleted = $deleted;
129+
130+
return $this;
131+
}
132+
133+
public function getSize(): ?array
134+
{
135+
return $this->size;
136+
}
137+
138+
public function setSize(?BaseRangeFilter $sizeRange = null, ?int $sizeUnit = null): self
139+
{
140+
$size = null;
141+
142+
if (!is_null($sizeRange)) {
143+
$size = $this->parseIntOrIntRangeFilter($sizeRange);
144+
}
145+
146+
if (!is_null($sizeUnit)) {
147+
$size['unit'] = $sizeUnit;
148+
}
149+
150+
$this->size = $size;
151+
152+
return $this;
153+
}
154+
155+
public function getDate(): ?array
156+
{
157+
return $this->date;
158+
}
159+
160+
public function setDate(
161+
?BaseRangeFilter $dateRange = null,
162+
?string $dateType = null,
163+
?string $datePreset = null
164+
): self {
165+
$date = null;
166+
167+
if (!is_null($dateRange)) {
168+
$date = $this->parseIntOrIntRangeFilter($dateRange);
169+
}
170+
171+
if (!is_null($dateType)) {
172+
$date['type'] = $dateType;
173+
}
174+
175+
if (!is_null($datePreset)) {
176+
$date['date_preset'] = $datePreset;
177+
}
178+
179+
$this->date = $date;
180+
181+
return $this;
182+
}
183+
184+
public function getCreatedBy(): ?array
185+
{
186+
return $this->createdBy;
187+
}
188+
189+
/**
190+
* @param array|int|null $createdBy
191+
* @return FilesFilter
192+
*/
193+
public function setCreatedBy($createdBy): self
194+
{
195+
$this->createdBy = $this->parseArrayOrNumberFilter($createdBy);
196+
197+
return $this;
198+
}
199+
200+
public function getUpdatedBy(): ?array
201+
{
202+
return $this->updatedBy;
203+
}
204+
205+
/**
206+
* @param array|int|null $updatedBy
207+
* @return FilesFilter
208+
*/
209+
public function setUpdatedBy($updatedBy): self
210+
{
211+
$this->updatedBy = $this->parseArrayOrNumberFilter($updatedBy);
212+
213+
return $this;
214+
}
215+
216+
public function buildFilter(): array
217+
{
218+
$filter = [];
219+
220+
if (!is_null($this->getUuid())) {
221+
$filter['filter']['uuid'] = implode(',', $this->getUuid());
222+
}
223+
224+
if (!is_null($this->getName())) {
225+
$filter['filter']['name'] = $this->getName();
226+
}
227+
228+
if (!is_null($this->getExtensions())) {
229+
$filter['filter']['extensions'] = $this->getExtensions();
230+
}
231+
232+
if (!is_null($this->getTerm())) {
233+
$filter['filter']['term'] = $this->getTerm();
234+
}
235+
236+
if (!is_null($this->getSourceId())) {
237+
$filter['filter']['source_id'] = $this->getSourceId();
238+
}
239+
240+
if ($this->getDeleted()) {
241+
$filter['filter']['deleted'] = $this->getDeleted();
242+
}
243+
244+
if (!is_null($this->getSize())) {
245+
$filter['filter']['size'] = $this->getSize();
246+
}
247+
248+
if (!is_null($this->getDate())) {
249+
$filter['filter']['date'] = $this->getDate();
250+
}
251+
252+
if (!is_null($this->getCreatedBy())) {
253+
$filter['filter']['created_by'] = $this->getCreatedBy();
254+
}
255+
256+
if (!is_null($this->getUpdatedBy())) {
257+
$filter['filter']['updated_by'] = $this->getUpdatedBy();
258+
}
259+
260+
return $this->buildPagesFilter($filter);
261+
}
262+
}

0 commit comments

Comments
 (0)