Skip to content

Commit 3bc39c9

Browse files
committed
feat(synonyms): add support for managing synonym set items
1 parent a5da1d9 commit 3bc39c9

4 files changed

Lines changed: 268 additions & 1 deletion

File tree

src/SynonymSet.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class SynonymSet
2121
* @var ApiCall
2222
*/
2323
private ApiCall $apiCall;
24+
25+
/**
26+
* @var SynonymSetItems
27+
*/
28+
private SynonymSetItems $items;
2429

2530
/**
2631
* SynonymSet constructor.
@@ -32,6 +37,7 @@ public function __construct(string $synonymSetName, ApiCall $apiCall)
3237
{
3338
$this->synonymSetName = $synonymSetName;
3439
$this->apiCall = $apiCall;
40+
$this->items = new SynonymSetItems($synonymSetName, $apiCall);
3541
}
3642

3743
/**
@@ -74,4 +80,12 @@ public function delete(): array
7480
{
7581
return $this->apiCall->delete($this->endPointPath());
7682
}
77-
}
83+
84+
/**
85+
* @return SynonymSetItems
86+
*/
87+
public function getItems(): SynonymSetItems
88+
{
89+
return $this->items;
90+
}
91+
}

src/SynonymSetItem.php

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

src/SynonymSetItems.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class SynonymSetItems
10+
*
11+
* @package \Typesense
12+
*/
13+
class SynonymSetItems implements \ArrayAccess
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $synonymSetName;
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* @var array
27+
*/
28+
private array $items = [];
29+
30+
/**
31+
* SynonymSetItems constructor.
32+
*
33+
* @param string $synonymSetName
34+
* @param ApiCall $apiCall
35+
*/
36+
public function __construct(string $synonymSetName, ApiCall $apiCall)
37+
{
38+
$this->synonymSetName = $synonymSetName;
39+
$this->apiCall = $apiCall;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
private function endPointPath(): string
46+
{
47+
return sprintf(
48+
'%s/%s/items',
49+
SynonymSets::RESOURCE_PATH,
50+
encodeURIComponent($this->synonymSetName)
51+
);
52+
}
53+
54+
/**
55+
* @return array
56+
* @throws TypesenseClientError|HttpClientException
57+
*/
58+
public function retrieve(): array
59+
{
60+
return $this->apiCall->get($this->endPointPath(), []);
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function offsetExists($itemId): bool
67+
{
68+
return isset($this->items[$itemId]);
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function offsetGet($itemId): SynonymSetItem
75+
{
76+
if (!isset($this->items[$itemId])) {
77+
$this->items[$itemId] = new SynonymSetItem($this->synonymSetName, $itemId, $this->apiCall);
78+
}
79+
80+
return $this->items[$itemId];
81+
}
82+
83+
/**
84+
* @inheritDoc
85+
*/
86+
public function offsetSet($itemId, $value): void
87+
{
88+
$this->items[$itemId] = $value;
89+
}
90+
91+
/**
92+
* @inheritDoc
93+
*/
94+
public function offsetUnset($itemId): void
95+
{
96+
unset($this->items[$itemId]);
97+
}
98+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Tests\TestCase;
6+
use Exception;
7+
8+
class SynonymSetItemsTest extends TestCase
9+
{
10+
private $synonymSets = null;
11+
private $synonymSetData = [
12+
'items' => [
13+
[
14+
'id' => 'synonym-rule-1',
15+
'synonyms' => ['foo', 'bar', 'baz'],
16+
'root' => '',
17+
],
18+
],
19+
];
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
if (!$this->isV30OrAbove()) {
26+
$this->markTestSkipped('SynonymSetItems is only supported in Typesense v30+');
27+
}
28+
29+
$this->synonymSets = $this->client()->synonymSets;
30+
$this->synonymSets->upsert('test-synonym-set-items', $this->synonymSetData);
31+
}
32+
33+
protected function tearDown(): void
34+
{
35+
try {
36+
if ($this->synonymSets !== null) {
37+
$this->synonymSets['test-synonym-set-items']->delete();
38+
}
39+
} catch (Exception $e) {
40+
// Ignore cleanup errors
41+
}
42+
parent::tearDown();
43+
}
44+
45+
public function testCanListItemsInASynonymSet(): void
46+
{
47+
$items = $this->synonymSets['test-synonym-set-items']->getItems()->retrieve();
48+
49+
$this->assertIsArray($items);
50+
$this->assertGreaterThan(0, count($items));
51+
$this->assertEquals('foo', $items[0]['synonyms'][0]);
52+
}
53+
54+
public function testCanUpsertRetrieveAndDeleteAnItem(): void
55+
{
56+
$upserted = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->upsert([
57+
'id' => 'synonym-rule-1',
58+
'synonyms' => ['red', 'crimson'],
59+
'root' => '',
60+
]);
61+
62+
$this->assertEquals('synonym-rule-1', $upserted['id']);
63+
64+
$fetched = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->retrieve();
65+
$this->assertEquals('red', $fetched['synonyms'][0]);
66+
67+
$deletion = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->delete();
68+
$this->assertEquals('synonym-rule-1', $deletion['id']);
69+
}
70+
}

0 commit comments

Comments
 (0)