Skip to content

Repository files navigation

Memio's Validator

A validator library for Memio: allows to define Constraints to check if the built Memio models are valid (e.g. Method cannot be both abstract and final).

Note: This package is part of Memio, a highly opinionated PHP code generator. Have a look at the main repository.

Installation

Install it using Composer:

$ composer require memio/validator:^3.0

Example

Let's say we want to check that Arguments aren't scalar. In order to do so, the first thing we'll need to do is to write a Constraint:

<?phprequire__DIR__.'/vendor/autoload.php';
useMemio\Validator\Constraint;
useMemio\Validator\Violation\NoneViolation;
useMemio\Validator\Violation\SomeViolation;
class ArgumentCannotBeScalar implements Constraint
{
publicfunctionvalidate($model)
{
$type = $model->getType();
if (in_array($type, ['array', 'bool', 'callable', 'double', 'int', 'mixed', 'null', 'resource', 'string'], true)) {
returnnewSomeViolation(sprintf('Argument "%s" cannot be scalar', $model->getName()));
}
returnnewNoneViolation();
}
}

Note: In Memio, all Constraints are named after their error message. This isn't a hard coded rule, they can have any name.

We then need to register our rule in an ArgumentValidator:

// ...$argumentValidator = newArgumentValidator();
$argumentValidator->add(newArgumentCannotBeScalar());

ArgumentValidator is a ModelValidator called by Validator if the given model is an Argument. However, if the given model is a Method we'd like Validator to check our Constraint against its Arguments. To do so, we need to assemble ModelValidators as follow:

// ...$collectionValidator = newCollectionValidator();
$methodValidator = newMethodValidator($argumentValidator, $collectionValidator);
$contractValidator = newContractValidator($collectionValidator, $methodValidator);
$objectValidator = newObjectValidator($collectionValidator, $methodValidator);
$fileValidator = newFileValidator($contractValidator, $objectValidator);

Finally, we need to create a validator and register our ModelValidators in it:

// ...$myValidator = newValidator();
$myValidator->add($argumentValidator);
$myValidator->add($collectionValidator);
$myValidator->add($methodValidator);
$myValidator->add($contractValidator);
$myValidator->add($objectValidator);
$myValidator->add($fileValidator);

This way we can build specialized validators: one that'd check syntax errors, one that'd check business rules, etc... Possibilities are endless!

Have a look at the main respository to discover the full power of Medio.

Want to know more?

Memio uses phpspec, which means the tests also provide the documentation. Not convinced? Then clone this repository and run the following commands:

make lib-init # Set up Docker environmentmake phpspec arg='--format pretty' # Run the specifications

Note: Run make or make help to see all available commands.

You can see the current and past versions using one of the following:

And finally some meta documentation:

About

[sub] A validator library to check Memio Models

Resources

Contributing

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages