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
27 changes: 27 additions & 0 deletions src/AmoCRM/Collections/Talks/TalksCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace AmoCRM\Collections\Talks;

use AmoCRM\Collections\BaseApiCollection;
use AmoCRM\Collections\Interfaces\HasPagesInterface;
use AmoCRM\Collections\Traits\PagesTrait;
use AmoCRM\Models\TalkModel;

/**
* @method TalkModel|null current()
* @method TalkModel|null last()
* @method TalkModel|null first()
* @method TalkModel|null offsetGet($offset)
* @method void offsetSet($offset, TalkModel $value)
* @method TalksCollection prepend(TalkModel $value)
* @method TalksCollection add(TalkModel $value)
* @method TalkModel|null getBy($key, $value)
*/
class TalksCollection extends BaseApiCollection implements HasPagesInterface
{
use PagesTrait;

public const ITEM_CLASS = TalkModel::class;
}
16 changes: 10 additions & 6 deletions src/AmoCRM/EntitiesServices/Talks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use AmoCRM\Client\AmoCRMApiClient;
use AmoCRM\Client\AmoCRMApiRequest;
use AmoCRM\Collections\BaseApiCollection;
use AmoCRM\Collections\Talks\TalksCollection;
use AmoCRM\EntitiesServices\Interfaces\HasPageMethodsInterface;
use AmoCRM\EntitiesServices\Traits\PageMethodsTrait;
use AmoCRM\Exceptions\AmoCRMApiException;
use AmoCRM\Exceptions\AmoCRMoAuthApiException;
use AmoCRM\Exceptions\NotAvailableForActionException;
Expand All @@ -18,14 +21,20 @@

/**
* @method TalkModel|null getOne($id, array $with = [])
* @method TalksCollection|null get(?BaseEntityFilter $filter = null, array $with = [])
*/
class Talks extends BaseEntity
class Talks extends BaseEntity implements HasPageMethodsInterface
{
use PageMethodsTrait;

public const ITEM_CLASS = TalkModel::class;

/** @var string */
protected $method = 'api/v' . AmoCRMApiClient::API_VERSION . '/' . EntityTypesInterface::TALKS;

/** @var string */
protected $collectionClass = TalksCollection::class;

/**
* @param array $response
*
Expand All @@ -36,11 +45,6 @@ protected function getEntitiesFromResponse(array $response): array
return $response[AmoCRMApiRequest::EMBEDDED][EntityTypesInterface::TALKS] ?? [];
}

public function get(?BaseEntityFilter $filter = null, array $with = []): ?BaseApiCollection
{
throw new NotAvailableForActionException('Method not available for this entity');
}

/**
* @param BaseApiModel $model
*
Expand Down
143 changes: 143 additions & 0 deletions src/AmoCRM/Filters/TalksFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

declare(strict_types=1);

namespace AmoCRM\Filters;

use AmoCRM\Filters\Interfaces\HasPagesInterface;
use AmoCRM\Filters\Traits\ArrayOrNumericFilterTrait;
use AmoCRM\Filters\Traits\PagesFilterTrait;

use function is_null;

class TalksFilter extends BaseEntityFilter implements HasPagesInterface
{
use PagesFilterTrait;
use ArrayOrNumericFilterTrait;

/** @var array<int, int>|null */
private $talkIds = null;

/** @var array<int, int>|null */
private $entityIds = null;

/** @var string|null */
private $entityType = null;

/** @var array<int, int>|null */
private $contactIds = null;

/** @var bool */
private $onlyInWork = false;

/**
* @return array<int, int>|null
*/
public function getTalkIds(): ?array
{
return $this->talkIds;
}

/**
* @param array<int, int>|int $talkIds
*
* @return self
*/
public function setTalkIds($talkIds): self
{
$this->talkIds = $this->parseArrayOrNumberFilter($talkIds);

return $this;
}

/**
* @return array<int, int>|null
*/
public function getEntityIds(): ?array
{
return $this->entityIds;
}

/**
* @param array<int, int>|int $entityIds
*
* @return self
*/
public function setEntityIds($entityIds): self
{
$this->entityIds = $this->parseArrayOrNumberFilter($entityIds);

return $this;
}

public function getEntityType(): ?string
{
return $this->entityType;
}

public function setEntityType(string $entityType): self
{
$this->entityType = $entityType;

return $this;
}

/**
* @return array<int, int>|null
*/
public function getContactIds(): ?array
{
return $this->contactIds;
}

/**
* @param array<int, int>|int $contactIds
*
* @return self
*/
public function setContactIds($contactIds): self
{
$this->contactIds = $this->parseArrayOrNumberFilter($contactIds);

return $this;
}

public function getOnlyInWork(): bool
{
return $this->onlyInWork;
}

public function setOnlyInWork(bool $onlyInWork): self
{
$this->onlyInWork = $onlyInWork;

return $this;
}

public function buildFilter(): array
{
$filter = [];

if (!is_null($this->getTalkIds())) {
$filter['filter']['talk_id'] = $this->getTalkIds();
}

if (!is_null($this->getEntityIds())) {
$filter['filter']['entity_id'] = $this->getEntityIds();
}

if (!is_null($this->getEntityType())) {
$filter['filter']['entity_type'] = $this->getEntityType();
}

if (!is_null($this->getContactIds())) {
$filter['filter']['contact_id'] = $this->getContactIds();
}

if ($this->getOnlyInWork()) {
$filter['filter']['only_in_work'] = true;
}

return $this->buildPagesFilter($filter);
}
}
42 changes: 40 additions & 2 deletions src/AmoCRM/Models/TalkModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

