项目作者: Bukashk0zzz

项目描述 :
Symfony filtering bundle
高级语言: PHP
项目地址: git://github.com/Bukashk0zzz/FilterBundle.git
创建时间: 2016-03-16T08:52:30Z
项目社区:https://github.com/Bukashk0zzz/FilterBundle

开源协议:MIT License

下载


Symfony Filter Bundle

Code Coverage
License
Latest Stable Version
Total Downloads

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

  1. composer config extra.symfony.allow-contrib true
  2. composer require bukashk0zzz/filter-bundle

Installation without Symfony Flex

  1. composer require bukashk0zzz/filter-bundle

Add the bundle to app/AppKernel.php

  1. $bundles = array(
  2. // ... other bundles
  3. new Bukashk0zzz\FilterBundle\Bukashk0zzzFilterBundle(),
  4. );

Configuration

Add this to your config.yml:

  1. bukashk0zzz_filter:
  2. # Enable if you need auto filtering form data before constraint(Validation) check
  3. 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.

  1. 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

  1. <?php
  2. namespace AppBundle\Entity;
  3. use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation as Filter;
  4. /**
  5. * User Entity
  6. */
  7. class User
  8. {
  9. #[Filter(parameters: [
  10. 'filter' => 'StripTags',
  11. 'options' => ['allowTags' => 'br']
  12. ])]
  13. #[Filter(parameters: ['filter' => 'StringTrim'])]
  14. #[Filter(parameters: ['filter' => 'StripNewlines'])]
  15. protected $name;
  16. #[Filter(parameters: ['filter' => 'StripTags'])]
  17. #[Filter(parameters: ['filter' => 'StringTrim'])]
  18. #[Filter(parameters: ['filter' => 'AppBundle\Filter\MyCustomFilter'])]
  19. protected $about;
  20. }

Using filter service

Use the bukashk0zzz_filter.filter service along with annotations in the Entity to filter data.

  1. public function indexAction()
  2. {
  3. $entity = new \Acme\DemoBundle\Entity\SampleEntity();
  4. $entity->name = "My <b>name</b>";
  5. $entity->email = " email@mail.com";
  6. $filterService = $this->get('bukashk0zzz_filter.filter');
  7. $filterService->filterEntity($entity);
  8. return ['entity' => $entity];
  9. }

See LICENSE