Developed with love by KnpLabs Hire us for your project!
0

FilterBundle

by Bukashk0zzz

Symfony filtering bundle

Symfony Filter Bundle

Build Status
Code Coverage
Scrutinizer Code Quality
Dependency Status
License
Latest Stable Version
Total Downloads

SensioLabsInsight
knpbundles.com

About

This bundle add a service that can be used to filter object values based on annotations. 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.
Zend 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 Zend 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 \Zend\Filter\AbstractFilter

Also there is one not required option options - it must be array type and will pass to Zend filter using setOptions method from zend filter.

Example entity

<?php
namespace AppBundle\Entity;

use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation as Filter;

/**
 * User Entity
 */
class User
{
    /**
     * @Filter("StripTags", options={"allowTags": "br"})
     * @Filter("StringTrim")
     * @Filter("StripNewlines")
     * @var string
     */
    protected $name;

    /**
     * @Filter("StripTags")
     * @Filter("StringTrim")
     * @Filter("AppBundle\Filter\MyCustomFilter")
     * @var string
     */
    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

The MIT License (MIT)

Copyright (c) 2015 Denis Golubovskiy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.