Skip to content

Commit 089d8a2

Browse files
committed
feat: add SynonymSet and SynonymSets classes for global synonym management
1 parent 1fbf79e commit 089d8a2

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

src/SynonymSet.php

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

src/SynonymSets.php

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

0 commit comments

Comments
 (0)