eureka / component-validation
PHP validation library.
Installs: 1 072
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: 7.4.*||8.0.*||8.1.*||8.2.*||8.3.*
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpcov: ^8.2
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
- symfony/cache: ^5.4||^6.3
This package is auto-updated.
Last update: 2024-11-06 18:52:42 UTC
README
Validation Component
Usage
Validator Factory
Validation component use php native filter_var()
to validate contents.
Please refer to Validate filters and
filter_var() for more information about options & flags.
<?php use Eureka\Component\Validation\ValidatorFactory; $factory = new ValidatorFactory(); $string = 'message'; $validatedString = $factory->getValidator('string')->validate($string); $validatedEmail = $factory->getValidator('email')->validate('test@example.com'); //...
Generic Entity
Validation component could be used to validate form. But to avoid set an invalid domain entity (a domain Entity should always be complete and valid), you can use generic entity factory.
This factory can create a pseudo entity with auto validation, and create or update a domain Entity only if the generic entity is valid.
<?php use Eureka\Component\Validation\Entity\ValidatorEntityFactory; use Eureka\Component\Validation\ValidatorFactory; use Eureka\Component\Validation\Validator\IntegerValidator; //~ Key as formatted in PascalCase in generic entity $formData = [ 'userId' => 1, 'userName' => 'Romain', 'user_email' => 'test@example.com', 'IsEnabled' => true, ]; //~ Key as formatted in PascalCase in generic entity also for the config $validatorConfig = [ 'user_id' => ['type' => 'integer', 'options' => IntegerValidator::INT_UNSIGNED], 'UserEmail' => ['type' => 'email'], ]; $entityFactory = new ValidatorEntityFactory(new ValidatorFactory()); $formEntity = $entityFactory->createGeneric($validatorConfig, $formData); if (!$formEntity->isValid()) { throw new \RuntimeException(implode("\n", $formEntity->getErrors())); } $user = new User(); $user->setId($formEntity->getUserId()); $user->setName($formEntity->getUserName()); $user->setEmail($formEntity->getUserEmail()); $user->setIsEnabled($formEntity->isEnabled()); // and persist user in database
Composer
composer require "eureka/component-validation"
Installation
You can install the component (for testing) with the following command:
make install
Update
You can update the component (for testing) with the following command:
make update
Testing
You can test the component with the following commands:
make phpcs make tests make testdox