From 3cc2a1afff39bc1ed11be537e7d99aab488c20bb Mon Sep 17 00:00:00 2001 From: Fausto Quaggia Date: Tue, 2 Jul 2024 23:55:53 +0200 Subject: [PATCH] fix: basic search query --- src/Client.php | 8 ++++---- src/Query.php | 15 ++++++++++----- tests/ClientTest.php | 6 +++--- tests/QueryTest.php | 9 +++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Client.php b/src/Client.php index 0f2be97..388fb53 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,4 +1,5 @@ endpoint}/search?api-key={$this->apiKey}"; - $options = [ + + $response = $this->http->request('POST', $endpoint, [ 'form_params' => [ 'q' => $query->toJson() ] - ]; - - $response = $this->http->request('POST', $endpoint, $options); + ]); return json_decode($response->getBody(), true); } diff --git a/src/Query.php b/src/Query.php index c6f3501..eb05501 100644 --- a/src/Query.php +++ b/src/Query.php @@ -1,4 +1,5 @@ $this->term, @@ -44,4 +40,13 @@ public function toArray() { public function toJson() { return json_encode($this->toArray()); } + + public static function fromArray($array) { + $query = new Query(); + $query + ->setTerm(isset($array['term']) ? $array['term'] : '') + ->setMode(isset($array['mode']) ? $array['mode'] : 'fulltext'); + + return $query; + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index fd70f05..12de171 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -4,8 +4,8 @@ use OramaCloud\Client; use OramaCloud\Query; -const ENDPOINT = 'https://cloud.orama.run/v1/indexes/askorama-ai-development-uc6oxa'; -const API_KEY = 'P9buEfpy8rWvT265McikCG1tP4pT6cBg'; +const API_ENDPOINT = 'https://cloud.orama.run/v1/indexes/askorama-ai-development-uc6oxa'; +const PUBLIC_API_KEY = 'P9buEfpy8rWvT265McikCG1tP4pT6cBg'; class ClientTest extends TestCase { @@ -14,7 +14,7 @@ class ClientTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->client = new Client(ENDPOINT, API_KEY); + $this->client = new Client(API_ENDPOINT, PUBLIC_API_KEY); } public function testBasicFullTextSearch() { diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 85bc71e..e6f6a2d 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -17,6 +17,15 @@ public function testQueryParams() { $this->assertEquals($mode, $query->getMode()); } + public function testDefaultQueryParamsFromArray() { + $query = Query::fromArray([]); + + $this->assertEquals($query->toArray(), [ + 'term' => '', + 'mode' => 'fulltext' + ]); + } + public function testQueryParamsFromArray() { $params = [ 'term' => 'mock-term',