Skip to content

Commit 2577a5c

Browse files
committed
Add Stopwords API
1 parent 50fc208 commit 2577a5c

4 files changed

Lines changed: 115 additions & 3 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.5'
22

33
services:
44
typesense:
5-
image: typesense/typesense:0.21.0.rc20
5+
image: typesense/typesense:26.0
66
environment:
77
TYPESENSE_DATA_DIR: /data
88
TYPESENSE_API_KEY: xyz

src/ApiCall.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
233233

234234
if (!(200 <= $statusCode && $statusCode < 300)) {
235235
$errorMessage = json_decode($response->getBody()
236-
->getContents(), true, 512, JSON_THROW_ON_ERROR)['message'] ?? 'API error.';
236+
->getContents(), true, 512, JSON_THROW_ON_ERROR)['message'] ?? 'API error.';
237237
throw $this->getException($statusCode)
238238
->setMessage($errorMessage);
239239
}
@@ -244,7 +244,7 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
244244
} catch (HttpException $exception) {
245245
if (
246246
$exception->getResponse()
247-
->getStatusCode() === 408
247+
->getStatusCode() === 408
248248
) {
249249
continue;
250250
}

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Client
2424
*/
2525
public Collections $collections;
2626

27+
/**
28+
* @var Stopwords
29+
*/
30+
public Stopwords $stopwords;
31+
2732
/**
2833
* @var Aliases
2934
*/
@@ -87,6 +92,7 @@ public function __construct(array $config)
8792
$this->apiCall = new ApiCall($this->config);
8893

8994
$this->collections = new Collections($this->apiCall);
95+
$this->stopwords = new Stopwords($this->apiCall);
9096
$this->aliases = new Aliases($this->apiCall);
9197
$this->keys = new Keys($this->apiCall);
9298
$this->debug = new Debug($this->apiCall);
@@ -106,6 +112,14 @@ public function getCollections(): Collections
106112
return $this->collections;
107113
}
108114

115+
/**
116+
* @return Stopwords
117+
*/
118+
public function getStopwords(): Stopwords
119+
{
120+
return $this->stopwords;
121+
}
122+
109123
/**
110124
* @return Aliases
111125
*/

src/Stopwords.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class Document
10+
*
11+
* @package \Typesense
12+
* @date 4/5/20
13+
* @author Abdullah Al-Faqeir <abdullah@devloops.net>
14+
*/
15+
class Stopwords
16+
{
17+
/**
18+
* @var ApiCall
19+
*/
20+
private $apiCall;
21+
22+
public const STOPWORDS_PATH = '/stopwords';
23+
24+
/**
25+
* Document constructor.
26+
*
27+
* @param ApiCall $apiCall
28+
*/
29+
public function __construct(ApiCall $apiCall)
30+
{
31+
$this->apiCall = $apiCall;
32+
}
33+
34+
/**
35+
* @return array|string
36+
* @throws HttpClientException
37+
* @throws TypesenseClientError
38+
*/
39+
public function get(string $stopwordsName)
40+
{
41+
return $this->apiCall->get(
42+
$this->endpointPath($stopwordsName),
43+
[]
44+
);
45+
}
46+
/**
47+
* @return array|string
48+
* @throws HttpClientException
49+
* @throws TypesenseClientError
50+
*/
51+
public function getAll()
52+
{
53+
return $this->apiCall->get(static::STOPWORDS_PATH, []);
54+
}
55+
56+
/**
57+
* @param array $options
58+
*
59+
* @return array
60+
* @throws HttpClientException
61+
* @throws TypesenseClientError
62+
*/
63+
public function put(array $options = [])
64+
{
65+
$stopwordsName = $options['stopwords_name'];
66+
$stopwordsData = $options['stopwords_data'];
67+
return $this->apiCall->put(
68+
$this->endpointPath($stopwordsName),
69+
['stopwords' => $stopwordsData]
70+
);
71+
}
72+
73+
/**
74+
* @param $presetName
75+
* @return array
76+
* @throws HttpClientException
77+
* @throws TypesenseClientError
78+
*/
79+
public function delete($presetName)
80+
{
81+
return $this->apiCall->delete(
82+
$this->endpointPath($presetName)
83+
);
84+
}
85+
86+
/**
87+
* @param $stopwordsName
88+
* @return string
89+
*/
90+
private function endpointPath($stopwordsName)
91+
{
92+
return sprintf(
93+
'%s/%s',
94+
static::STOPWORDS_PATH,
95+
$stopwordsName
96+
);
97+
}
98+
}

0 commit comments

Comments
 (0)