Skip to content

Instantly share code, notes, and snippets.

@webmozart
Created March 11, 2014 14:02
Show Gist options
  • Save webmozart/9486236 to your computer and use it in GitHub Desktop.
Save webmozart/9486236 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
$validator = Validation::createValidatorBuilder()
->setApiVersion(Validation::API_VERSION_2_4)
// ->setApiVersion(Validation::API_VERSION_2_5)
// ->setApiVersion(Validation::API_VERSION_2_4 | Validation::API_VERSION_2_5)
->enableAnnotationMapping()
->getValidator();
class BenchClass
{
/**
* @NotNull
* @Range(min=2, max=50)
*/
public $foo;
/**
* @NotNull
* @Length(min=3)
*/
public $bar;
}
$object = new BenchClass();
$object->foo = 60;
$object->bar = 'fo';
$constraint = new Range(array('min' => 50));
// Validate once to autoload all needed classes
$validator->validate($object);
$time = microtime(true);
for ($i = 0; $i < 20; ++$i) {
$validator->validate($object);
}
echo 1000*(microtime(true)-$time)."ms\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment