2017 © Pedro PelĆ”ez
 

library validator

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

image

memio/validator

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

  • Friday, March 2, 2018
  • by gnupat
  • Repository
  • 1 Watchers
  • 2 Stars
  • 118,302 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 8 % Grown

The README.md

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)., (*1)

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

Installation

Install it using Composer:, (*3)

$ 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:, (*4)

<?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., (*5)

We then need to register our rule in an ArgumentValidator:, (*6)

// ...

$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:, (*7)

// ...

$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:, (*8)

// ...

$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!, (*9)

Have a look at the main respository to discover the full power of Medio., (*10)

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:, (*11)

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

You can see the current and past versions using one of the following:, (*12)

And finally some meta documentation:, (*13)

The Versions

02/03 2018

dev-master

9999999-dev http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

02/03 2018

v2.0.3

2.0.3.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

27/09 2016

dev-version-2

dev-version-2 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

27/09 2016

v2.0.0-alpha2

2.0.0.0-alpha2 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

27/09 2016

v2.0.1

2.0.1.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

23/09 2016

v2.0.0-alpha1

2.0.0.0-alpha1 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

01/12 2015

v1.0.2

1.0.2.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

01/12 2015

v2.0.0

2.0.0.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

21/04 2015

v1.0.1

1.0.1.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

20/04 2015

v1.0.0

1.0.0.0 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator

16/04 2015

v1.0.0-rc1

1.0.0.0-RC1 http://memio.github.io/memio

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

  Sources   Download

MIT

The Requires

 

The Development Requires

php code generator