|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Typesense; |
| 4 | + |
| 5 | +class StemmingDictionaries implements \ArrayAccess |
| 6 | +{ |
| 7 | + const RESOURCE_PATH = '/stemming/dictionaries'; |
| 8 | + |
| 9 | + private ApiCall $apiCall; |
| 10 | + private $typesenseDictionaries = []; |
| 11 | + |
| 12 | + public function __construct(ApiCall $apiCall) |
| 13 | + { |
| 14 | + $this->apiCall = $apiCall; |
| 15 | + } |
| 16 | + |
| 17 | + public function __get($id) |
| 18 | + { |
| 19 | + if (!isset($this->typesenseDictionaries[$id])) { |
| 20 | + $this->typesenseDictionaries[$id] = new StemmingDictionary($id, $this->apiCall); |
| 21 | + } |
| 22 | + return $this->typesenseDictionaries[$id]; |
| 23 | + } |
| 24 | + |
| 25 | + public function upsert($id, $wordRootCombinations) |
| 26 | + { |
| 27 | + $dictionaryInJSONLFormat = is_array($wordRootCombinations) ? implode( |
| 28 | + "\n", |
| 29 | + array_map( |
| 30 | + static fn(array $wordRootCombo) => json_encode($wordRootCombo, JSON_THROW_ON_ERROR), |
| 31 | + $wordRootCombinations |
| 32 | + ) |
| 33 | + ) : $wordRootCombinations; |
| 34 | + |
| 35 | + $resultsInJSONLFormat = $this->apiCall->post($this->endpoint_path("import"), $dictionaryInJSONLFormat, false, ["id" => $id]); |
| 36 | + |
| 37 | + return is_array($wordRootCombinations) ? array_map( |
| 38 | + static function ($item) { |
| 39 | + return json_decode($item, true, 512, JSON_THROW_ON_ERROR); |
| 40 | + }, |
| 41 | + array_filter( |
| 42 | + explode("\n", $resultsInJSONLFormat), |
| 43 | + 'strlen' |
| 44 | + ) |
| 45 | + ) : $resultsInJSONLFormat; |
| 46 | + } |
| 47 | + |
| 48 | + public function retrieve() |
| 49 | + { |
| 50 | + $response = $this->apiCall->get(StemmingDictionaries::RESOURCE_PATH, []); |
| 51 | + |
| 52 | + // If response is null, return empty dictionaries structure |
| 53 | + if ($response === null) { |
| 54 | + return ['dictionaries' => []]; |
| 55 | + } |
| 56 | + return $response; |
| 57 | + } |
| 58 | + |
| 59 | + private function endpoint_path($operation = null) |
| 60 | + { |
| 61 | + return $operation === null ? self::RESOURCE_PATH : self::RESOURCE_PATH . "/" . encodeURIComponent($operation); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @inheritDoc |
| 66 | + */ |
| 67 | + public function offsetExists($offset): bool |
| 68 | + { |
| 69 | + return isset($this->typesenseDictionaries[$offset]); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritDoc |
| 74 | + */ |
| 75 | + public function offsetGet($offset): StemmingDictionary |
| 76 | + { |
| 77 | + if (!isset($this->typesenseDictionaries[$offset])) { |
| 78 | + $this->typesenseDictionaries[$offset] = new StemmingDictionary($offset, $this->apiCall); |
| 79 | + } |
| 80 | + |
| 81 | + return $this->typesenseDictionaries[$offset]; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritDoc |
| 86 | + */ |
| 87 | + public function offsetSet($offset, $value): void |
| 88 | + { |
| 89 | + $this->typesenseDictionaries[$offset] = $value; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheritDoc |
| 94 | + */ |
| 95 | + public function offsetUnset($offset): void |
| 96 | + { |
| 97 | + unset($this->typesenseDictionaries[$offset]); |
| 98 | + } |
| 99 | +} |
0 commit comments