Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
test:
name: Run tests with PHP v8.3
name: Run tests with PHP v8.4
runs-on: ubuntu-latest
steps:
- name: Start Typesense
Expand All @@ -24,10 +24,10 @@ jobs:
--enable-cors

- uses: actions/checkout@v4
- name: Setup PHP 8.3
- name: Setup PHP 8.4
uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
php-version: "8.4"
coverage: xdebug
- uses: php-actions/composer@v6
- name: Run tests
Expand Down
26 changes: 20 additions & 6 deletions src/CurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class CurationSet
*/
private ApiCall $apiCall;

/**
* @var CurationSetItems
*/
private CurationSetItems $items;
private array $typesenseCurationSetItems = [];

/**
* CurationSet constructor.
Expand All @@ -37,7 +34,24 @@ public function __construct(string $curationSetName, ApiCall $apiCall)
{
$this->curationSetName = $curationSetName;
$this->apiCall = $apiCall;
$this->items = new CurationSetItems($curationSetName, $apiCall);
}

/**
* @param $id
*
* @return mixed
*/
public function __get($id)
{
if (isset($this->{$id})) {
return $this->{$id};
}

if (!isset($this->typesenseCurationSetItems[$id])) {
$this->typesenseCurationSetItems[$id] = new CurationSetItems($this->curationSetName, $this->apiCall);
}

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

/**
Expand Down Expand Up @@ -86,6 +100,6 @@ public function delete(): array
*/
public function getItems(): CurationSetItems
{
return $this->items;
return $this->__get('items');
}
}
30 changes: 29 additions & 1 deletion src/SynonymSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SynonymSet
* @var ApiCall
*/
private ApiCall $apiCall;

private array $typesenseSynonymSetItems = [];

/**
* SynonymSet constructor.
Expand All @@ -34,6 +36,24 @@ public function __construct(string $synonymSetName, ApiCall $apiCall)
$this->apiCall = $apiCall;
}

/**
* @param $id
*
* @return mixed
*/
public function __get($id)
{
if (isset($this->{$id})) {
return $this->{$id};
}

if (!isset($this->typesenseSynonymSetItems[$id])) {
$this->typesenseSynonymSetItems[$id] = new SynonymSetItems($this->synonymSetName, $this->apiCall);
}

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

/**
* @return string
*/
Expand Down Expand Up @@ -74,4 +94,12 @@ public function delete(): array
{
return $this->apiCall->delete($this->endPointPath());
}
}

/**
* @return SynonymSetItems
*/
public function getItems(): SynonymSetItems
{
return $this->__get('items');
}
}
85 changes: 85 additions & 0 deletions src/SynonymSetItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Typesense;

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

/**
* Class SynonymSetItem
*
* @package \Typesense
*/
class SynonymSetItem
{
/**
* @var string
*/
private string $synonymSetName;

/**
* @var string
*/
private string $itemId;

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

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

/**
* @return string
*/
private function endPointPath(): string
{
return sprintf(
'%s/%s/items/%s',
SynonymSets::RESOURCE_PATH,
encodeURIComponent($this->synonymSetName),
encodeURIComponent($this->itemId)
);
}

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

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

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function delete(): array
{
return $this->apiCall->delete($this->endPointPath());
}
}
98 changes: 98 additions & 0 deletions src/SynonymSetItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Typesense;

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

/**
* Class SynonymSetItems
*
* @package \Typesense
*/
class SynonymSetItems implements \ArrayAccess
{
/**
* @var string
*/
private string $synonymSetName;

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

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

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

/**
* @return string
*/
private function endPointPath(): string
{
return sprintf(
'%s/%s/items',
SynonymSets::RESOURCE_PATH,
encodeURIComponent($this->synonymSetName)
);
}

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

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

/**
* @inheritDoc
*/
public function offsetGet($itemId): SynonymSetItem
{
if (!isset($this->items[$itemId])) {
$this->items[$itemId] = new SynonymSetItem($this->synonymSetName, $itemId, $this->apiCall);
}

return $this->items[$itemId];
}

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

/**
* @inheritDoc
*/
public function offsetUnset($itemId): void
{
unset($this->items[$itemId]);
}
}
70 changes: 70 additions & 0 deletions tests/Feature/SynonymSetItemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Feature;

use Tests\TestCase;
use Exception;

class SynonymSetItemsTest extends TestCase
{
private $synonymSets = null;
private $synonymSetData = [
'items' => [
[
'id' => 'synonym-rule-1',
'synonyms' => ['foo', 'bar', 'baz'],
'root' => '',
],
],
];

protected function setUp(): void
{
parent::setUp();

if (!$this->isV30OrAbove()) {
$this->markTestSkipped('SynonymSetItems is only supported in Typesense v30+');
}

$this->synonymSets = $this->client()->synonymSets;
$this->synonymSets->upsert('test-synonym-set-items', $this->synonymSetData);
}

protected function tearDown(): void
{
try {
if ($this->synonymSets !== null) {
$this->synonymSets['test-synonym-set-items']->delete();
}
} catch (Exception $e) {
// Ignore cleanup errors
}
parent::tearDown();
}

public function testCanListItemsInASynonymSet(): void
{
$items = $this->synonymSets['test-synonym-set-items']->getItems()->retrieve();

$this->assertIsArray($items);
$this->assertGreaterThan(0, count($items));
$this->assertEquals('foo', $items[0]['synonyms'][0]);
}

public function testCanUpsertRetrieveAndDeleteAnItem(): void
{
$upserted = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->upsert([
'id' => 'synonym-rule-1',
'synonyms' => ['red', 'crimson'],
'root' => '',
]);

$this->assertEquals('synonym-rule-1', $upserted['id']);

$fetched = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->retrieve();
$this->assertEquals('red', $fetched['synonyms'][0]);

$deletion = $this->synonymSets['test-synonym-set-items']->getItems()['synonym-rule-1']->delete();
$this->assertEquals('synonym-rule-1', $deletion['id']);
}
}