Skip to content

Commit

Permalink
feat: query params
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 7, 2024
1 parent e261385 commit fcb0c12
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 64 deletions.
43 changes: 35 additions & 8 deletions src/Client/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OramaCloud\Client;

use OramaCloud\Client\QueryParams\SortBy;
use OramaCloud\Client\QueryParams\SortByOrder;
use OramaCloud\Client\QueryParams\Where;

class Query
Expand All @@ -10,6 +12,7 @@ class Query
private $mode;
private $limit;
private $offset;
private $sortBy;
private $where = [];

public function __construct($term = '', $mode = 'fulltext')
Expand All @@ -30,19 +33,25 @@ public function mode($mode)
return $this;
}

public function where($property, $operator, $value)
public function where(string $property, $operator, $value)
{
$this->where[] = new Where($property, $operator, $value);
return $this;
}

public function limit($limit)
public function sortBy(string $property, $order = SortByOrder::ASC)
{
$this->sortBy = new SortBy($property, $order);
return $this;
}

public function limit(int $limit)
{
$this->limit = $limit;
return $this;
}

public function offset($offset)
public function offset(int $offset)
{
$this->offset = $offset;
return $this;
Expand Down Expand Up @@ -76,12 +85,11 @@ public function toArray()
}
}

return $array;
}
if (!is_null($this->sortBy)) {
$array['sortBy'] = $this->sortBy->toArray();
}

public function toJson()
{
return json_encode($this->toArray());
return $array;
}

public static function fromArray($array)
Expand All @@ -97,6 +105,10 @@ public static function fromArray($array)
}
}

if (isset($array['sortBy']) && !is_null($array['sortBy'])) {
$query->sortBy($array['sortBy']['property'], $array['sortBy']['order']);
}

if (isset($array['limit']) && !is_null($array['limit'])) {
$query->limit($array['limit']);
}
Expand All @@ -107,4 +119,19 @@ public static function fromArray($array)

return $query;
}

public function toJson()
{
return json_encode($this->toArray());
}

public static function fromJson($json)
{
return Query::fromArray(json_decode($json, true));
}

public function toQueryString()
{
return http_build_query($this->toArray());
}
}
36 changes: 36 additions & 0 deletions src/Client/QueryParams/SortBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace OramaCloud\Client\QueryParams;

class SortBy
{
private $property;
private $order;
private $availableOrders = [
SortByOrder::ASC,
SortByOrder::DESC
];

public function __construct(string $property, $order = SortByOrder::ASC)
{
$this->property = $property;
$this->order = strtoupper($order);

$this->validate();
}

public function toArray()
{
return [
'property' => $this->property,
'order' => $this->order
];
}

private function validate()
{
if (!in_array($this->order, $this->availableOrders)) {
throw new \InvalidArgumentException('Invalid $order parameter in SortBy');
}
}
}
10 changes: 10 additions & 0 deletions src/Client/QueryParams/SortByOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OramaCloud\Client\QueryParams;

class SortByOrder
{
const ASC = 'ASC';

const DESC = 'DESC';
}
29 changes: 28 additions & 1 deletion src/Client/QueryParams/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ class Where
private $property;
private $operator;
private $value;
private $availableOperators = [
WhereOperator::GT,
WhereOperator::GTE,
WhereOperator::LT,
WhereOperator::LTE,
WhereOperator::EQ,
WhereOperator::BETWEEN,
WhereOperator::IN,
WhereOperator::NIN
];

public function __construct($property, $operator, $value)
public function __construct(string $property, $operator, $value)
{
$this->property = $property;
$this->operator = $operator;
$this->value = $value;

$this->validate();
}

public function toArray()
Expand All @@ -24,4 +36,19 @@ public function toArray()
]
];
}

private function validate()
{
if (!in_array($this->operator, $this->availableOperators)) {
throw new \InvalidArgumentException("Invalid operator {$this->operator}");
}

if (in_array($this->operator, [
WhereOperator::BETWEEN,
WhereOperator::IN,
WhereOperator::NIN
]) && !is_array($this->value)) {
throw new \InvalidArgumentException('Where $value parameter must be an array');
}
}
}
22 changes: 22 additions & 0 deletions src/Client/QueryParams/WhereOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace OramaCloud\Client\QueryParams;

