Skip to content

Commit

Permalink
retrieve preset by name & presets complies with convention
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Jun 27, 2024
1 parent 5168e5e commit fcbcc3d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 38 deletions.
62 changes: 62 additions & 0 deletions src/Preset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Typesense;

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

/**
* Class Preset
*
* @package \Typesense
*/
class Preset
{
/**
* @var string
*/
private string $presetName;

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

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

/**
* @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', Presets::PRESETS_PATH, $this->presetName);
}
}
83 changes: 64 additions & 19 deletions src/Presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Typesense\Exceptions\TypesenseClientError;

/**
* Class Document
* Class Presets
*
* @package \Typesense
* @date 4/5/20
* @author Abdullah Al-Faqeir <abdullah@devloops.net>
*/
class Presets
class Presets implements \ArrayAccess
{
/**
* @var ApiCall
Expand All @@ -24,7 +24,12 @@ class Presets
public const MULTI_SEARCH_PATH = '/multi_search';

/**
* Document constructor.
* @var array
*/
private array $presets = [];

/**
* Presets constructor.
*
* @param ApiCall $apiCall
*/
Expand All @@ -49,37 +54,24 @@ public function searchWithPreset($presetName)
* @throws HttpClientException
* @throws TypesenseClientError
*/
public function get()
public function retrieve()
{
return $this->apiCall->get(static::PRESETS_PATH, []);
}

/**
* @param string $presetName
* @param array $options
*
* @return array
* @throws HttpClientException
* @throws TypesenseClientError
*/
public function put(array $options = [])
public function upsert(string $presetName, array $presetsData)
{
$presetName = $options['preset_name'];
$presetsData = $options['preset_data'];

return $this->apiCall->put($this->endpointPath($presetName), $presetsData);
}

/**
* @param $presetName
* @return array
* @throws HttpClientException
* @throws TypesenseClientError
*/
public function delete($presetName)
{
return $this->apiCall->delete($this->endpointPath($presetName));
}

/**
* @param $presetsName
* @return string
Expand All @@ -104,4 +96,57 @@ private function multiSearchEndpointPath()
static::MULTI_SEARCH_PATH
);
}

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

return $this->presets[$presetName];
}

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

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

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

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

/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->presets[$offset]);
}
}
35 changes: 16 additions & 19 deletions tests/Feature/PresetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Feature;

use Tests\TestCase;
use Typesense\Exceptions\ObjectNotFound;

class PresetsTest extends TestCase
{
Expand All @@ -14,22 +15,19 @@ protected function setUp(): void
{
parent::setUp();

$returnData = $this->client()->presets->put([
'preset_name' => $this->presetName,
'preset_data' => [
'value' => [
'query_by' => "*",
],
]
$returnData = $this->client()->presets->upsert($this->presetName, [
'value' => [
'query_by' => "*",
],
]);
$this->presetUpsertRes = $returnData;
}

protected function tearDown(): void
{
$presets = $this->client()->presets->get();
$presets = $this->client()->presets->retrieve();
foreach ($presets['presets'] as $preset) {
$this->client()->presets->delete($preset['name']);
$this->client()->presets[$preset['name']]->delete();
}
}

Expand All @@ -38,25 +36,24 @@ public function testCanUpsertAPreset(): void
$this->assertEquals($this->presetName, $this->presetUpsertRes['name']);
}

//* Currently there isn't a method for retrieving a preset by name
// public function testCanRetrieveAPreset(): void
// {
// $returnData = $this->client()->presets->get($this->presetName);
// $this->assertEquals($this->presetName, $returnData['name']);
// }
public function testCanRetrieveAPresetByName(): void
{
$returnData = $this->client()->presets[$this->presetName]->retrieve();
$this->assertEquals($this->presetName, $returnData['name']);
}

public function testCanDeleteAPreset(): void
{
$returnData = $this->client()->presets->delete($this->presetName);
$returnData = $this->client()->presets[$this->presetName]->delete();
$this->assertEquals($this->presetName, $returnData['name']);

$returnPresets = $this->client()->presets->get();
$this->assertCount(0, $returnPresets['presets']);
$this->expectException(ObjectNotFound::class);
$this->client()->presets[$this->presetName]->retrieve();
}

public function testCanRetrieveAllPresets(): void
{
$returnData = $this->client()->presets->get();
$returnData = $this->client()->presets->retrieve();
$this->assertCount(1, $returnData['presets']);
}
}

0 comments on commit fcbcc3d

Please sign in to comment.