-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
324 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.