Skip to content

Commit ab74432

Browse files
committed
refactor(analytics): rename old analytics to analyticsV1
1 parent 38d6765 commit ab74432

18 files changed

+659
-659
lines changed

src/Analytics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public function events()
3232
}
3333
return $this->events;
3434
}
35-
}
35+
}

src/AnalyticsEvents.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,42 @@
44

55
/**
66
* Class AnalyticsEvents
7+
*
8+
* Implements the updated analytics events API for Typesense +
79
*
810
* @package \Typesense
911
*/
1012
class AnalyticsEvents
1113
{
1214
const RESOURCE_PATH = '/analytics/events';
1315

14-
/**
15-
* @var ApiCall
16-
*/
1716
private ApiCall $apiCall;
1817

19-
/**
20-
* AnalyticsEvents constructor.
21-
*
22-
* @param ApiCall $apiCall
23-
*/
2418
public function __construct(ApiCall $apiCall)
2519
{
2620
$this->apiCall = $apiCall;
2721
}
2822

2923
/**
30-
* @param array $params
31-
*
32-
* @return array
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
3328
* @throws TypesenseClientError|HttpClientException
3429
*/
35-
public function create($params)
30+
public function create(array $params)
3631
{
37-
return $this->apiCall->post($this->endpoint_path(), $params);
32+
return $this->apiCall->post(self::RESOURCE_PATH, $params);
3833
}
3934

4035
/**
41-
* @return string
36+
* Retrieve analytics events
37+
*
38+
* @param array $params Query parameters
39+
* @return array Response from the API
4240
*/
43-
private function endpoint_path($operation = null)
41+
public function retrieve(array $params = [])
4442
{
45-
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
43+
return $this->apiCall->get(self::RESOURCE_PATH, $params);
4644
}
47-
}
45+
}

src/AnalyticsEventsV1.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
/**
6+
* Class AnalyticsEventsV1
7+
*
8+
* @package \Typesense
9+
*/
10+
class AnalyticsEventsV1
11+
{
12+
const RESOURCE_PATH = '/analytics/events';
13+
14+
/**
15+
* @var ApiCall
16+
*/
17+
private ApiCall $apiCall;
18+
19+
/**
20+
* AnalyticsEventsV1 constructor.
21+
*
22+
* @param ApiCall $apiCall
23+
*/
24+
public function __construct(ApiCall $apiCall)
25+
{
26+
$this->apiCall = $apiCall;
27+
}
28+
29+
/**
30+
* @param array $params
31+
*
32+
* @return array
33+
* @throws TypesenseClientError|HttpClientException
34+
*/
35+
public function create($params)
36+
{
37+
return $this->apiCall->post($this->endpoint_path(), $params);
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
private function endpoint_path($operation = null)
44+
{
45+
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
46+
}
47+
}

src/AnalyticsEventsV2.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/AnalyticsRule.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,48 @@
44

55
class AnalyticsRule
66
{
7-
private $ruleName;
7+
private string $ruleName;
88
private ApiCall $apiCall;
99

1010
public function __construct(string $ruleName, ApiCall $apiCall)
1111
{
1212
$this->ruleName = $ruleName;
13-
$this->apiCall = $apiCall;
13+
$this->apiCall = $apiCall;
1414
}
1515

16+
/**
17+
* Retrieve a specific analytics rule
18+
*
19+
* @return array Response from the API
20+
*/
1621
public function retrieve()
1722
{
1823
return $this->apiCall->get($this->endpointPath(), []);
1924
}
2025

26+
/**
27+
* Delete a specific analytics rule
28+
*
29+
* @return array Response from the API
30+
*/
2131
public function delete()
2232
{
2333
return $this->apiCall->delete($this->endpointPath());
2434
}
2535

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+
2647
private function endpointPath()
2748
{
2849
return AnalyticsRules::RESOURCE_PATH . '/' . encodeURIComponent($this->ruleName);
2950
}
30-
}
51+
}

src/AnalyticsRuleV1.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
class AnalyticsRuleV1
6+
{
7+
private $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+
public function retrieve()
17+
{
18+
return $this->apiCall->get($this->endpointPath(), []);
19+
}
20+
21+
public function delete()
22+
{
23+
return $this->apiCall->delete($this->endpointPath());
24+
}
25+
26+
private function endpointPath()
27+
{
28+
return AnalyticsRulesV1::RESOURCE_PATH . '/' . encodeURIComponent($this->ruleName);
29+
}
30+
}

src/AnalyticsRuleV2.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/AnalyticsRules.php

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,64 @@ class AnalyticsRules implements \ArrayAccess
77
const RESOURCE_PATH = '/analytics/rules';
88

99
private ApiCall $apiCall;
10-
private $analyticsRules = [];
1110

1211
public function __construct(ApiCall $apiCall)
1312
{
1413
$this->apiCall = $apiCall;
1514
}
1615

17-
public function __get($ruleName)
18-
{
19-
if (!isset($this->analyticsRules[$ruleName])) {
20-
$this->analyticsRules[$ruleName] = new AnalyticsRule($ruleName, $this->apiCall);
21-
}
22-
return $this->analyticsRules[$ruleName];
23-
}
24-
25-
public function upsert($ruleName, $params)
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)
2623
{
27-
return $this->apiCall->put($this->endpoint_path($ruleName), $params);
24+
return $this->apiCall->post(self::RESOURCE_PATH, $rules);
2825
}
2926

27+
/**
28+
* Retrieve all analytics rules
29+
*
30+
* @return array Response from the API
31+
*/
3032
public function retrieve()
3133
{
32-
return $this->apiCall->get($this->endpoint_path(), []);
34+
return $this->apiCall->get(self::RESOURCE_PATH, []);
3335
}
3436

35-
private function endpoint_path($operation = null)
37+
/**
38+
* Get a specific rule by name
39+
*
40+
* @param string $ruleName
41+
* @return AnalyticsRule
42+
*/
43+
public function __get($ruleName)
3644
{
37-
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
45+
return new AnalyticsRule($ruleName, $this->apiCall);
3846
}
3947

4048
/**
41-
* @inheritDoc
49+
* ArrayAccess implementation for backwards compatibility
4250
*/
4351
public function offsetExists($offset): bool
4452
{
45-
return isset($this->analyticsRules[$offset]);
53+
return true; // Rules can be accessed by name
4654
}
4755

48-
/**
49-
* @inheritDoc
50-
*/
5156
public function offsetGet($offset): AnalyticsRule
5257
{
53-
if (!isset($this->analyticsRules[$offset])) {
54-
$this->analyticsRules[$offset] = new AnalyticsRule($offset, $this->apiCall);
55-
}
56-
57-
return $this->analyticsRules[$offset];
58+
return new AnalyticsRule($offset, $this->apiCall);
5859
}
5960

60-
/**
61-
* @inheritDoc
62-
*/
6361
public function offsetSet($offset, $value): void
6462
{
65-
$this->analyticsRules[$offset] = $value;
63+
// Not implemented for read-only access
6664
}
6765

68-
/**
69-
* @inheritDoc
70-
*/
7166
public function offsetUnset($offset): void
7267
{
73-
unset($this->analyticsRules[$offset]);
68+
// Not implemented for read-only access
7469
}
75-
}
70+
}

0 commit comments

Comments
 (0)