fivelab/var-tag-validator

This package is abandoned and no longer maintained. No replacement package was suggested.

Validate objects by var tag

v1.0.2 2016-03-21 09:51 UTC

README

With this package you can validate objects by @var tags.

Installation

Add FiveLab/VarTagValidator in your composer.json:

{
    "require": {
        "fivelab/var-tag-validator": "~1.0"
    }
}

Now tell composer to download the library by running the command:

$ php composer.phar update fivelab/var-tag-validator

Basic usage

Basic create VarTagValidator instance:

use FiveLab\Component\VarTagValidator\VarTagValidator;
use FiveLab\Component\VarTagValidator\Metadata\MetadataFactory;
use Symfony\Component\Validator\ValidatorBuilder;

$validator = (new ValidatorBuilder())
    ->getValidator();

$metadataFactory = new MetadataFactory();
$varTagValidator = new VarTagValidator($validator, $metadataFactory);

After create VarTagValidator instance you can validate objects by @var tags:

class MyClass
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $firstName;
}

$object = new MyClass();
$object->id = 1;
$object->firstName = 'Foo Bar';

$violationList = $varTagValidator->validateObjectByVarTags($object);

Attention: VarTagValidator system use Symfony Validator for validate values.

Available @var tag types:

  1. integer, int
  2. float, double
  3. string
  4. array

Custom Var Tag

If you want add custom @var tag type, Money as example, you must create a ConstraintFactory for this type
and add to registry.

Step #1: Create constraint factory

use FiveLab\Component\VarTagValidator\Constraint\ConstraintFactoryInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FiveLab\Component\VarTagValidator\Constraint\VarTagConstraint;

class MoneyConstraintFactory implements ConstraintFactoryInterface
{
    /**
     * {@inheritDoc}
     */
    public function getVarTagConstraint()
    {
        $constraints = array(
            new Assert\Type('numeric')
        );

        return new VarTagConstraint($constraints);
    }
}

And, if necessary, you can set a group sequence for Symfony2 Validator.

use FiveLab\Component\VarTagValidator\Constraint\ConstraintFactoryInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FiveLab\Component\VarTagValidator\Constraint\VarTagConstraint;
use Symfony\Component\Validator\Constraints\GroupSequence;

class MoneyConstraintFactory implements ConstraintFactoryInterface
{
    /**
     * {@inheritDoc}
     */
    public function getVarTagConstraint()
    {
        $constraints = [];

        $constraints[] = new Assert\Type([
            'type' => 'numeric',
            'message' => 'This value should be of type money.',
            'groups' => 'FirstStep'
        ]);

        $constraints[] = new Assert\Regex([
            'pattern' => '/^\d+\.\d{2}$/',
            'message' => 'This value should be of type money.',
            'groups' => 'SecondStep'
        ]);

        $groupSequence = new GroupSequence(['FirstStep', 'SecondStep']);

        return new VarTagConstraint($constraints, $groupSequence);
    }
}

Step #2: Add constraint factory to registry

use FiveLab\Component\VarTagValidator\VarTagValidator;
use FiveLab\Component\VarTagValidator\Metadata\MetadataFactory;
use Symfony\Component\Validator\ValidatorBuilder;
use FiveLab\Component\VarTagValidator\Constraint\FactoryRegistry;

$registry = FactoryRegistry::createDefault();
$registry->addConstraintFactory('money', new MoneyConstraintFactory());

$validator = (new ValidatorBuilder())
    ->getValidator();

$metadataFactory = new MetadataFactory();
$varTagValidator = new VarTagValidator($validator, $metadataFactory, $registry);

Tips & Tricks

  1. You can add alias to registry for type.
    As example: int -> integer, or float -> double For more info, please see FiveLab\Component\VarTagValidator\Constraint\FactoryRegistryInterface::addConstraintFactoryAlias