Skip to content

Commit eb32032

Browse files
authored
Merge pull request #72 from phiHero/master
retrieve preset by name, implement array access for presets, analyticsRules
2 parents 313d934 + fcbcc3d commit eb32032

File tree

5 files changed

+182
-42
lines changed

5 files changed

+182
-42
lines changed

src/AnalyticsRules.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Typesense;
44

5-
class AnalyticsRules
5+
class AnalyticsRules implements \ArrayAccess
66
{
77
const RESOURCE_PATH = '/analytics/rules';
88

@@ -36,4 +36,40 @@ private function endpoint_path($operation = null)
3636
{
3737
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
3838
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function offsetExists($offset): bool
44+
{
45+
return isset($this->analyticsRules[$offset]);
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function offsetGet($offset): AnalyticsRule
52+
{
53+
if (!isset($this->analyticsRules[$offset])) {
54+
$this->analyticsRules[$offset] = new AnalyticsRule($offset, $this->apiCall);
55+
}
56+
57+
return $this->analyticsRules[$offset];
58+
}
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function offsetSet($offset, $value): void
64+
{
65+
$this->analyticsRules[$offset] = $value;
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function offsetUnset($offset): void
72+
{
73+
unset($this->analyticsRules[$offset]);
74+
}
3975
}

src/Preset.php

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

src/Presets.php

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use Typesense\Exceptions\TypesenseClientError;
77

88
/**
9-
* Class Document
9+
* Class Presets
1010
*
1111
* @package \Typesense
1212
* @date 4/5/20
1313
* @author Abdullah Al-Faqeir <[email protected]>
1414
*/
15-
class Presets
15+
class Presets implements \ArrayAccess
1616
{
1717
/**
1818
* @var ApiCall
@@ -24,7 +24,12 @@ class Presets
2424
public const MULTI_SEARCH_PATH = '/multi_search';
2525

2626
/**
27-
* Document constructor.
27+
* @var array
28+
*/
29+
private array $presets = [];
30+
31+
/**
32+
* Presets constructor.
2833
*
2934
* @param ApiCall $apiCall
3035
*/
@@ -49,37 +54,24 @@ public function searchWithPreset($presetName)
4954
* @throws HttpClientException
5055
* @throws TypesenseClientError
5156
*/
52-
public function get()
57+
public function retrieve()
5358
{
5459
return $this->apiCall->get(static::PRESETS_PATH, []);
5560
}
5661

5762
/**
63+
* @param string $presetName
5864
* @param array $options
5965
*
6066
* @return array
6167
* @throws HttpClientException
6268
* @throws TypesenseClientError
6369
*/
64-
public function put(array $options = [])
70+
public function upsert(string $presetName, array $presetsData)
6571
{
66-
$presetName = $options['preset_name'];
67-
$presetsData = $options['preset_data'];
68-
6972
return $this->apiCall->put($this->endpointPath($presetName), $presetsData);
7073
}
7174

72-
/**
73-
* @param $presetName
74-
* @return array
75-
* @throws HttpClientException
76-
* @throws TypesenseClientError
77-
*/
78-
public function delete($presetName)
79-
{
80-
return $this->apiCall->delete($this->endpointPath($presetName));
81-
}
82-
8375
/**
8476
* @param $presetsName
8577
* @return string
@@ -104,4 +96,57 @@ private function multiSearchEndpointPath()
10496
static::MULTI_SEARCH_PATH
10597
);
10698
}
99+
100+
/**
101+
* @param $presetName
102+
*
103+
* @return mixed
104+
*/
105+
public function __get($presetName)
106+
{
107+
if (isset($this->{$presetName})) {
108+
return $this->{$presetName};
109+
}
110+
if (!isset($this->presets[$presetName])) {
111+
$this->presets[$presetName] = new Preset($presetName, $this->apiCall);
112+
}
113+
114+
return $this->presets[$presetName];
115+
}
116+
117+
/**
118+
* @inheritDoc
119+
*/
120+
public function offsetExists($offset): bool
121+
{
122+
return isset($this->presets[$offset]);
123+
}
124+
125+
/**
126+
* @inheritDoc
127+
*/
128+
public function offsetGet($offset): Preset
129+
{
130+
if (!isset($this->presets[$offset])) {
131+
$this->presets[$offset] = new Preset($offset, $this->apiCall);
132+
}
133+
134+
return $this->presets[$offset];
135+
}
136+
137+
/**
138+
* @inheritDoc
139+
*/
140+
public function offsetSet($offset, $value): void
141+
{
142+
$this->presets[$offset] = $value;
143+
}
144+
145+
/**
146+
* @inheritDoc
147+
*/
148+
public function offsetUnset($offset): void
149+
{
150+
unset($this->presets[$offset]);
151+
}
107152
}

tests/Feature/AnalyticsRulesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public function testCanUpsertARule(): void
4444

4545
public function testCanRetrieveARule(): void
4646
{
47-
$returnData = $this->client()->analytics->rules()->{$this->ruleName}->retrieve();
47+
$returnData = $this->client()->analytics->rules()[$this->ruleName]->retrieve();
4848
$this->assertEquals($returnData['name'], $this->ruleName);
4949
}
5050

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

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

6060
public function testCanRetrieveAllRules(): void

tests/Feature/PresetsTest.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Feature;
44

55
use Tests\TestCase;
6+
use Typesense\Exceptions\ObjectNotFound;
67

78
class PresetsTest extends TestCase
89
{
@@ -14,22 +15,19 @@ protected function setUp(): void
1415
{
1516
parent::setUp();
1617

17-
$returnData = $this->client()->presets->put([
18-
'preset_name' => $this->presetName,
19-
'preset_data' => [
20-
'value' => [
21-
'query_by' => "*",
22-
],
23-
]
18+
$returnData = $this->client()->presets->upsert($this->presetName, [
19+
'value' => [
20+
'query_by' => "*",
21+
],
2422
]);
2523
$this->presetUpsertRes = $returnData;
2624
}
2725

2826
protected function tearDown(): void
2927
{
30-
$presets = $this->client()->presets->get();
28+
$presets = $this->client()->presets->retrieve();
3129
foreach ($presets['presets'] as $preset) {
32-
$this->client()->presets->delete($preset['name']);
30+
$this->client()->presets[$preset['name']]->delete();
3331
}
3432
}
3533

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

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

4845
public function testCanDeleteAPreset(): void
4946
{
50-
$returnData = $this->client()->presets->delete($this->presetName);
47+
$returnData = $this->client()->presets[$this->presetName]->delete();
5148
$this->assertEquals($this->presetName, $returnData['name']);
5249

53-
$returnPresets = $this->client()->presets->get();
54-
$this->assertCount(0, $returnPresets['presets']);
50+
$this->expectException(ObjectNotFound::class);
51+
$this->client()->presets[$this->presetName]->retrieve();
5552
}
5653

5754
public function testCanRetrieveAllPresets(): void
5855
{
59-
$returnData = $this->client()->presets->get();
56+
$returnData = $this->client()->presets->retrieve();
6057
$this->assertCount(1, $returnData['presets']);
6158
}
6259
}

0 commit comments

Comments
 (0)