bukashk0zzz/filter-bundle

Symfony 过滤器包。

安装量: 386,083

依赖者: 1

建议者: 0

安全: 0

星标: 24

关注者: 6

分支: 5

类型:symfony-bundle

v5.0.1 2023-01-16 08:32 UTC

README

Code Coverage License Latest Stable Version Total Downloads

关于

此包添加了一个服务,可以用于根据注解过滤对象值。使用旧的 Laminas 过滤器ZendFilters 进行过滤。如果找到带有注解的实体,包还可以过滤你的表单。如果启用 auto_filter_forms,实体将在验证之前被过滤。Laminas 过滤器文档

使用 Symfony Flex 安装

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

不使用 Symfony Flex 安装

composer require bukashk0zzz/filter-bundle

将包添加到 app/AppKernel.php

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

配置

将此添加到你的 config.yml

bukashk0zzz_filter:
    # Enable if you need auto filtering form data before constraint(Validation) check
    auto_filter_forms: false

用法

该包提供了一个注解,允许在实体中过滤字段。

将以下类添加到你的实体类中的 use 部分。

use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation as Filter;

注解 @Filter 有一个必需的选项 filter,其值应为 Laminas 过滤器类的名称。它可以设置为 @Filter("StringTrim")@Filter(filter="AppBundle\Filter\MyCustomFilter")

AppBundle\Filter\MyCustomFilter - 在此示例中,必须是一个继承自 \Laminas\Filter\AbstractFilter 的类

还有一个非必需的选项 options - 它必须是数组类型,并且将传递给 Laminas 过滤器,使用 Laminas 过滤器的 setOptions 方法。

示例实体

<?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;
}

使用过滤器服务

使用 bukashk0zzz_filter.filter 服务以及实体中的注解来过滤数据。

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];
}

版权 / 许可证

LICENSE