class TalkModel extends BaseApiModel
{
public const STATUS_IN_WORK = 'in_work';
public const STATUS_CLOSED = 'closed';
public const STATUS_WITH_ERROR = 'with_error';
public const STATUS_NPS_SCHEDULED = 'nps_scheduled';
public const STATUS_NPS_IN_PROGRESS = 'nps_in_progress';

/** @var int */
protected $talkId;
/** @var int */
Expand All @@ -24,13 +30,17 @@ class TalkModel extends BaseApiModel
protected $entityId;
/** @var string|null */
protected $entityType;
/** @var string|null */
protected $status;
/** @var bool */
protected $isInWork;
/** @var bool */
protected $isRead;
/** @var string */
protected $origin;
/** @var int|null */
protected $sourceId;
/** @var int|null */
protected $missedAt;
/** @var int */
protected $accountId;
Expand All @@ -49,11 +59,13 @@ public static function fromArray(array $talk): self
->setRate((int)$talk['rate'])
->setContactId((int)$talk['contact_id'])
->setChatId(empty($talk['chat_id']) ? null : (string)$talk['chat_id'])
->setEntityId(empty($talk['entity_id']) ?: (int)$talk['entity_id'])
->setEntityType(empty($talk['entity_type']) ?: (string)$talk['entity_type'])
->setEntityId(empty($talk['entity_id']) ? null : (int)$talk['entity_id'])
->setEntityType(empty($talk['entity_type']) ? null : (string)$talk['entity_type'])
->setStatus($talk['status'] ?? null)
->setIsInWork(!empty($talk['is_in_work']))
->setIsRead(!empty($talk['is_read']))
->setOrigin((string)($talk['origin'] ?? ''))
->setSourceId(($talk['source_id'] ?? null) !== null ? (int)$talk['source_id'] : null)
->setMissedAt(empty($talk['missed_at']) ? null : (int)$talk['missed_at'])
->setAccountId((int)$talk['account_id']);
}
Expand Down Expand Up @@ -154,6 +166,18 @@ public function setEntityType(?string $entityType): self
return $this;
}

public function getStatus(): ?string
{
return $this->status;
}

public function setStatus(?string $status): self
{
$this->status = $status;

return $this;
}

public function isInWork(): bool
{
return $this->isInWork;
Expand Down Expand Up @@ -190,6 +214,18 @@ public function setOrigin(string $origin): self
return $this;
}

public function getSourceId(): ?int
{
return $this->sourceId;
}

public function setSourceId(?int $sourceId): self
{
$this->sourceId = $sourceId;

return $this;
}

public function getMissedAt(): ?int
{
return $this->missedAt;
Expand Down Expand Up @@ -228,9 +264,11 @@ public function toArray(): array
'chat_id' => $this->getChatId(),
'entity_id' => $this->getEntityId(),
'entity_type' => $this->getEntityType(),
'status' => $this->getStatus(),
'is_in_work' => $this->isInWork(),
'is_read' => $this->isRead(),
'origin' => $this->getOrigin(),
'source_id' => $this->getSourceId(),
'missed_at' => $this->getMissedAt(),
'account_id' => $this->getAccountId(),
];
Expand Down