memio/validator

Memio's validator, used to check built models again a pre-defined set of rules

v3.0.1 2024-08-13 06:06 UTC

This package is auto-updated.

Last update: 2024-12-13 06:51:01 UTC


README

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:

<?php

require __DIR__.'/vendor/autoload.php';

use Memio\Validator\Constraint;
use Memio\Validator\Violation\NoneViolation;
use Memio\Validator\Violation\SomeViolation;

class ArgumentCannotBeScalar implements Constraint
{
    public function validate($model)
    {
        $type = $model->getType();
        if (in_array($type, ['array', 'bool', 'callable', 'double', 'int', 'mixed', 'null', 'resource', 'string'], true)) {
            return new SomeViolation(sprintf('Argument "%s" cannot be scalar', $model->getName()));
        }

        return new NoneViolation();
    }
}

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 = new ArgumentValidator();
$argumentValidator->add(new ArgumentCannotBeScalar());

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 = new CollectionValidator();
$methodValidator = new MethodValidator($argumentValidator, $collectionValidator);
$contractValidator = new ContractValidator($collectionValidator, $methodValidator);
$objectValidator = new ObjectValidator($collectionValidator, $methodValidator);
$fileValidator = new FileValidator($contractValidator, $objectValidator);

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

// ...

$myValidator = new Validator();
$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:

$ composer install -o
$ ./vendor/bin/phpspec run -n -f pretty

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

And finally some meta documentation: