[Validator] Added the constraints MinCount and MaxCount
This commit is contained in:
parent
1a732e4983
commit
d84b689529
@ -170,6 +170,10 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
case 'Symfony\Component\Validator\Constraints\Max':
|
||||
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\MinCount':
|
||||
case 'Symfony\Component\Validator\Constraints\MaxCount':
|
||||
return new TypeGuess('collection', array(), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Time':
|
||||
return new TypeGuess('time', array('input'=>'string'), Guess::HIGH_CONFIDENCE);
|
||||
|
||||
|
@ -22,3 +22,4 @@ CHANGELOG
|
||||
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
|
||||
recursively anymore by default. `Valid` contains a new property `deep`
|
||||
which enables the BC behavior.
|
||||
* added MinCount and MaxCount constraint
|
||||
|
41
src/Symfony/Component/Validator/Constraints/MaxCount.php
Normal file
41
src/Symfony/Component/Validator/Constraints/MaxCount.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class MaxCount extends Constraint
|
||||
{
|
||||
public $message = 'This collection should contain {{ limit }} elements or less.';
|
||||
public $limit;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDefaultOption()
|
||||
{
|
||||
return 'limit';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRequiredOptions()
|
||||
{
|
||||
return array('limit');
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MaxCountValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the passed value is valid.
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (null === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($value) && !$value instanceof \Countable) {
|
||||
throw new UnexpectedTypeException($value, 'array or \Countable');
|
||||
}
|
||||
|
||||
$count = count($value);
|
||||
|
||||
if ($count > $constraint->limit) {
|
||||
$this->context->addViolation($constraint->message, array(
|
||||
'{{ count }}' => $count,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
), $value, (int) $constraint->limit);
|
||||
}
|
||||
}
|
||||
}
|
41
src/Symfony/Component/Validator/Constraints/MinCount.php
Normal file
41
src/Symfony/Component/Validator/Constraints/MinCount.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class MinCount extends Constraint
|
||||
{
|
||||
public $message = 'This collection should contain {{ limit }} elements or more.';
|
||||
public $limit;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDefaultOption()
|
||||
{
|
||||
return 'limit';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRequiredOptions()
|
||||
{
|
||||
return array('limit');
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MinCountValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the passed value is valid.
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*
|
||||
* @throws UnexpectedTypeException If the given value is no array or \Countable.
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (null === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($value) && !$value instanceof \Countable) {
|
||||
throw new UnexpectedTypeException($value, 'array or \Countable');
|
||||
}
|
||||
|
||||
$count = count($value);
|
||||
|
||||
if ($count < $constraint->limit) {
|
||||
$this->context->addViolation($constraint->message, array(
|
||||
'{{ count }}' => $count,
|
||||
'{{ limit }}' => $constraint->limit,
|
||||
), $value, (int) $constraint->limit);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MaxCountValidatorArrayTest extends MaxCountValidatorTest
|
||||
{
|
||||
protected function createCollection(array $content)
|
||||
{
|
||||
return $content;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
class MaxCountValidatorCountableTest_Countable implements \Countable
|
||||
{
|
||||
private $content;
|
||||
|
||||
public function __construct(array $content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($this->content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MaxCountValidatorCountableTest extends MaxCountValidatorTest
|
||||
{
|
||||
protected function createCollection(array $content)
|
||||
{
|
||||
return new MaxCountValidatorCountableTest_Countable($content);
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\MaxCount;
|
||||
use Symfony\Component\Validator\Constraints\MaxCountValidator;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
abstract class MaxCountValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $context;
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
||||
$this->validator = new MaxCountValidator();
|
||||
$this->validator->initialize($this->context);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->context = null;
|
||||
$this->validator = null;
|
||||
}
|
||||
|
||||
abstract protected function createCollection(array $content);
|
||||
|
||||
public function testNullIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate(null, new MaxCount(6));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testExpectsCountableType()
|
||||
{
|
||||
$this->validator->validate(new \stdClass(), new MaxCount(5));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidValues
|
||||
*/
|
||||
public function testValidValues($value)
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new MaxCount(3);
|
||||
$this->validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
public function getValidValues()
|
||||
{
|
||||
return array(
|
||||
array($this->createCollection(array(1))),
|
||||
array($this->createCollection(array(1, 2))),
|
||||
array($this->createCollection(array(1, 2, 3))),
|
||||
array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidValues
|
||||
*/
|
||||
public function testInvalidValues($value)
|
||||
{
|
||||
$constraint = new MaxCount(array(
|
||||
'limit' => 3,
|
||||
'message' => 'myMessage'
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation')
|
||||
->with('myMessage', $this->identicalTo(array(
|
||||
'{{ count }}' => count($value),
|
||||
'{{ limit }}' => 3,
|
||||
)), $value, 3);
|
||||
|
||||
$this->validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
public function getInvalidValues()
|
||||
{
|
||||
return array(
|
||||
array($this->createCollection(array(1, 2, 3, 4))),
|
||||
array($this->createCollection(array(1, 2, 3, 4, 5))),
|
||||
array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
|
||||
);
|
||||
}
|
||||
|
||||
public function testDefaultOption()
|
||||
{
|
||||
$constraint = new MaxCount(5);
|
||||
|
||||
$this->assertEquals(5, $constraint->limit);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MinCountValidatorArrayTest extends MinCountValidatorTest
|
||||
{
|
||||
protected function createCollection(array $content)
|
||||
{
|
||||
return $content;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
class MinCountValidatorCountableTest_Countable implements \Countable
|
||||
{
|
||||
private $content;
|
||||
|
||||
public function __construct(array $content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($this->content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class MinCountValidatorCountableTest extends MinCountValidatorTest
|
||||
{
|
||||
protected function createCollection(array $content)
|
||||
{
|
||||
return new MinCountValidatorCountableTest_Countable($content);
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\MinCount;
|
||||
use Symfony\Component\Validator\Constraints\MinCountValidator;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
abstract class MinCountValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $context;
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
||||
$this->validator = new MinCountValidator();
|
||||
$this->validator->initialize($this->context);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->context = null;
|
||||
$this->validator = null;
|
||||
}
|
||||
|
||||
abstract protected function createCollection(array $content);
|
||||
|
||||
public function testNullIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate(null, new MinCount(6));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testExpectsCountableType()
|
||||
{
|
||||
$this->validator->validate(new \stdClass(), new MinCount(5));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidValues
|
||||
*/
|
||||
public function testValidValues($value)
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new MinCount(3);
|
||||
$this->validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
public function getValidValues()
|
||||
{
|
||||
return array(
|
||||
array($this->createCollection(array(1, 2, 3))),
|
||||
array($this->createCollection(array(1, 2, 3, 4))),
|
||||
array($this->createCollection(array(1, 2, 3, 4, 5))),
|
||||
array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidValues
|
||||
*/
|
||||
public function testInvalidValues($value)
|
||||
{
|
||||
$constraint = new MinCount(array(
|
||||
'limit' => 4,
|
||||
'message' => 'myMessage'
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation')
|
||||
->with('myMessage', $this->identicalTo(array(
|
||||
'{{ count }}' => count($value),
|
||||
'{{ limit }}' => 4,
|
||||
)), $value, 4);
|
||||
|
||||
$this->validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
public function getInvalidValues()
|
||||
{
|
||||
return array(
|
||||
array($this->createCollection(array(1))),
|
||||
array($this->createCollection(array(1, 2))),
|
||||
array($this->createCollection(array(1, 2, 3))),
|
||||
array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))),
|
||||
);
|
||||
}
|
||||
|
||||
public function testDefaultOption()
|
||||
{
|
||||
$constraint = new MinCount(5);
|
||||
|
||||
$this->assertEquals(5, $constraint->limit);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user