diff --git a/src/Client/Query.php b/src/Client/Query.php index ebf7d17..e3a095f 100644 --- a/src/Client/Query.php +++ b/src/Client/Query.php @@ -5,6 +5,7 @@ use OramaCloud\Client\QueryParams\SortBy; use OramaCloud\Client\QueryParams\SortByOrder; use OramaCloud\Client\QueryParams\Where; +use OramaCloud\Exceptions\QueryException; class Query { @@ -17,12 +18,15 @@ class Query public function __construct($term = '', $mode = 'fulltext') { - $this->term = $term; - $this->mode = $mode; + $this->term($term)->mode($mode); } public function term(string $term) { + if (!is_string($term)) { + throw new QueryException('Invalid search term.'); + } + $this->term = $term; return $this; } diff --git a/src/Client/QueryParams/SortBy.php b/src/Client/QueryParams/SortBy.php index f3a2d0c..dde7d14 100644 --- a/src/Client/QueryParams/SortBy.php +++ b/src/Client/QueryParams/SortBy.php @@ -2,6 +2,8 @@ namespace OramaCloud\Client\QueryParams; +use OramaCloud\Exceptions\QueryException; + class SortBy { private $property; @@ -30,7 +32,7 @@ public function toArray() private function validate() { if (!in_array($this->order, $this->availableOrders)) { - throw new \InvalidArgumentException('Invalid $order parameter in SortBy'); + throw new QueryException('Invalid $order parameter in SortBy'); } } } diff --git a/src/Client/QueryParams/Where.php b/src/Client/QueryParams/Where.php index 9dcd735..e7a35b1 100644 --- a/src/Client/QueryParams/Where.php +++ b/src/Client/QueryParams/Where.php @@ -2,6 +2,8 @@ namespace OramaCloud\Client\QueryParams; +use OramaCloud\Exceptions\QueryException; + class Where { private $property; @@ -38,8 +40,12 @@ public function toArray() private function validate() { + if (empty($this->property)) { + throw new QueryException("Missing or invalid property: {$this->property}"); + } + if (!in_array($this->operator, $this->availableOperators)) { - throw new \InvalidArgumentException("Invalid operator {$this->operator}"); + throw new QueryException("Invalid where operator: {$this->operator}"); } if (in_array($this->operator, [ @@ -47,7 +53,7 @@ private function validate() WhereOperator::IN, WhereOperator::NIN ]) && !is_array($this->value)) { - throw new \InvalidArgumentException('Where $value parameter must be an array'); + throw new QueryException('Where $value argument must be an array'); } } } diff --git a/src/Exceptions/IndexManagerException.php b/src/Exceptions/IndexManagerException.php new file mode 100644 index 0000000..0e4a99c --- /dev/null +++ b/src/Exceptions/IndexManagerException.php @@ -0,0 +1,11 @@ +indexId) { - throw new \Exception('Index ID is not set'); + throw new IndexManagerException('Index ID is not set'); } - $config = [ - 'headers' => [ + try { + $config['headers'] = [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $this->apiKey - ] - ]; + ]; - if (!is_null($payload)) { - $config['body'] = json_encode($payload); - } + if (!is_null($payload)) { + $config['body'] = json_encode($payload); + } + + $response = $this->http->request('POST', $this->getEndpoint($endpoint), $config); - $response = $this->http->request('POST', $this->getEndpoint($endpoint), $config); + if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { + throw new IndexManagerException('Error calling webhook: ' . $response->getBody()->getContents()); + } - return json_decode($response->getBody()->getContents()); + return json_decode($response->getBody()->getContents()); + } catch (\Exception $e) { + throw new IndexManagerException($e->getMessage()); + } } private function getEndpoint(string $endpoint): string diff --git a/src/Manager/IndexManager.php b/src/Manager/IndexManager.php index 88d4054..0d26683 100644 --- a/src/Manager/IndexManager.php +++ b/src/Manager/IndexManager.php @@ -68,7 +68,7 @@ private function init($manager) { if ($manager instanceof CloudManager) { $this->manager = $manager; - } else if ($manager instanceof string) { + } else if (gettype($manager) === 'string') { $this->manager = new CloudManager($manager); } else { throw new \Exception('Invalid manager parameter. It should be an instance of CloudManager or an API key string.');