Download the PHP package fivepercent/var-tag-validator without Composer

On this page you can find all versions of the php package fivepercent/var-tag-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package var-tag-validator


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

All versions of var-tag-validator with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4
fivepercent/cache Version ~1.0
fivepercent/reflection Version ~1.0
fivepercent/exception Version ~1.0
symfony/validator Version ~2.3
phpdocumentor/reflection-docblock Version ~2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package fivepercent/var-tag-validator contains the following files

Loading the files please wait ....