PHP code example of eureka / component-validation

1. Go to this page and download the library: Download eureka/component-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eureka / component-validation example snippets




use Eureka\Component\Validation\ValidatorFactory;

$factory = new ValidatorFactory();

$string = 'message';

$validatedString = $factory->getValidator('string')->validate($string);
$validatedEmail  = $factory->getValidator('email')->validate('[email protected]');
//...



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' => '[email protected]',
    '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