-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
475 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Vonage\Conversation; | ||
|
||
use Vonage\Client\APIClient; | ||
use Vonage\Client\APIResource; | ||
use Vonage\Conversation\ConversationObjects\Conversation; | ||
use Vonage\Conversation\Filter\ListConversationFilter; | ||
use Vonage\Entity\Hydrator\ArrayHydrator; | ||
use Vonage\Entity\IterableAPICollection; | ||
|
||
class Client implements APIClient | ||
{ | ||
public function __construct(protected APIResource $api) | ||
{ | ||
} | ||
|
||
public function getAPIResource(): APIResource | ||
{ | ||
return $this->api; | ||
} | ||
|
||
public function listConversations(ListConversationFilter $conversationFilter = null): IterableAPICollection | ||
{ | ||
if (!$conversationFilter) { | ||
$conversationFilter = new ListConversationFilter(); | ||
} | ||
$response = $this->api->search($conversationFilter); | ||
$response->setHasPagination(false); | ||
$response->setNaiveCount(true); | ||
|
||
$hydrator = new ArrayHydrator(); | ||
$hydrator->setPrototype(new Conversation()); | ||
$response->setHydrator($hydrator); | ||
|
||
return $response; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Vonage\Conversation; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Vonage\Client\APIResource; | ||
use Vonage\Client\Credentials\Handler\KeypairHandler; | ||
use Vonage\Verify2\Client; | ||
|
||
class ClientFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): Client | ||
{ | ||
$api = $container->make(APIResource::class); | ||
$api->setIsHAL(true) | ||
->setErrorsOn200(false) | ||
->setAuthHandler(new KeypairHandler()) | ||
->setBaseUrl('https://api.nexmo.com/v1/conversations'); | ||
|
||
return new Client($api); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vonage\Conversation\ConversationObjects; | ||
|
||
use Vonage\Entity\Hydrator\ArrayHydrateInterface; | ||
|
||
class Conversation implements ArrayHydrateInterface | ||
{ | ||
protected array $data; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function fromArray(array $data): void | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function __get(string $name) | ||
{ | ||
return $this->data[$name]; | ||
} | ||
|
||
public function __set(string $name, $value) | ||
{ | ||
$this->data[$name] = $value; | ||
} | ||
|
||
public function __isset(string $name): bool | ||
{ | ||
return isset($this->data[$name]); | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vonage\Conversation\Filter; | ||
|
||
use http\Exception\InvalidArgumentException; | ||
use Vonage\Entity\Filter\FilterInterface; | ||
|
||
class ListConversationFilter implements FilterInterface | ||
{ | ||
protected ?string $startDate = null; | ||
protected ?string $endDate = null; | ||
protected int $pageSize = 10; | ||
protected string $order = 'asc'; | ||
private const VALID_ORDERS = ['asc', 'ASC', 'desc', 'DESC']; | ||
protected ?string $cursor = null; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function getStartDate(): ?string | ||
{ | ||
return $this->startDate; | ||
} | ||
|
||
public function setStartDate(?string $startDate): ListConversationFilter | ||
{ | ||
$this->startDate = $startDate; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEndDate(): ?string | ||
{ | ||
return $this->endDate; | ||
} | ||
|
||
public function setEndDate(?string $endDate): ListConversationFilter | ||
{ | ||
$this->endDate = $endDate; | ||
|
||
return $this; | ||
} | ||
|
||
public function getPageSize(): ?int | ||
{ | ||
return $this->pageSize; | ||
} | ||
|
||
public function setPageSize(?int $pageSize): ListConversationFilter | ||
{ | ||
$this->pageSize = $pageSize; | ||
|
||
return $this; | ||
} | ||
|
||
public function getOrder(): string | ||
{ | ||
return $this->order; | ||
} | ||
|
||
public function setOrder(string $order): ListConversationFilter | ||
{ | ||
if (!in_array($order, self::VALID_ORDERS)) { | ||
throw new \InvalidArgumentException($order . ' is an invalid order value'); | ||
} | ||
|
||
$this->order = $order; | ||
|
||
return $this; | ||
} | ||
|
||
public function setCursor(?string $cursor): ListConversationFilter | ||
{ | ||
$this->cursor = $cursor; | ||
|
||
return $this; | ||
} | ||
|
||
public function getQuery(): array | ||
{ | ||
$query = []; | ||
|
||
if ($this->getStartDate()) { | ||
$query['date_start'] = $this->getStartDate(); | ||
} | ||
|
||
if ($this->getEndDate()) { | ||
$query['date_end'] = $this->getEndDate(); | ||
} | ||
|
||
if ($this->getPageSize()) { | ||
$query['page_size'] = $this->getPageSize(); | ||
} | ||
|
||
$query['order'] = $this->getOrder(); | ||
|
||
return $query; | ||
} | ||
} |
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
Oops, something went wrong.