Installation
composer require bukashk0zzz/filter-bundle
For Symfony Flex projects, ensure allow-contrib is enabled:
composer config extra.symfony.allow-contrib true
Register the bundle in config/bundles.php (Symfony 4.3+):
Bukashk0zzz\FilterBundle\FilterBundle::class => ['all' => true],
First Use Case: Filtering an Entity
Annotate a property in your entity (e.g., User.php) with @Filter:
use Bukashk0zzz\FilterBundle\Annotation\Filter;
/**
* @Filter("StringTrim")
*/
private $name;
Inject the filter_service into a controller/service:
use Bukashk0zzz\FilterBundle\Service\FilterService;
public function __construct(private FilterService $filterService) {}
public function applyFilter(User $user): void
{
$this->filterService->filter($user);
}
Key Configuration
Enable form auto-filtering in config/packages/bukashk0zzz_filter.yaml:
bukashk0zzz_filter:
auto_filter_forms: true
Entity Filtering
filter() method to apply filters to entities:
$filteredEntity = $this->filterService->filter($entity);
$filteredEntities = $this->filterService->filter($entities);
Form Integration
auto_filter_forms to filter entities before validation (e.g., trimming whitespace from form data).$formData = $this->filterService->filter($formData);
$form->submit($formData);
Custom Filters
Laminas\Filter\FilterInterface and bind them in services:
# config/services.yaml
services:
App\Filter\CustomFilter:
tags: ['bukashk0zzz.filter.filter']
/**
* @Filter("custom_filter_name")
*/
private $customField;
Conditional Filtering
@Filter(skip=true) to disable filtering for a property:
/**
* @Filter("StringTrim", skip=true)
*/
private $skipTrimming;
Bukashk0zzz\FilterBundle\Filter\ConditionalFilterInterface for dynamic rules.Event Listeners
kernel.request to filter input data globally:
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$data = $request->request->all();
$filteredData = $this->filterService->filter($data);
$request->request->replace($filteredData);
}
Doctrine Integration
$entityManager->persist($this->filterService->filter($entity));
API Requests
$dto = $this->filterService->filter($request->getContent());
Testing
FilterService in tests to isolate filtering logic:
$this->filterService->expects($this->once())
->method('filter')
->with($entity);
Performance Overhead
@Filter selectively or disable auto_filter_forms for non-critical forms.Circular References
User ↔ Profile) may cause infinite loops.@Filter(skip=true) for circularly referenced properties or implement depth limits.Form Validation Conflicts
@Assert\NotBlank with @Filter("StringTrim") cautiously.Filter Order
@Filter({"StringTrim", "StringToUpper"})).Deprecated Laminas Filters
ZendFilter names (e.g., Zend\Filter\StringTrim) may not work; use Laminas equivalents (e.g., StringTrim).Enable Debug Mode
debug: true in config/packages/bukashk0zzz_filter.yaml to log filtered properties:
bukashk0zzz_filter:
debug: true
Inspect Filtered Data
var_dump($this->filterService->getFilteredProperties($entity)) to see which properties were modified.Common Errors
FilterNotFoundException: Ensure the filter name matches a registered Laminas filter or a custom service tagged with bukashk0zzz.filter.filter.InvalidArgumentException: Verify the annotated property exists in the entity class.Custom Filter Providers
Bukashk0zzz\FilterBundle\Filter\FilterProviderInterface to dynamically register filters based on runtime conditions.Annotation Overrides
bukashk0zzz_filter:
overrides:
App\Entity\User:
name: ["StringTrim", "StringToUpper"]
Event-Driven Filtering
$this->filterService->filter($entity, new FilterEvent($entity));
Batch Processing
FilterService to support chunked filtering for large datasets:
$this->filterService->filterBatch($entities, 100);
Non-Entity Filtering
filter() method with arrays or objects without annotations:
$data = [
'name' => ' Alice ',
'age' => '30',
];
$filtered = $this->filterService->filter($data, [
'name' => ['StringTrim'],
'age' => ['Int'],
]);
How can I help you explore Laravel packages today?