Skip to content

Commit

Permalink
delegate filter query by empty value to filters
Browse files Browse the repository at this point in the history
extract code from Datagrid::buildPager() into separate methods to improve readability
  • Loading branch information
peter-gribanov committed Sep 23, 2020
1 parent c39000a commit 1e38aa1
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,54 +146,73 @@ static function ($value) {
$this->form = $this->formBuilder->getForm();
$this->form->submit($this->values);

$data = $this->form->getData();
$this->applyFilters($this->form->getData() ?? []);
$this->applySorting();

$this->pager->setMaxPerPage($this->getMaxPerPage(25));
$this->pager->setPage($this->getPage(1));
$this->pager->setQuery($this->query);
$this->pager->init();

$this->bound = true;
}

private function applyFilters(array $data): void
{
foreach ($this->getFilters() as $name => $filter) {
$this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
$filterFormName = $filter->getFormName();
if (isset($this->values[$filterFormName]['value']) && '' !== $this->values[$filterFormName]['value']) {
if (isset($this->values[$filterFormName]['type'], $this->values[$filterFormName]['value']) &&
('' !== $this->values[$filterFormName]['type'] || '' !== $this->values[$filterFormName]['value'])
) {
$filter->apply($this->query, $data[$filterFormName]);
}
}
}

private function applySorting(): void
{
if (isset($this->values['_sort_by'])) {
if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class);
}

if ($this->values['_sort_by']->isSortable()) {
$this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping());
$this->query->setSortBy(
$this->values['_sort_by']->getSortParentAssociationMapping(),
$this->values['_sort_by']->getSortFieldMapping()
);

$this->values['_sort_order'] = $this->values['_sort_order'] ?? 'ASC';
$this->query->setSortOrder($this->values['_sort_order']);
}
}
}

$maxPerPage = 25;
private function getMaxPerPage(int $default): int
{
if (isset($this->values['_per_page'])) {
if (isset($this->values['_per_page']['value'])) {
$maxPerPage = $this->values['_per_page']['value'];
} else {
$maxPerPage = $this->values['_per_page'];
return (int) $this->values['_per_page']['value'];
}

return (int) $this->values['_per_page'];
}
$this->pager->setMaxPerPage($maxPerPage);

$page = 1;
return $default;
}

private function getPage(int $default): int
{
if (isset($this->values['_page'])) {
if (isset($this->values['_page']['value'])) {
$page = $this->values['_page']['value'];
} else {
$page = $this->values['_page'];
return (int) $this->values['_page']['value'];
}
}

$this->pager->setPage($page);

$this->pager->setQuery($this->query);
$this->pager->init();
return (int) $this->values['_page'];
}

$this->bound = true;
return $default;
}

public function addFilter(FilterInterface $filter)
Expand Down

0 comments on commit 1e38aa1

Please sign in to comment.