Skip to content

Commit

Permalink
Merge adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdheiden committed Oct 30, 2017
1 parent 3fed6da commit 0b30f77
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
44 changes: 42 additions & 2 deletions src/Adapters/FilterAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Vdhicts\Dicms\Filter\Filter;
use Vdhicts\Dicms\Filter\Field;
use Vdhicts\Dicms\Filter\Group;
use Vdhicts\Dicms\Filter\Order;
use Vdhicts\Dicms\Filter\OrderField;
use Vdhicts\Dicms\Filter\Pagination;

class FilterAdapter implements Contracts\FilterAdapter
class FilterAdapter implements Contracts\FilterAdapter, Contracts\OrderAdapter, Contracts\PaginationAdapter
{
/**
* Returns the filter field operator.
Expand Down Expand Up @@ -100,6 +103,34 @@ public function getFilterGroupQuery(Builder $builder, Group $group)
});
}

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

return $builder;
}

/**
* Returns the query builder with the pagination applied.
* @param mixed $builder
* @param Pagination $pagination
* @return mixed
*/
private function getPaginationQuery($builder, Pagination $pagination)
{
return $builder->limit($pagination->getLimit())
->offset($pagination->getOffset());
}

/**
* Returns the eloquent query builder with the filter applied.
* @param Builder $builder
Expand All @@ -110,10 +141,19 @@ public function getFilterQuery($builder, Filter $filter)
{
$adapter = $this;

return $builder->where(function(Builder $query) use($adapter, $filter) {
// Apply the filter
$queryBuilder = $builder->where(function(Builder $query) use($adapter, $filter) {
foreach ($filter->getGroups() as $group) {
$query = $adapter->getFilterGroupQuery($query, $group);
}
});

// Apply the sort order
$queryBuilder = $this->getOrderQuery($queryBuilder, $filter->getOrder());

// Apply the pagination
$queryBuilder = $this->getPaginationQuery($queryBuilder, $filter->getPagination());

return $queryBuilder;
}
}
26 changes: 0 additions & 26 deletions src/Adapters/OrderAdapter.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Adapters/PaginationAdapter.php

This file was deleted.

0 comments on commit 0b30f77

Please sign in to comment.