Skip to content

Commit

Permalink
add toArray helper to queryOption
Browse files Browse the repository at this point in the history
  • Loading branch information
Whyounes committed Jan 30, 2023
1 parent 5a21e53 commit 62e250f
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Tests/QueryFilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@
expect($queryFilter)->toBeInstanceOf(QueryFilter::class);
expect($queryFilter->getValue())->toEqual('myvalue2');
});

test('it can be casted to array', function () {
$filterCollection = new QueryFilterCollection();
$filterCollection->addFilterParams('myfield1', 'myvalue1');
$filterCollection->addFilterParams('myfield2', 'myvalue2');

$expectedArray = [
['field' => 'myfield1', 'operator' => '=', 'value' => 'myvalue1'],
['field' => 'myfield2', 'operator' => '=', 'value' => 'myvalue2'],
];

foreach ($filterCollection->toArray() as $key => $filterArray) {
expect($filterArray)->toMatchArray($expectedArray[$key]);
}
});
6 changes: 6 additions & 0 deletions Tests/QueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
test('it throws exception on invalid operator', function () {
new QueryFilter('myfield', 'wrong', 'myvalue');
})->throws(InvalidFilterOperatorException::class);

test('it can be casted to array', function () {
$queryFilter = new QueryFilter('myfield', QueryFilter::OPERATOR_EQ, 'myvalue');

expect($queryFilter->toArray())->toEqual(['field' => 'myfield', 'operator' => '=', 'value' => 'myvalue']);
});
40 changes: 40 additions & 0 deletions Tests/QueryOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@
expect($queryOption->getFilters()->findByName('created_at'))->toBeNull();
expect($queryOption->getFilters())->toHaveCount(1);
});

test('it can be casted to array', function () {
$queryOption = new QueryOption(
new QuerySearch('term', QuerySearch::SEARCH_TYPE_LIKE),
new QueryFilterCollection(),
new QuerySort('created_at', QuerySort::SORT_DESC),
2,
10
);

$filterParams = [
[
'field' => 'myfield1',
'value' => 'myvalue1',
],
[
'field' => 'myfield2',
'value' => 'myvalue2',
]
];
foreach ($filterParams as $param) {
$queryOption->getFilters()->addFilterParams($param['field'], $param['value']);
}

expect($queryOption->toArray())->toMatchArray([
'q' => 'term',
'search_type' => 'like',
'page' => 2,
'limit' => 10,
'sort_field' => 'created_at',
'sort_order' => 'desc',
]);

expect($queryOption->toArray())->toHaveKey('filters');
expect($queryOption->toArray()['filters'])->toBeArray();

foreach ($queryOption->getFilters() as $key => $filter) {
expect($filter->toArray())->toMatchArray($filterParams[$key]);
}
});
6 changes: 6 additions & 0 deletions Tests/QuerySearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
test('it throws exception when given invalid search type', function () {
new QuerySearch('test', 10);
})->throws(InvalidArgumentException::class);

test('it can be casted to array', function () {
$querySearch = new QuerySearch('test', QuerySearch::SEARCH_TYPE_LIKE);

expect($querySearch->toArray())->toEqual(['term' => 'test', 'type' => 'like']);
});
6 changes: 6 additions & 0 deletions Tests/QuerySortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@

expect($querySort->getDirection())->toEqual(QuerySort::SORT_DESC);
});

test('it can be casted to array', function () {
$querySort = new QuerySort('myfield', 'desc');

expect($querySort->toArray())->toEqual(['sort_field' => 'myfield', 'sort_order' => 'desc']);
});
13 changes: 13 additions & 0 deletions src/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace YouCanShop\QueryOption;

interface Arrayable
{
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array;
}
11 changes: 10 additions & 1 deletion src/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use YouCanShop\QueryOption\Exceptions\InvalidFilterOperatorException;

class QueryFilter
class QueryFilter implements Arrayable
{
public const OPERATOR_EQ = '=';
public const OPERATOR_IS = 'is';
Expand Down Expand Up @@ -92,4 +92,13 @@ public function getValue()
{
return $this->value;
}

public function toArray(): array
{
return [
'field' => $this->getField(),
'operator' => $this->getOperator(),
'value' => $this->getValue(),
];
}
}
13 changes: 12 additions & 1 deletion src/QueryFilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use ArrayAccess;
use Countable;

class QueryFilterCollection implements ArrayAccess, Countable, \Iterator
class QueryFilterCollection implements ArrayAccess, Countable, \Iterator, Arrayable
{
/** @var QueryFilter[] */
private array $filters = [];

private int $iteratorCursor = 0;
Expand Down Expand Up @@ -128,4 +129,14 @@ public function rewind()
{
$this->iteratorCursor = 0;
}

public function toArray(): array
{
$items = [];
foreach ($this->filters as $filter) {
$items[] = $filter->toArray();
}

return $items;
}
}
15 changes: 14 additions & 1 deletion src/QueryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace YouCanShop\QueryOption;

class QueryOption
class QueryOption implements Arrayable
{
const MAX_LIMIT = 50;
const DEFAULT_LIMIT = 10;
Expand Down Expand Up @@ -114,4 +114,17 @@ public function allowedFilters(array $filtersNames): self

return $this;
}

public function toArray(): array
{
return [
'q' => $this->getSearch()->getTerm(),
'search_type' => $this->getSearch()->getType(),
'page' => $this->getPage(),
'limit' => $this->getLimit(),
'sort_field' => $this->getSort()->getField(),
'sort_order' => $this->getSort()->getDirection(),
'filters' => $this->getFilters()->toArray()
];
}
}
10 changes: 9 additions & 1 deletion src/QuerySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use InvalidArgumentException;

class QuerySearch
class QuerySearch implements Arrayable
{
public const SEARCH_TYPE_LIKE = 'like';
public const SEARCH_TYPE_EQUAL = 'equal';
Expand Down Expand Up @@ -43,4 +43,12 @@ private function setType(?string $type): self

return $this;
}

public function toArray(): array
{
return [
'term' => $this->getTerm(),
'type' => $this->getType(),
];
}
}
10 changes: 9 additions & 1 deletion src/QuerySort.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace YouCanShop\QueryOption;

class QuerySort
class QuerySort implements Arrayable
{
public const DEFAULT_SORT_FIELD = 'created_at';

Expand Down Expand Up @@ -59,4 +59,12 @@ public function getField(): string
{
return $this->field;
}

public function toArray(): array
{
return [
'sort_field' => $this->getField(),
'sort_order' => $this->getDirection(),
];
}
}

0 comments on commit 62e250f

Please sign in to comment.