Skip to content

Instantly share code, notes, and snippets.

@webmozart
Created March 17, 2014 19:37
Show Gist options
  • Save webmozart/9606689 to your computer and use it in GitHub Desktop.
Save webmozart/9606689 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\Valid;
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 BenchArticle
{
/**
* @NotNull
* @Length(min=3)
*/
public $title;
/**
* @NotNull
* @Range(min=2, max=50)
*/
public $number;
/**
* @NotNull
*/
public $text;
/**
* @Valid
*/
public $tags;
}
class BenchTag
{
/**
* @NotNull
* @Length(max=10)
*/
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
$article = new BenchArticle();
$article->title = 'fo';
$article->text = 'Lorem ipsum dolor';
$article->number = 60;
$article->tags = array();
$article->tags[] = new BenchTag('symfony');
$article->tags[] = new BenchTag('validator');
$article->tags[] = new BenchTag('benchmark');
// Validate once to autoload all needed classes
$validator->validate($article);
$time = microtime(true);
for ($i = 0; $i < 20; ++$i) {
$validator->validate($article);
}
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