Skip to content

Commit

Permalink
feature: add a filter factory to allow resource collection to be filt…
Browse files Browse the repository at this point in the history
…ered
  • Loading branch information
johnkrovitch committed Oct 28, 2023
1 parent b628629 commit 3940a33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/Filter/Factory/EventFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public function __construct(
) {
}

public function create(FilterInterface $filter): FilterInterface
public function create(FilterInterface $filterDefinition): FilterInterface
{
$event = new FilterEvent($filter);
$event = new FilterEvent($filterDefinition);
$this->eventDispatcher->dispatch($event, FilterEvent::FILTER_CREATE);
$this->eventDispatcher->dispatch($event, sprintf(FilterEvent::FILTER_CREATE_PATTERN, $filter->getName()));
$this->eventDispatcher->dispatch($event, sprintf(FilterEvent::FILTER_CREATE_PATTERN, $filterDefinition->getName()));

$filter = $this->decorated->create($filter);
$filterDefinition = $this->decorated->create($filterDefinition);

$event = new FilterEvent($filter);
$event = new FilterEvent($filterDefinition);
$this->eventDispatcher->dispatch($event, FilterEvent::FILTER_CREATED);
$this->eventDispatcher->dispatch($event, sprintf(FilterEvent::FILTER_CREATED_PATTERN, $filter->getName()));
$this->eventDispatcher->dispatch($event, sprintf(FilterEvent::FILTER_CREATED_PATTERN, $filterDefinition->getName()));

return $filter;
return $filterDefinition;
}

public function createFromProperty(PropertyInterface $property): FilterInterface
Expand Down
20 changes: 16 additions & 4 deletions src/Filter/Factory/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use LAG\AdminBundle\Metadata\Filter\FilterInterface;
use LAG\AdminBundle\Metadata\Filter\StringFilter;
use LAG\AdminBundle\Metadata\Property\PropertyInterface;
use LAG\AdminBundle\Metadata\Property\StringProperty;
use LAG\AdminBundle\Metadata\Property\Text;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Validator\ValidatorInterface;

Expand All @@ -20,8 +20,9 @@ public function __construct(
) {
}

public function create(FilterInterface $filter): FilterInterface
public function create(FilterInterface $filterDefinition): FilterInterface
{
$filter = $this->initializeFilter($filterDefinition);
$errors = $this->validator->validate($filter, [new Valid()]);

if ($errors->count() > 0) {
Expand All @@ -36,13 +37,24 @@ public function createFromProperty(PropertyInterface $property): FilterInterface
$class = Filter::class;
$name = $property->getName();

if ($property instanceof StringProperty) {
if ($property instanceof Text) {
$class = StringFilter::class;
}

return new $class(
$definition = new $class(
name: $name,
propertyPath: $property->getPropertyPath(),
);

return $this->create($definition);
}

private function initializeFilter(FilterInterface $filter): FilterInterface
{
if ($filter->getPropertyPath() === null) {
$filter = $filter->withPropertyPath($filter->getName());
}

return $filter;
}
}
2 changes: 1 addition & 1 deletion src/Filter/Factory/FilterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

interface FilterFactoryInterface
{
public function create(FilterInterface $filter): FilterInterface;
public function create(FilterInterface $filterDefinition): FilterInterface;

public function createFromProperty(PropertyInterface $property): FilterInterface;
}

0 comments on commit 3940a33

Please sign in to comment.