Skip to content

Commit

Permalink
Add pagination and order
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdheiden committed Oct 30, 2017
1 parent 14aed57 commit aebff37
Show file tree
Hide file tree
Showing 27 changed files with 874 additions and 144 deletions.
41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ composer require vdhicts/data-filter

## Usage

### Filter Field

Creating field is based on a key and a value:

```php
Expand All @@ -32,6 +34,8 @@ parameters are constants of the field `APPROVAL_ACCEPT`, `APPROVAL_REJECT`, `APP
`APPROVAL_END_OF_RANGE`, `APPROVAL_IN`, `APPROVAL_NOT_IN`, `APPROVAL_LIKE` and `APPROVAL_ILIKE` (which can only be used
if your Database supports it, like Postgres).

### Filter Group

The field is a member of a group. The group has a conjunction, which are constants of the group called
`CONJUNCTION_AND` and `CONJUCTION_OR`.

Expand All @@ -45,6 +49,35 @@ One or more groups are provided to the filter:
$filter = new Filter([$group]);
```

### Order

The sort order can be defined by `OrderField` objects:

```php
$orderField = new Orderfield('myField', 'ASC');
```

These objects are added to the order:

```php
$order = new Order([$orderField2]);
```

### Pagination

The pagination is handled by the `Pagination` object:

```php
$limit = 50;
$offset = 10;
$pagination = new Pagination($limit, $offset);
```

The remove the limit, use the `NO_LIMIT` constant from the `Pagination` class. By default no limit is used and the
offset is zero.

### Manager

The manager can encode and decode the filter so it can be used in the url or session. The actual encoding or decoding
is performed by a `Codec` which implements the `Codec` contract. By default a `Base64` codec is provided.

Expand Down Expand Up @@ -87,13 +120,11 @@ Which will return an array of groups.

## Tests

Full code coverage unit tests are available in the `tests` folder. Run via phpunit:

`vendor\bin\phpunit tests`
Full code coverage unit tests are available in the tests folder. Run via phpunit:

Or if you are interested in the (html) coverage report:
`vendor\bin\phpunit`

`vendor\bin\phpunit tests --coverage-html report --whitelist src`
By default a coverage report will be generated in the build/coverage folder.

## Contribution

Expand Down
14 changes: 8 additions & 6 deletions src/Adapters/QueryBuilder.php → src/Adapters/FilterAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Vdhicts\Dicms\Filter\Adapters;

use Illuminate\Database\Query\Builder;
use Vdhicts\Dicms\Filter\Contracts\FilterAdapter;
use Vdhicts\Dicms\Filter\Contracts;
use Vdhicts\Dicms\Filter\Filter;
use Vdhicts\Dicms\Filter\Field;
use Vdhicts\Dicms\Filter\Group;

class QueryBuilder implements FilterAdapter
class FilterAdapter implements Contracts\FilterAdapter
{
/**
* Returns the filter field operator.
Expand Down Expand Up @@ -44,37 +44,39 @@ public function getFilterFieldQuery(Builder $query, Field $field, $method = 'and
{
switch($field->getApproval()) {
case Field::APPROVAL_NOT_IN :
$query->whereNotIn(
$query = $query->whereNotIn(
$field->getOption(),
$field->getValue(),
$method
);
break;
case Field::APPROVAL_IN :
$query->whereIn(
$query = $query->whereIn(
$field->getOption(),
$field->getValue(),
$method
);
break;
case Field::APPROVAL_LIKE :
case Field::APPROVAL_ILIKE :
$query->where(
$query = $query->where(
$field->getOption(),
$this->getFilterFieldOperator($field->getApproval()),
sprintf('%%%s%%', $field->getValue()),
$method
);
break;
default :
$query->where(
$query = $query->where(
$field->getOption(),
$this->getFilterFieldOperator($field->getApproval()),
$field->getValue(),
$method
);
break;
}

return $query;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/Adapters/OrderAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Vdhicts\Dicms\Filter;

use Vdhicts\Dicms\Filter\Contracts;

class OrderAdapter implements Contracts\OrderAdapter
{
/**
* Returns the query builder with the order applied.
* @param mixed $builder
* @param Order $order
* @return mixed
*/
public function getOrderQuery($builder, Order $order)
{
foreach ($order->get() as $orderField) {
/** @var OrderField $orderField */
$builder = $builder->orderBy($orderField->getField(), $orderField->getDirection());
}

return $builder;
}
}
20 changes: 20 additions & 0 deletions src/Adapters/PaginationAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Vdhicts\Dicms\Filter;

use Vdhicts\Dicms\Filter\Contracts;

class PaginationAdapter implements Contracts\PaginationAdapter
{
/**
* Returns the query builder with the pagination applied.
* @param mixed $builder
* @param Pagination $pagination
* @return mixed
*/
public function getPaginationQuery($builder, Pagination $pagination)
{
return $builder->limit($pagination->getLimit())
->offset($pagination->getOffset());
}
}
16 changes: 16 additions & 0 deletions src/Contracts/OrderAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Vdhicts\Dicms\Filter\Contracts;

use Vdhicts\Dicms\Filter\Order;

interface OrderAdapter
{
/**
* Returns the query builder with the order applied.
* @param mixed $builder
* @param Order $order
* @return mixed
*/
public function getOrderQuery($builder, Order $order);
}
16 changes: 16 additions & 0 deletions src/Contracts/PaginationAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Vdhicts\Dicms\Filter\Contracts;

use Vdhicts\Dicms\Filter\Pagination;

interface PaginationAdapter
{
/**
* Returns the query builder with the pagination applied.
* @param mixed $builder
* @param Pagination $pagination
* @return mixed
*/
public function getPaginationQuery($builder, Pagination $pagination);
}
10 changes: 10 additions & 0 deletions src/Exceptions/DuplicatedGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vdhicts\Dicms\Filter\Exceptions;

use Vdhicts\Dicms\Filter\FilterException;

class DuplicatedGroup extends FilterException
{

}
10 changes: 0 additions & 10 deletions src/Exceptions/DuplicatedGroupException.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Exceptions/InvalidApproval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vdhicts\Dicms\Filter\Exceptions;

use Vdhicts\Dicms\Filter\FilterException;

class InvalidApproval extends FilterException
{

}
10 changes: 0 additions & 10 deletions src/Exceptions/InvalidApprovalException.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Exceptions/InvalidOrderDirection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Vdhicts\Dicms\Filter\Exceptions;

use Exception;
use Vdhicts\Dicms\Filter\FilterException;

class InvalidOrderDirection extends FilterException
{
/**
* InvalidOrderDirection constructor.
* @param string $direction
* @param array $supportedDirections
* @param Exception|null $previous
*/
public function __construct($direction, array $supportedDirections, Exception $previous = null)
{
parent::__construct(
sprintf(
'Direction `%s` not supported, please use on of the `%s` directions',
$direction,
implode(', ', $supportedDirections)
),
0,
$previous
);
}
}
23 changes: 23 additions & 0 deletions src/Exceptions/InvalidOrderField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Vdhicts\Dicms\Filter\Exceptions;

use Exception;
use Vdhicts\Dicms\Filter\FilterException;

class InvalidOrderField extends FilterException
{
/**
* InvalidOrderField constructor.
* @param string $field
* @param Exception|null $previous
*/
public function __construct($field, Exception $previous = null)
{
parent::__construct(
sprintf('Invalid field provided, it must be a string and filled'),
0,
$previous
);
}
}
6 changes: 3 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Vdhicts\Dicms\Filter;

use Vdhicts\Dicms\Filter\Contracts\Arrayable;
use Vdhicts\Dicms\Filter\Exceptions\InvalidApprovalException;
use Vdhicts\Dicms\Filter\Exceptions\InvalidApproval;

class Field implements Arrayable
{
Expand Down Expand Up @@ -102,7 +102,7 @@ public function getApproval()
* Stores if the value should be accepted or rejected.
* @param int $approval
* @return Field
* @throws InvalidApprovalException
* @throws InvalidApproval
*/
private function setApproval($approval)
{
Expand All @@ -117,7 +117,7 @@ private function setApproval($approval)
self::APPROVAL_ILIKE,
];
if (! in_array($approval, $availableApprovals)) {
throw new InvalidApprovalException(sprintf('Approval "%s" is not supported', $approval));
throw new InvalidApproval(sprintf('Approval "%s" is not supported', $approval));
}

$this->approval = $approval;
Expand Down
Loading

0 comments on commit aebff37

Please sign in to comment.