Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

final class Filter
{
/** @var array<int|string, FormInterface> */
private array $forms = [];

public function __construct(
private readonly FormFactoryInterface $formFactory,
private readonly RequestStack $requestStack,
Expand All @@ -31,12 +28,16 @@ public function __construct(
public function filter(string $name): array
{
$filter = [];
$fname = $name.$this->getSession()->getId();
/** @var array<string, mixed>|null $values */
$values = $this->getSession()->get('filter.'.$name);
if (null !== $values) {
if ($this->forms[$fname]->isSubmitted() || $this->forms[$fname]->submit($values)->isValid()) {
$filter = \array_filter($values, static fn ($value): bool => '' !== $value);
/** @var array{type: string, options: array<string, mixed>}|null $meta */
$meta = $this->getSession()->get('filter_meta.'.$name);
if (null !== $meta) {
$form = $this->formFactory->create($meta['type'], null, $meta['options']);
if ($this->getRequest()->query->has('submit-filter') || $form->submit($values)->isValid()) {
$filter = \array_filter($values, static fn ($value): bool => '' !== $value);
}
}
}
if ([] !== ($sort = $this->getSort($name))) {
Expand Down Expand Up @@ -76,8 +77,7 @@ public function getFormView(string $name, ?string $type = null): FormView
*/
public function saveFilter(string $type, string $name, array $defaults = [], array $options = []): bool
{
$fname = $name.$this->getSession()->getId();
$this->forms[$fname] = $this->formFactory->create($type, null, $options);
$this->getSession()->set('filter_meta.'.$name, ['type' => $type, 'options' => $options]);
if ($this->getRequest()->query->has('reset-filter')) {
$this->getSession()->set('filter.'.$name, null);

Expand All @@ -91,9 +91,10 @@ public function saveFilter(string $type, string $name, array $defaults = [], arr
if (!$this->getRequest()->query->has('submit-filter')) {
return false;
}
$this->forms[$fname]->handleRequest($this->getRequest());
if ($this->forms[$fname]->isSubmitted() && $this->forms[$fname]->isValid()) {
$this->getSession()->set('filter.'.$name, $this->getRequest()->query->all()[$this->forms[$fname]->getName()]);
$form = $this->formFactory->create($type, null, $options);
$form->handleRequest($this->getRequest());
if ($form->isSubmitted() && $form->isValid()) {
$this->getSession()->set('filter.'.$name, $this->getRequest()->query->all()[$form->getName()]);

return true;
}
Expand All @@ -114,9 +115,17 @@ public function sort(string $name, string $field, string $direction = 'ASC'): vo
*/
private function getForm(string $name, ?string $type = null): FormInterface
{
$name .= $this->getSession()->getId();
/** @var array{type: string, options: array<string, mixed>}|null $meta */
$meta = $this->getSession()->get('filter_meta.'.$name);
if (null === $meta) {
return $this->formFactory->create($type ?? FormType::class);
}
$form = $this->formFactory->create($type ?? $meta['type'], null, $meta['options']);
if ($this->getRequest()->query->has('submit-filter')) {
$form->handleRequest($this->getRequest());
}

return $this->forms[$name] ?? $this->formFactory->create($type ?? FormType::class);
return $form;
}

private function getRequest(): Request
Expand Down