Skip to content

Commit a35736e

Browse files
authored
Merge pull request #107 from tharropoulos/v30
feat!!: add v30 changes
2 parents 5354a7a + ad047c9 commit a35736e

26 files changed

+1563
-116
lines changed

.github/workflows/tests.yml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@ jobs:
66
test:
77
name: Run tests with PHP v8.3
88
runs-on: ubuntu-latest
9-
services:
10-
typesense:
11-
image: typesense/typesense:28.0.rc36
12-
ports:
13-
- 8108:8108/tcp
14-
volumes:
15-
- /tmp/typesense-server-data:/data
16-
env:
17-
TYPESENSE_DATA_DIR: /data
18-
TYPESENSE_API_KEY: xyz
19-
TYPESENSE_ENABLE_CORS: true
209
steps:
10+
- name: Start Typesense
11+
run: |
12+
docker run -d \
13+
-p 8108:8108 \
14+
--name typesense \
15+
-v /tmp/typesense-data:/data \
16+
-v /tmp/typesense-analytics-data:/analytics-data \
17+
typesense/typesense:30.0.alpha1 \
18+
--api-key=xyz \
19+
--data-dir=/data \
20+
--enable-search-analytics=true \
21+
--analytics-dir=/analytics-data \
22+
--analytics-flush-interval=60 \
23+
--analytics-minute-rate-limit=50 \
24+
--enable-cors
25+
2126
- uses: actions/checkout@v4
2227
- name: Setup PHP 8.3
2328
uses: shivammathur/setup-php@v2
2429
with:
25-
php-version: '8.3'
30+
php-version: "8.3"
2631
coverage: xdebug
2732
- uses: php-actions/composer@v6
2833
- name: Run tests

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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/**
66
* Class AnalyticsEvents
7+
*
8+
* Implements the updated analytics events API for Typesense v30.0+
79
*
810
* @package \Typesense
911
*/
@@ -27,21 +29,25 @@ public function __construct(ApiCall $apiCall)
2729
}
2830

2931
/**
30-
* @param array $params
31-
*
32-
* @return array
32+
* Create an analytics event
33+
*
34+
* @param array $params Event parameters including name, event_type, and data
35+
* @return array Response from the API
3336
* @throws TypesenseClientError|HttpClientException
3437
*/
35-
public function create($params)
38+
public function create(array $params)
3639
{
37-
return $this->apiCall->post($this->endpoint_path(), $params);
40+
return $this->apiCall->post(self::RESOURCE_PATH, $params);
3841
}
3942

4043
/**
41-
* @return string
44+
* Retrieve analytics events
45+
*
46+
* @param array $params Query parameters
47+
* @return array Response from the API
4248
*/
43-
private function endpoint_path($operation = null)
49+
public function retrieve(array $params = [])
4450
{
45-
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
51+
return $this->apiCall->get(self::RESOURCE_PATH, $params);
4652
}
47-
}
53+
}

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/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/AnalyticsRules.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,58 @@ class AnalyticsRules implements \ArrayAccess
77
const RESOURCE_PATH = '/analytics/rules';
88

99
private ApiCall $apiCall;
10-
private $analyticsRules = [];
10+
11+
/**
12+
* @var array
13+
*/
14+
private array $analyticsRules = [];
1115

1216
public function __construct(ApiCall $apiCall)
1317
{
1418
$this->apiCall = $apiCall;
1519
}
1620

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)
21+
/**
22+
* Create multiple analytics rules
23+
*
24+
* @param array $rules Array of rule objects
25+
* @return array Response from the API
26+
*/
27+
public function create(array $rules)
2628
{
27-
return $this->apiCall->put($this->endpoint_path($ruleName), $params);
29+
return $this->apiCall->post(self::RESOURCE_PATH, $rules);
2830
}
2931

32+
/**
33+
* Retrieve all analytics rules
34+
*
35+
* @return array Response from the API
36+
*/
3037
public function retrieve()
3138
{
32-
return $this->apiCall->get($this->endpoint_path(), []);
39+
return $this->apiCall->get(self::RESOURCE_PATH, []);
3340
}
3441

35-
private function endpoint_path($operation = null)
42+
/**
43+
* Get a specific rule by name
44+
*
45+
* @param string $ruleName
46+
* @return AnalyticsRule
47+
*/
48+
public function __get($ruleName)
3649
{
37-
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
50+
if (isset($this->{$ruleName})) {
51+
return $this->{$ruleName};
52+
}
53+
if (!isset($this->analyticsRules[$ruleName])) {
54+
$this->analyticsRules[$ruleName] = new AnalyticsRule($ruleName, $this->apiCall);
55+
}
56+
57+
return $this->analyticsRules[$ruleName];
3858
}
3959

4060
/**
41-
* @inheritDoc
61+
* ArrayAccess implementation for backwards compatibility
4262
*/
4363
public function offsetExists($offset): bool
4464
{
@@ -72,4 +92,4 @@ public function offsetUnset($offset): void
7292
{
7393
unset($this->analyticsRules[$offset]);
7494
}
75-
}
95+
}

src/AnalyticsRulesV1.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
class AnalyticsRulesV1 implements \ArrayAccess
6+
{
7+
const RESOURCE_PATH = '/analytics/rules';
8+
9+
private ApiCall $apiCall;
10+
private $analyticsRules = [];
11+
12+
public function __construct(ApiCall $apiCall)
13+
{
14+
$this->apiCall = $apiCall;
15+
}
16+
17+
public function __get($ruleName)
18+
{
19+
if (!isset($this->analyticsRules[$ruleName])) {
20+
$this->analyticsRules[$ruleName] = new AnalyticsRuleV1($ruleName, $this->apiCall);
21+
}
22+
return $this->analyticsRules[$ruleName];
23+
}
24+
25+
public function upsert($ruleName, $params)
26+
{
27+
return $this->apiCall->put($this->endpoint_path($ruleName), $params);
28+
}
29+
30+
public function retrieve()
31+
{
32+
return $this->apiCall->get($this->endpoint_path(), []);
33+
}
34+
35+
private function endpoint_path($operation = null)
36+
{
37+
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
38+
}
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): AnalyticsRuleV1
52+
{
53+
if (!isset($this->analyticsRules[$offset])) {
54+
$this->analyticsRules[$offset] = new AnalyticsRuleV1($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+
}
75+
}

0 commit comments

Comments
 (0)