Skip to content

Commit 3859d4b

Browse files
authored
Merge pull request #71 from phiHero/feat/conversations
Add support for conversations
2 parents 2b04944 + 4e6f14b commit 3859d4b

13 files changed

+565
-13
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"require-dev": {
5050
"phpunit/phpunit": "^11.2",
5151
"squizlabs/php_codesniffer": "3.*",
52-
"symfony/http-client": "^5.2"
52+
"symfony/http-client": "^5.2",
53+
"mockery/mockery": "^1.6"
5354
},
5455
"config": {
5556
"optimize-autoloader": true,

src/Client.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Client
7474
*/
7575
public Analytics $analytics;
7676

77+
/**
78+
* @var Conversations
79+
*/
80+
public Conversations $conversations;
81+
7782
/**
7883
* @var ApiCall
7984
*/
@@ -91,17 +96,18 @@ public function __construct(array $config)
9196
$this->config = new Configuration($config);
9297
$this->apiCall = new ApiCall($this->config);
9398

94-
$this->collections = new Collections($this->apiCall);
95-
$this->stopwords = new Stopwords($this->apiCall);
96-
$this->aliases = new Aliases($this->apiCall);
97-
$this->keys = new Keys($this->apiCall);
98-
$this->debug = new Debug($this->apiCall);
99-
$this->metrics = new Metrics($this->apiCall);
100-
$this->health = new Health($this->apiCall);
101-
$this->operations = new Operations($this->apiCall);
102-
$this->multiSearch = new MultiSearch($this->apiCall);
103-
$this->presets = new Presets($this->apiCall);
104-
$this->analytics = new Analytics($this->apiCall);
99+
$this->collections = new Collections($this->apiCall);
100+
$this->stopwords = new Stopwords($this->apiCall);
101+
$this->aliases = new Aliases($this->apiCall);
102+
$this->keys = new Keys($this->apiCall);
103+
$this->debug = new Debug($this->apiCall);
104+
$this->metrics = new Metrics($this->apiCall);
105+
$this->health = new Health($this->apiCall);
106+
$this->operations = new Operations($this->apiCall);
107+
$this->multiSearch = new MultiSearch($this->apiCall);
108+
$this->presets = new Presets($this->apiCall);
109+
$this->analytics = new Analytics($this->apiCall);
110+
$this->conversations = new Conversations($this->apiCall);
105111
}
106112

107113
/**
@@ -191,4 +197,12 @@ public function getAnalytics(): Analytics
191197
{
192198
return $this->analytics;
193199
}
200+
201+
/**
202+
* @return Conversations
203+
*/
204+
public function getConversations(): Conversations
205+
{
206+
return $this->conversations;
207+
}
194208
}

src/Conversation.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class Conversation
10+
*
11+
* @package \Typesense
12+
*/
13+
class Conversation
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $id;
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* Conversation constructor.
27+
*
28+
* @param string $id
29+
* @param ApiCall $apiCall
30+
*/
31+
public function __construct(string $id, ApiCall $apiCall)
32+
{
33+
$this->id = $id;
34+
$this->apiCall = $apiCall;
35+
}
36+
37+
/**
38+
* @param array $params
39+
*
40+
* @return array
41+
* @throws TypesenseClientError|HttpClientException
42+
*/
43+
public function update(array $params): array
44+
{
45+
return $this->apiCall->put($this->endPointPath(), $params);
46+
}
47+
48+
/**
49+
* @return array
50+
* @throws TypesenseClientError|HttpClientException
51+
*/
52+
public function retrieve(): array
53+
{
54+
return $this->apiCall->get($this->endPointPath(), []);
55+
}
56+
57+
/**
58+
* @return array
59+
* @throws TypesenseClientError|HttpClientException
60+
*/
61+
public function delete(): array
62+
{
63+
return $this->apiCall->delete($this->endPointPath());
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function endPointPath(): string
70+
{
71+
return sprintf('%s/%s', Conversations::RESOURCE_PATH, $this->id);
72+
}
73+
}

