Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrieve preset by name, implement array access for presets, analyticsRules #72

Merged
merged 2 commits into from
Oct 7, 2024
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
38 changes: 37 additions & 1 deletion src/AnalyticsRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Typesense;

class AnalyticsRules
class AnalyticsRules implements \ArrayAccess
{
const RESOURCE_PATH = '/analytics/rules';

Expand Down Expand Up @@ -36,4 +36,40 @@ private function endpoint_path($operation = null)
{
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
}

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

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

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

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

/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->analyticsRules[$offset]);
}
}
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 <[email protected]>
*/
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]);
}
}
6 changes: 3 additions & 3 deletions tests/Feature/AnalyticsRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public function testCanUpsertARule(): void

public function testCanRetrieveARule(): void
{
$returnData = $this->client()->analytics->rules()->{$this->ruleName}->retrieve();
$returnData = $this->client()->analytics->rules()[$this->ruleName]->retrieve();
$this->assertEquals($returnData['name'], $this->ruleName);
}

public function testCanDeleteARule(): void
{
$returnData = $this->client()->analytics->rules()->{$this->ruleName}->delete();
$returnData = $this->client()->analytics->rules()[$this->ruleName]->delete();
$this->assertEquals($returnData['name'], $this->ruleName);

$this->expectException(ObjectNotFound::class);
$this->client()->analytics->rules()->{$this->ruleName}->retrieve();
$this->client()->analytics->rules()[$this->ruleName]->retrieve();
}

public function testCanRetrieveAllRules(): void
Expand Down
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']);
}
}