bukashk0zzz / filter-bundle
Symfony filter bundle.
Installs: 402 094
Dependents: 1
Suggesters: 0
Security: 0
Stars: 24
Watchers: 6
Forks: 5
Type:symfony-bundle
Requires
- php: >=8.0.0
- doctrine/orm: ^2.5
- laminas/laminas-filter: ^2.9
- symfony/cache: ^5.4|^6.0
- symfony/config: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/form: ^5.4|^6.0
- symfony/http-kernel: ^5.4|^6.0
- symfony/yaml: ^5.4|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^8.4
- symfony/validator: ^5.4|^6.0
README
About
This bundle add a service that can be used to filter object values based on annotations. Laminas filter old ZendFilters used for filtering.
Also bundle can filter your forms if it finds a annotated entity attached. If auto_filter_forms
enabled entities will be filtered before they are validated.
Laminas filters doc
Installation Symfony Flex
composer config extra.symfony.allow-contrib true
composer require bukashk0zzz/filter-bundle
Installation without Symfony Flex
composer require bukashk0zzz/filter-bundle
Add the bundle to app/AppKernel.php
$bundles = array( // ... other bundles new Bukashk0zzz\FilterBundle\Bukashk0zzzFilterBundle(), );
Configuration
Add this to your config.yml
:
bukashk0zzz_filter: # Enable if you need auto filtering form data before constraint(Validation) check auto_filter_forms: false
Usage
Bundle provides one annotation which allow filter fields in your entities.
Add the next class to the use
section of your entity class.
use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation as Filter;
Annotation @Filter
has one required option filter which value should be name of Laminas filter class.
It can be set like this @Filter("StringTrim")
or @Filter(filter="AppBundle\Filter\MyCustomFilter")
.
AppBundle\Filter\MyCustomFilter
- in this example, must be class that extends \Laminas\Filter\AbstractFilter
Also there is one not required option options
- it must be array type and will pass to Laminas filter using setOptions
method from Laminas filter.
Example entity
<?php namespace AppBundle\Entity; use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation as Filter; /** * User Entity */ class User { #[Filter(parameters: [ 'filter' => 'StripTags', 'options' => ['allowTags' => 'br'] ])] #[Filter(parameters: ['filter' => 'StringTrim'])] #[Filter(parameters: ['filter' => 'StripNewlines'])] protected $name; #[Filter(parameters: ['filter' => 'StripTags'])] #[Filter(parameters: ['filter' => 'StringTrim'])] #[Filter(parameters: ['filter' => 'AppBundle\Filter\MyCustomFilter'])] protected $about; }
Using filter service
Use the bukashk0zzz_filter.filter
service along with annotations in the Entity to filter data.
public function indexAction() { $entity = new \Acme\DemoBundle\Entity\SampleEntity(); $entity->name = "My <b>name</b>"; $entity->email = " email@mail.com"; $filterService = $this->get('bukashk0zzz_filter.filter'); $filterService->filterEntity($entity); return ['entity' => $entity]; }
Copyright / License
See LICENSE