Skip to content

Commit 5aa6daf

Browse files
committed
feat(analytics): add analytics v2 classes
1 parent 6eced84 commit 5aa6daf

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

src/AnalyticsEventsV2.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
/**
6+
* Class AnalyticsEventsV2
7+
*
8+
* Implements the updated analytics events API for Typesense v2+
9+
*
10+
* @package \Typesense
11+
*/
12+
class AnalyticsEventsV2
13+
{
14+
const RESOURCE_PATH = '/analytics/events';
15+
16+
private ApiCall $apiCall;
17+
18+
public function __construct(ApiCall $apiCall)
19+
{
20+
$this->apiCall = $apiCall;
21+
}
22+
23+
/**
24+
* Create an analytics event
25+
*
26+
* @param array $params Event parameters including name, event_type, and data
27+
* @return array Response from the API
28+
* @throws TypesenseClientError|HttpClientException
29+
*/
30+
public function create(array $params)
31+
{
32+
return $this->apiCall->post(self::RESOURCE_PATH, $params);
33+
}
34+
35+
/**
36+
* Retrieve analytics events
37+
*
38+
* @param array $params Query parameters
39+
* @return array Response from the API
40+
*/
41+
public function retrieve(array $params = [])
42+
{
43+
return $this->apiCall->get(self::RESOURCE_PATH, $params);
44+
}
45+
}

src/AnalyticsRuleV2.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
class AnalyticsRuleV2
6+
{
7+
private string $ruleName;
8+
private ApiCall $apiCall;
9+
10+
public function __construct(string $ruleName, ApiCall $apiCall)
11+
{
12+
$this->ruleName = $ruleName;
13+
$this->apiCall = $apiCall;
14+
}
15+
16+
/**
17+
* Retrieve a specific analytics rule
18+
*
19+
* @return array Response from the API
20+
*/
21+
public function retrieve()
22+
{
23+
return $this->apiCall->get($this->endpointPath(), []);
24+
}
25+
26+
/**
27+
* Delete a specific analytics rule
28+
*
29+
* @return array Response from the API
30+
*/
31+
public function delete()
32+
{
33+
return $this->apiCall->delete($this->endpointPath());
34+
}
35+
36+
/**
37+
* Update a specific analytics rule
38+
*
39+
* @param array $params Rule parameters
40+
* @return array Response from the API
41+
*/
42+
public function update(array $params)
43+
{
44+
return $this->apiCall->put($this->endpointPath(), $params);
45+
}
46+
47+
private function endpointPath()
48+
{
49+
return AnalyticsRulesV2::RESOURCE_PATH . '/' . encodeURIComponent($this->ruleName);
50+
}
51+
}

src/AnalyticsRulesV2.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
class AnalyticsRulesV2 implements \ArrayAccess
6+
{
7+
const RESOURCE_PATH = '/analytics/rules';
8+
9+
private ApiCall $apiCall;
10+
11+
public function __construct(ApiCall $apiCall)
12+
{
13+
$this->apiCall = $apiCall;
14+
}
15+
16+
/**
17+
* Create multiple analytics rules
18+
*
19+
* @param array $rules Array of rule objects
20+
* @return array Response from the API
21+
*/
22+
public function create(array $rules)
23+
{
24+
return $this->apiCall->post(self::RESOURCE_PATH, $rules);
25+
}
26+
27+
/**
28+
* Retrieve all analytics rules
29+
*
30+
* @return array Response from the API
31+
*/
32+
public function retrieve()
33+
{
34+
return $this->apiCall->get(self::RESOURCE_PATH, []);
35+
}
36+
37+
/**
38+
* Get a specific rule by name
39+
*
40+
* @param string $ruleName
41+
* @return AnalyticsRuleV2
42+
*/
43+
public function __get($ruleName)
44+
{
45+
return new AnalyticsRuleV2($ruleName, $this->apiCall);
46+
}
47+
48+
/**
49+
* ArrayAccess implementation for backwards compatibility
50+
*/
51+
public function offsetExists($offset): bool
52+
{
53+
return true; // Rules can be accessed by name
54+
}
55+
56+
public function offsetGet($offset): AnalyticsRuleV2
57+
{
58+
return new AnalyticsRuleV2($offset, $this->apiCall);
59+
}
60+
61+
public function offsetSet($offset, $value): void
62+
{
63+
// Not implemented for read-only access
64+
}
65+
66+
public function offsetUnset($offset): void
67+
{
68+
// Not implemented for read-only access
69+
}
70+
}

src/AnalyticsV2.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
class AnalyticsV2
6+
{
7+
const RESOURCE_PATH = '/analytics';
8+
9+
private ApiCall $apiCall;
10+
11+
private AnalyticsRulesV2 $rules;
12+
13+
private AnalyticsEventsV2 $events;
14+
15+
public function __construct(ApiCall $apiCall)
16+
{
17+
$this->apiCall = $apiCall;
18+
}
19+
20+
public function rules()
21+
{
22+
if (!isset($this->rules)) {
23+
$this->rules = new AnalyticsRulesV2($this->apiCall);
24+
}
25+
return $this->rules;
26+
}
27+
28+
public function events()
29+
{
30+
if (!isset($this->events)) {
31+
$this->events = new AnalyticsEventsV2($this->apiCall);
32+
}
33+
return $this->events;
34+
}
35+
}

0 commit comments

Comments
 (0)