Skip to content

Commit 5168e5e

Browse files
committed
analyticsRules array access
1 parent 2b04944 commit 5168e5e

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
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 ? '' : "/$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
}

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

0 commit comments

Comments
 (0)