From 1e38aa192b7e2513d9e5906f76044b088fb076fe Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 22 Sep 2020 10:00:27 +0300 Subject: [PATCH] delegate filter query by empty value to filters extract code from Datagrid::buildPager() into separate methods to improve readability --- src/Datagrid/Datagrid.php | 55 ++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Datagrid/Datagrid.php b/src/Datagrid/Datagrid.php index 35680602943..9e106fd8b90 100644 --- a/src/Datagrid/Datagrid.php +++ b/src/Datagrid/Datagrid.php @@ -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)