Skip to content

Commit

Permalink
Merge pull request #71 from phiHero/feat/conversations
Browse files Browse the repository at this point in the history
Add support for conversations
  • Loading branch information
jasonbosco committed Jun 26, 2024
2 parents 2b04944 + 4e6f14b commit 3859d4b
Show file tree
Hide file tree
Showing 13 changed files with 565 additions and 13 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"require-dev": {
"phpunit/phpunit": "^11.2",
"squizlabs/php_codesniffer": "3.*",
"symfony/http-client": "^5.2"
"symfony/http-client": "^5.2",
"mockery/mockery": "^1.6"
},
"config": {
"optimize-autoloader": true,
Expand Down
36 changes: 25 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Client
*/
public Analytics $analytics;

/**
* @var Conversations
*/
public Conversations $conversations;

/**
* @var ApiCall
*/
Expand All @@ -91,17 +96,18 @@ public function __construct(array $config)
$this->config = new Configuration($config);
$this->apiCall = new ApiCall($this->config);

$this->collections = new Collections($this->apiCall);
$this->stopwords = new Stopwords($this->apiCall);
$this->aliases = new Aliases($this->apiCall);
$this->keys = new Keys($this->apiCall);
$this->debug = new Debug($this->apiCall);
$this->metrics = new Metrics($this->apiCall);
$this->health = new Health($this->apiCall);
$this->operations = new Operations($this->apiCall);
$this->multiSearch = new MultiSearch($this->apiCall);
$this->presets = new Presets($this->apiCall);
$this->analytics = new Analytics($this->apiCall);
$this->collections = new Collections($this->apiCall);
$this->stopwords = new Stopwords($this->apiCall);
$this->aliases = new Aliases($this->apiCall);
$this->keys = new Keys($this->apiCall);
$this->debug = new Debug($this->apiCall);
$this->metrics = new Metrics($this->apiCall);
$this->health = new Health($this->apiCall);
$this->operations = new Operations($this->apiCall);
$this->multiSearch = new MultiSearch($this->apiCall);
$this->presets = new Presets($this->apiCall);
$this->analytics = new Analytics($this->apiCall);
$this->conversations = new Conversations($this->apiCall);
}

/**
Expand Down Expand Up @@ -191,4 +197,12 @@ public function getAnalytics(): Analytics
{
return $this->analytics;
}

/**
* @return Conversations
*/
public function getConversations(): Conversations
{
return $this->conversations;
}
}
73 changes: 73 additions & 0 deletions src/Conversation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Typesense;

use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;

/**
* Class Conversation
*
* @package \Typesense
*/
class Conversation
{
/**
* @var string
*/
private string $id;

/**
* @var ApiCall
*/
private ApiCall $apiCall;

/**
* Conversation constructor.
*
* @param string $id
* @param ApiCall $apiCall
*/
public function __construct(string $id, ApiCall $apiCall)
{
$this->id = $id;
$this->apiCall = $apiCall;
}

/**
* @param array $params
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function update(array $params): array
{
return $this->apiCall->put($this->endPointPath(), $params);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get($this->endPointPath(), []);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function delete(): array
{
return $this->apiCall->delete($this->endPointPath());
}

/**
* @return string
*/
public function endPointPath(): string
{
return sprintf('%s/%s', Conversations::RESOURCE_PATH, $this->id);
}
}
73 changes: 73 additions & 0 deletions src/ConversationModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Typesense;

use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;

/**
* Class ConversationModel
*
* @package \Typesense
*/
class ConversationModel
{
/**
* @var string
*/
private string $id;

/**
* @var ApiCall
*/
private ApiCall $apiCall;

/**
* ConversationModel constructor.
*
* @param string $id
* @param ApiCall $apiCall
*/
public function __construct(string $id, ApiCall $apiCall)
{
$this->id = $id;
$this->apiCall = $apiCall;
}

/**
* @param array $params
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function update(array $params): array
{
return $this->apiCall->put($this->endPointPath(), $params);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get($this->endPointPath(), []);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function delete(): array
{
return $this->apiCall->delete($this->endPointPath());
}

/**
* @return string
*/
public function endPointPath(): string
{
return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, $this->id);
}
}
109 changes: 109 additions & 0 deletions src/ConversationModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Typesense;

use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;

/**
* Class ConversationModels
*
* @package \Typesense
*/
class ConversationModels implements \ArrayAccess
{
public const RESOURCE_PATH = '/conversations/models';

/**
* @var ApiCall
*/
private ApiCall $apiCall;

/**
* @var array
*/
private array $models = [];

/**
* ConversationModels constructor.
*
* @param ApiCall $apiCall
*/
public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}

/**
* @param $id
*
* @return mixed
*/
public function __get($id)
{
if (isset($this->{$id})) {
return $this->{$id};
}
if (!isset($this->models[$id])) {
$this->models[$id] = new ConversationModel($id, $this->apiCall);
}

return $this->models[$id];
}

/**
* @param array $params
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function create(array $params): array
{
return $this->apiCall->post(static::RESOURCE_PATH, $params);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get(static::RESOURCE_PATH, []);
}

/**
* @inheritDoc
*/
public function offsetExists($offset): bool
{
return isset($this->models[$offset]);
}

/**
* @inheritDoc
*/
public function offsetGet($offset): ConversationModel
{
if (!isset($this->models[$offset])) {
$this->models[$offset] = new ConversationModel($offset, $this->apiCall);
}

return $this->models[$offset];
}

/**
* @inheritDoc
*/
public function offsetSet($offset, $value): void
{
$this->models[$offset] = $value;
}

/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->models[$offset]);
}
}
Loading

0 comments on commit 3859d4b

Please sign in to comment.