Var Tag Validator

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

Installation

Add FivePercent/VarTagValidator in your composer.json:

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

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

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

Basic usage

Basic create VarTagValidator instance:

use FivePercent\Component\VarTagValidator\VarTagValidator;
use FivePercent\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. string
  3. scalar
  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 FivePercent\Component\VarTagValidator\Constraint\ConstraintFactoryInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FivePercent\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 FivePercent\Component\VarTagValidator\Constraint\ConstraintFactoryInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FivePercent\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 FivePercent\Component\VarTagValidator\VarTagValidator;
use FivePercent\Component\VarTagValidator\Metadata\MetadataFactory;
use Symfony\Component\Validator\ValidatorBuilder;
use FivePercent\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 FivePercent\Component\VarTagValidator\Constraint\FactoryRegistryInterface::addConstraintFactoryAlias