src/ConversationModel.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class ConversationModel
10+
*
11+
* @package \Typesense
12+
*/
13+
class ConversationModel
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $id;
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* ConversationModel constructor.
27+
*
28+
* @param string $id
29+
* @param ApiCall $apiCall
30+
*/
31+
public function __construct(string $id, ApiCall $apiCall)
32+
{
33+
$this->id = $id;
34+
$this->apiCall = $apiCall;
35+
}
36+
37+
/**
38+
* @param array $params
39+
*
40+
* @return array
41+
* @throws TypesenseClientError|HttpClientException
42+
*/
43+
public function update(array $params): array
44+
{
45+
return $this->apiCall->put($this->endPointPath(), $params);
46+
}
47+
48+
/**
49+
* @return array
50+
* @throws TypesenseClientError|HttpClientException
51+
*/
52+
public function retrieve(): array
53+
{
54+
return $this->apiCall->get($this->endPointPath(), []);
55+
}
56+
57+
/**
58+
* @return array
59+
* @throws TypesenseClientError|HttpClientException
60+
*/
61+
public function delete(): array
62+
{
63+
return $this->apiCall->delete($this->endPointPath());
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function endPointPath(): string
70+
{
71+
return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, $this->id);
72+
}
73+
}

src/ConversationModels.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class ConversationModels
10+
*
11+
* @package \Typesense
12+
*/
13+
class ConversationModels implements \ArrayAccess
14+
{
15+
public const RESOURCE_PATH = '/conversations/models';
16+
17+
/**
18+
* @var ApiCall
19+
*/
20+
private ApiCall $apiCall;
21+
22+
/**
23+
* @var array
24+
*/
25+
private array $models = [];
26+
27+
/**
28+
* ConversationModels constructor.
29+
*
30+
* @param ApiCall $apiCall
31+
*/
32+
public function __construct(ApiCall $apiCall)
33+
{
34+
$this->apiCall = $apiCall;
35+
}
36+
37+
/**
38+
* @param $id
39+
*
40+
* @return mixed
41+
*/
42+
public function __get($id)
43+
{
44+
if (isset($this->{$id})) {
45+
return $this->{$id};
46+
}
47+
if (!isset($this->models[$id])) {
48+
$this->models[$id] = new ConversationModel($id, $this->apiCall);
49+
}
50+
51+
return $this->models[$id];
52+
}
53+
54+
/**
55+
* @param array $params
56+
*
57+
* @return array
58+
* @throws TypesenseClientError|HttpClientException
59+
*/
60+
public function create(array $params): array
61+
{
62+
return $this->apiCall->post(static::RESOURCE_PATH, $params);
63+
}
64+
65+
/**
66+
* @return array
67+
* @throws TypesenseClientError|HttpClientException
68+
*/
69+
public function retrieve(): array
70+
{
71+
return $this->apiCall->get(static::RESOURCE_PATH, []);
72+
}
73+
74+
/**
75+
* @inheritDoc
76+
*/
77+
public function offsetExists($offset): bool
78+
{
79+
return isset($this->models[$offset]);
80+
}
81+
82+
/**
83+
* @inheritDoc
84+
*/
85+
public function offsetGet($offset): ConversationModel
86+
{
87+
if (!isset($this->models[$offset])) {
88+
$this->models[$offset] = new ConversationModel($offset, $this->apiCall);
89+
}
90+
91+
return $this->models[$offset];
92+
}
93+
94+
/**
95+
* @inheritDoc
96+
*/
97+
public function offsetSet($offset, $value): void
98+
{
99+
$this->models[$offset] = $value;
100+
}
101+
102+
/**
103+
* @inheritDoc
104+
*/
105+
public function offsetUnset($offset): void
106+
{
107+
unset($this->models[$offset]);
108+
}
109+
}

0 commit comments

Comments
 (0)