Skip to content

Commit 2680d50

Browse files
Merge pull request #40 from typesense/feature/presets
Version compatible with typesense v. 0.24
2 parents ea2e1dd + 49402ec commit 2680d50

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

src/Client.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
class Client
1616
{
17-
1817
/**
1918
* @var Configuration
2019
*/
@@ -60,6 +59,11 @@ class Client
6059
*/
6160
public MultiSearch $multiSearch;
6261

62+
/**
63+
* @var Presets
64+
*/
65+
public Presets $presets;
66+
6367
/**
6468
* @var ApiCall
6569
*/
@@ -85,6 +89,7 @@ public function __construct(array $config)
8589
$this->health = new Health($this->apiCall);
8690
$this->operations = new Operations($this->apiCall);
8791
$this->multiSearch = new MultiSearch($this->apiCall);
92+
$this->presets = new Presets($this->apiCall);
8893
}
8994

9095
/**
@@ -150,4 +155,12 @@ public function getMultiSearch(): MultiSearch
150155
{
151156
return $this->multiSearch;
152157
}
158+
159+
/**
160+
* @return Presets
161+
*/
162+
public function getPresets(): Presets
163+
{
164+
return $this->presets;
165+
}
153166
}

src/Presets.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 <[email protected]>
14+
*/
15+
class Presets
16+
{
17+
/**
18+
* @var ApiCall
19+
*/
20+
private ApiCall $apiCall;
21+
22+
public const PRESETS_PATH = '/presets';
23+
24+
public const MULTI_SEARCH_PATH = '/multi_search';
25+
26+
/**
27+
* Document constructor.
28+
*
29+
* @param ApiCall $apiCall
30+
*/
31+
public function __construct(ApiCall $apiCall)
32+
{
33+
$this->apiCall = $apiCall;
34+
}
35+
36+
/**
37+
* @param $presetName
38+
* @return array|string
39+
* @throws HttpClientException
40+
* @throws TypesenseClientError
41+
*/
42+
public function searchWithPreset($presetName): array|string
43+
{
44+
return $this->apiCall->post($this->multiSearchEndpointPath(), [], true, ['preset' => $presetName]);
45+
}
46+
47+
/**
48+
* @return array|string
49+
* @throws HttpClientException
50+
* @throws TypesenseClientError
51+
*/
52+
public function get(): array|string
53+
{
54+
return $this->apiCall->get(static::PRESETS_PATH, []);
55+
}
56+
57+
/**
58+
* @param array $options
59+
*
60+
* @return array
61+
* @throws HttpClientException
62+
* @throws TypesenseClientError
63+
*/
64+
public function put(array $options = []): array
65+
{
66+
$presetName = $options['preset_name'];
67+
$presetsData = $options['preset_data'];
68+
return $this->apiCall->put($this->endpointPath($presetName), $presetsData);
69+
}
70+
71+
/**
72+
* @param $presetName
73+
* @return array
74+
* @throws HttpClientException
75+
* @throws TypesenseClientError
76+
*/
77+
public function delete($presetName): array
78+
{
79+
return $this->apiCall->delete($this->endpointPath($presetName));
80+
}
81+
82+
/**
83+
* @param $presetsName
84+
* @return string
85+
*/
86+
private function endpointPath($presetsName): string
87+
{
88+
return sprintf(
89+
'%s/%s',
90+
static::PRESETS_PATH,
91+
$presetsName
92+
);
93+
}
94+
95+
/**
96+
* @param $presetsName
97+
* @return string
98+
*/
99+
private function multiSearchEndpointPath(): string
100+
{
101+
return sprintf(
102+
'%s',
103+
static::MULTI_SEARCH_PATH
104+
);
105+
}
106+
}

0 commit comments

Comments
 (0)