fivelab / var-tag-validator
Validate objects by var tag
Installs: 1 112
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=5.4
- fivelab/cache: ~1.0
- fivelab/exception: ~1.0
- fivelab/reflection: ~1.0
- phpdocumentor/reflection-docblock: ~2.0
- symfony/validator: ~2.7|~3.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2017-08-16 12:52:44 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:
- integer, int
- float, double
- string
- 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
- 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