lsbproject/blacklist-bundle

此包已被弃用且不再维护。未建议替代包。

使用注解简化黑名单创建的包

安装: 0

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 4

类型:symfony-bundle

dev-master 2019-10-08 15:25 UTC

README

一个灵活的包,用于使用注解处理黑名单。高度受到 https://github.com/AntoineLemaire/BlacklistBundle 的启发

安装

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录并执行

$ composer require lsbproject/blacklist-bundle

不使用 Symfony Flex 的应用程序

步骤 1: 下载包

打开命令行,进入您的项目目录并执行以下命令以下载此包的最新稳定版本

$ composer require lsbproject/blacklist-bundle

此命令需要您全局安装了 Composer,如 Composer 文档中的安装章节所述。

步骤 2: 启用包

然后,通过将其添加到项目 config/bundles.php 文件中注册的包列表中来启用该包

// config/bundles.php

return [
    // ...
    LSBProject\BlacklistBundle\LSBProjectBlacklistBundle::class => ['all' => true],
];

步骤 3: 更新数据库模式

php bin/console doctrine:schema:update --force

用法

    use LSBProject\BlacklistBundle\Validator\Constraints\IsNotBlacklisted;

    //...

    /**
     * 'baz' type isn't defined in bundle, so it will be handled with
     * default_type class. Default one has no validation and will compare
     * any value with other existed
     *
     * @IsNotBlacklisted(type="baz", caseSensetive=true)
     * @var string
     */
    private $bar;

    /**
     * 'email' type will dissallow to put invalid emails in blacklist
     *
     * @IsNotBlacklisted(type="email", caseSensetive=true)
     * @var string
     */
    private $email;

类型

包尝试使用验证器类型验证精确的黑名单类型。您可以实现自己的类型或使用默认类型。要添加自己的验证器,只需实现 TypeInterface

例如。

use LSBProject\BlacklistBundle\Type\TypeInterface;
use LSBProject\BlacklistBundle\Type\DefaultType;

class EmailType extends DefaultType implements TypeInterface
{
    /**
     * {@inheritDoc}
     */
    public function satisfies(string $value): bool
    {
        return filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    /**
     * {@inheritDoc}
     */
    public function supports(string $type): bool
    {
        return $type === 'email';
    }
}

并将其标记为 lsbproject.blacklist.type

  email_blacklist_type:
    class: 'LSBProject\BlacklistBundle\Type\EmailType'
    tags:
      - { name: 'lsbproject.blacklist.type' }

默认

如果没有找到支持的类型,则包将使用默认类型。您可以在配置中覆盖它

    lsb_project_blacklist:
      default_type: LSBProject\BlacklistBundle\Type\DefaultType

验证存储

如果您不想将数据库用作黑名单的存储,您可以为单独的或默认类型实现自己的 validate 方法。

默认 validate 的示例

class DefaultType implements TypeInterface
{
    //...    

    /**
     * {@inheritDoc}
     */
    public function validate(
        string $value,
        Constraint $constraint,
        ExecutionContextInterface &$context,
        BlacklistManagerInterface $manager
    ): void {
        if ($manager->isBlacklisted($value, $constraint->type, $constraint->caseSensetive)) {
            $context->buildViolation($constraint->message)->addViolation();
        }
    }
}