|
| 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