lsbproject / blacklist-bundle
Bundle to simplify blacklist creation using annotations
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 4
Type:symfony-bundle
Requires
- php: ^7.1
- doctrine/common: ^2.3
- symfony/framework-bundle: ^3.0 || ^4.0
- symfony/validator: ^3.0 || ^4.0
Requires (Dev)
- doctrine/doctrine-bundle: ^1.11
- doctrine/orm: ^2.6
- phpstan/phpstan: ^0.10.1
- phpstan/phpstan-doctrine: ^0.10.0
- phpstan/phpstan-phpunit: ^0.10.0
- sensio/framework-extra-bundle: ^5.4
- slevomat/coding-standard: ~4.0
- squizlabs/php_codesniffer: *
- symfony/class-loader: ^3.4
- symfony/phpunit-bridge: ^3.0 || ^4.0
- symfony/yaml: ^3.0 || ^4.0
This package is auto-updated.
Last update: 2023-06-13 03:27:30 UTC
README
Flexible bundle to handle blacklist with annotations. Highly inspired by https://github.com/AntoineLemaire/BlacklistBundle
Installation
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
$ composer require lsbproject/blacklist-bundle
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require lsbproject/blacklist-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php return [ // ... LSBProject\BlacklistBundle\LSBProjectBlacklistBundle::class => ['all' => true], ];
Step 3: Update database schema
php bin/console doctrine:schema:update --force
Usage
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;
Types
Bundle tries to validate exact blacklist type with validator types.
You can implement your own type or use default one.
To add your own validator just implement TypeInterface
e.g.
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'; } }
and tag it with lsbproject.blacklist.type
email_blacklist_type: class: 'LSBProject\BlacklistBundle\Type\EmailType' tags: - { name: 'lsbproject.blacklist.type' }
Default
If there are no supported types found bundle will use default type. You can override it in config:
lsb_project_blacklist: default_type: LSBProject\BlacklistBundle\Type\DefaultType
Validate storage
If you do not want to use database as a storage for blacklist you
can implement your own validate
method for a separate or default types.
example of default 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(); } } }