class WhereOperator
{
const GT = 'gt';

const GTE = 'gte';

const LT = 'lt';

const LTE = 'lte';

const EQ = 'eq';

const BETWEEN = 'between';

const IN = 'in';

const NIN = 'nin';
}
109 changes: 54 additions & 55 deletions tests/Unit/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,62 @@

use OramaCloud\Client\Query;

test('configure query params', function () {
$query = new Query();
$query
->term('red shoes')
->mode('fulltext')
->where('price', 'gte', 99.99)
->where('category', 'eq', 'shoes');

$result = $query->toArray();

$this->assertEquals($result['term'], 'red shoes');
$this->assertEquals($result['mode'], 'fulltext');
$this->assertEquals($result['where'], [
'category' => [
'eq' => 'shoes'
],
'price' => [
'gte' => 99.99
]
]);
});

test('default query params from array', function () {
$query = Query::fromArray([]);

$this->assertEquals($query->toArray(), [
'term' => '',
'mode' => 'fulltext',
]);
});

test('query params from array', function () {
$params = [
'term' => 'mock-term',
'mode' => 'mock-mode',
'where' => [
'foo' => [
'eq' => 99
describe('Query builder', function () {
it('should configure query params', function () {
$query = new Query();
$query
->term('red shoes')
->mode('fulltext')
->where('price', 'gte', 99.99)
->where('category', 'eq', 'shoes')
->sortBy('price', 'desc');

$result = $query->toArray();

$this->assertEquals($result['term'], 'red shoes');
$this->assertEquals($result['mode'], 'fulltext');
$this->assertEquals($result['where'], [
'category' => [
'eq' => 'shoes'
],
'bar' => [
'gt' => 10
'price' => [
'gte' => 99.99
]
]
];

$query = Query::fromArray($params);

$this->assertEquals($params, $query->toArray());
});

test('query params as json', function () {
$params = [
'term' => 'mock-term',
'mode' => 'mock-mode'
];
]);
$this->assertEquals($result['sortBy'], [
'property' => 'price',
'order' => 'DESC'
]);

$this->assertEquals($query->toJson(), json_encode($result));
});

it('defaults query params from array', function () {
$query = Query::fromArray([]);

$this->assertEquals($query->toArray(), [
'term' => '',
'mode' => 'fulltext',
]);
});

it('accepts query params from array', function () {
$params = [
'term' => 'mock-term',
'mode' => 'mock-mode',
'where' => [
'foo' => [
'eq' => 99
],
'bar' => [
'gt' => 10
]
]
];

$query = Query::fromArray($params);
$query = Query::fromArray($params);

$this->assertEquals($params, json_decode($query->toJson(), true));
$this->assertEquals($params, $query->toArray());
$this->assertEquals($params, json_decode($query->toJson(), true));
});
});
41 changes: 41 additions & 0 deletions tests/Unit/SortByTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use OramaCloud\Client\QueryParams\SortBy;
use OramaCloud\Client\QueryParams\SortByOrder;

describe('Sort By', function () {
it('should create a sort by object', function () {
$sortBy = new SortBy('name', SortByOrder::ASC);

expect($sortBy->toArray())->toBe([
'property' => 'name',
'order' => 'ASC'
]);
});

it('should throw an exception when invalid order is passed', function () {
$closure = function () {
new SortBy('name', 'INVALID');
};

expect($closure)->toThrow(new \InvalidArgumentException('Invalid $order parameter in SortBy'));
});

it('should create a sort by object with default order', function () {
$sortBy = new SortBy('metadata.title');

expect($sortBy->toArray())->toBe([
'property' => 'metadata.title',
'order' => 'ASC'
]);
});

it('should create a sort by object with DESC order', function () {
$sortBy = new SortBy('name', SortByOrder::DESC);

expect($sortBy->toArray())->toBe([
'property' => 'name',
'order' => 'DESC'
]);
});
});
Loading

0 comments on commit fcb0c12

Please sign in to comment.