Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] added a SizeLength validator
- Loading branch information
Showing
4 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Validator/Constraints/SizeLength.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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 SizeLength extends Constraint | ||
{ | ||
public $minMessage = 'This value is too short. It should have {{ limit }} characters or more'; | ||
public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less'; | ||
public $exactMessage = 'This value should have exactly {{ limit }} characters'; | ||
public $min; | ||
public $max; | ||
public $charset = 'UTF-8'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRequiredOptions() | ||
{ | ||
return array('min', 'max'); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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; | ||
|
||
/** | ||
* @api | ||
*/ | ||
class SizeLengthValidator 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 | ||
* | ||
* @return Boolean Whether or not the value is valid | ||
* | ||
* @api | ||
*/ | ||
public function isValid($value, Constraint $constraint) | ||
{ | ||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { | ||
throw new UnexpectedTypeException($value, 'string'); | ||
} | ||
|
||
$value = (string) $value; | ||
|
||
$length = function_exists('mb_strlen') ? mb_strlen($value, $constraint->charset) : strlen($value); | ||
|
||
if ($constraint->min == $constraint->max && $length != $constraint->max) { | ||
$this->setMessage($constraint->exactMessage, array( | ||
'{{ value }}' => $value, | ||
'{{ limit }}' => $constraint->max, | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
if ($length > $constraint->max) { | ||
$this->setMessage($constraint->maxMessage, array( | ||
'{{ value }}' => $value, | ||
'{{ limit }}' => $constraint->max, | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
if ($length < $constraint->min) { | ||
$this->setMessage($constraint->minMessage, array( | ||
'{{ value }}' => $value, | ||
'{{ limit }}' => $constraint->min, | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
tests/Symfony/Tests/Component/Validator/Constraints/SizeLengthValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?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\Tests\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraints\SizeLength; | ||
use Symfony\Component\Validator\Constraints\SizeLengthValidator; | ||
|
||
class SizeLengthValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $validator; | ||
|
||
protected function setUp() | ||
{ | ||
$this->validator = new SizeLengthValidator(); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->validator = null; | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$this->assertTrue($this->validator->isValid(null, new SizeLength(array('min' => 6, 'max' => 10)))); | ||
} | ||
|
||
public function testEmptyStringIsValid() | ||
{ | ||
$this->assertTrue($this->validator->isValid('', new SizeLength(array('min' => 6, 'max' => 10)))); | ||
} | ||
|
||
public function testExpectsStringCompatibleType() | ||
{ | ||
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); | ||
|
||
$this->validator->isValid(new \stdClass(), new SizeLength(array('min' => 6, 'max' => 10))); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidValues | ||
*/ | ||
public function testValidValues($value, $skip = false) | ||
{ | ||
if (!$skip) { | ||
$constraint = new SizeLength(array('min' => 6, 'max' => 10)); | ||
$this->assertTrue($this->validator->isValid($value, $constraint)); | ||
} | ||
} | ||
|
||
public function getValidValues() | ||
{ | ||
return array( | ||
array(123456), | ||
array(1234567890), | ||
array('123456'), | ||
array('1234567890'), | ||
array('üüüüüü', !function_exists('mb_strlen')), | ||
array('üüüüüüüüüü', !function_exists('mb_strlen')), | ||
array('éééééé', !function_exists('mb_strlen')), | ||
array('éééééééééé', !function_exists('mb_strlen')), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidValues | ||
*/ | ||
public function testInvalidValues($value, $skip = false) | ||
{ | ||
if (!$skip) { | ||
$constraint = new SizeLength(array('min' => 6, 'max' => 10)); | ||
$this->assertFalse($this->validator->isValid($value, $constraint)); | ||
} | ||
} | ||
|
||
public function getInvalidValues() | ||
{ | ||
return array( | ||
array(12345), | ||
array(12345678901), | ||
array('12345'), | ||
array('12345678901'), | ||
array('üüüüü', !function_exists('mb_strlen')), | ||
array('üüüüüüüüüüü', !function_exists('mb_strlen')), | ||
array('ééééé', !function_exists('mb_strlen')), | ||
array('ééééééééééé', !function_exists('mb_strlen')), | ||
); | ||
} | ||
|
||
public function testMessageIsSet() | ||
{ | ||
$constraint = new SizeLength(array( | ||
'min' => 5, | ||
'max' => 10, | ||
'minMessage' => 'myMinMessage', | ||
'maxMessage' => 'myMaxMessage', | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid('1234', $constraint)); | ||
$this->assertEquals('myMinMessage', $this->validator->getMessageTemplate()); | ||
$this->assertEquals(array( | ||
'{{ value }}' => '1234', | ||
'{{ limit }}' => 5, | ||
), $this->validator->getMessageParameters()); | ||
|
||
$this->assertFalse($this->validator->isValid('12345678901', $constraint)); | ||
$this->assertEquals('myMaxMessage', $this->validator->getMessageTemplate()); | ||
$this->assertEquals(array( | ||
'{{ value }}' => '12345678901', | ||
'{{ limit }}' => 10, | ||
), $this->validator->getMessageParameters()); | ||
} | ||
|
||
public function testExactErrorMessage() | ||
{ | ||
$constraint = new SizeLength(array( | ||
'min' => 5, | ||
'max' => 5, | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid('1234', $constraint)); | ||
$this->assertEquals('This value should have exactly {{ limit }} characters', $this->validator->getMessageTemplate()); | ||
$this->assertEquals(array( | ||
'{{ value }}' => '1234', | ||
'{{ limit }}' => 5, | ||
), $this->validator->getMessageParameters()); | ||
} | ||
} |
b9ba117
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reminds me of another useful validator that I previously had to create myself. What are you thoughts on enhancing this to also support arrays and Countable objects? Then it could also be used on collections?
Previously, the developer's two options for such validation was to either roll their own validator or use the Min/Max numeric validators on a method returning the count (possible for object validation, not so much for arrays).