bug #11928 [Validator] The ratio of the ImageValidator is rounded to two decimals now (webmozart)
This PR was merged into the 2.4 branch.
Discussion
----------
[Validator] The ratio of the ImageValidator is rounded to two decimals now
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Before, using a max ratio of 1.33 for 4-by-3 images did not work, since the actual ratio is 1.3333... The actual ratio is now rounded to two decimals before comparing.
Commits
-------
709db6f
[Validator] The ratio of the ImageValidator is rounded to two decimals now
This commit is contained in:
commit
4a0adbaf74
@ -62,7 +62,7 @@ class ImageValidator extends FileValidator
|
||||
if ($width < $constraint->minWidth) {
|
||||
$this->context->addViolation($constraint->minWidthMessage, array(
|
||||
'{{ width }}' => $width,
|
||||
'{{ min_width }}' => $constraint->minWidth
|
||||
'{{ min_width }}' => $constraint->minWidth,
|
||||
));
|
||||
|
||||
return;
|
||||
@ -77,7 +77,7 @@ class ImageValidator extends FileValidator
|
||||
if ($width > $constraint->maxWidth) {
|
||||
$this->context->addViolation($constraint->maxWidthMessage, array(
|
||||
'{{ width }}' => $width,
|
||||
'{{ max_width }}' => $constraint->maxWidth
|
||||
'{{ max_width }}' => $constraint->maxWidth,
|
||||
));
|
||||
|
||||
return;
|
||||
@ -92,7 +92,7 @@ class ImageValidator extends FileValidator
|
||||
if ($height < $constraint->minHeight) {
|
||||
$this->context->addViolation($constraint->minHeightMessage, array(
|
||||
'{{ height }}' => $height,
|
||||
'{{ min_height }}' => $constraint->minHeight
|
||||
'{{ min_height }}' => $constraint->minHeight,
|
||||
));
|
||||
|
||||
return;
|
||||
@ -107,12 +107,12 @@ class ImageValidator extends FileValidator
|
||||
if ($height > $constraint->maxHeight) {
|
||||
$this->context->addViolation($constraint->maxHeightMessage, array(
|
||||
'{{ height }}' => $height,
|
||||
'{{ max_height }}' => $constraint->maxHeight
|
||||
'{{ max_height }}' => $constraint->maxHeight,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$ratio = $width / $height;
|
||||
$ratio = round($width / $height, 2);
|
||||
|
||||
if (null !== $constraint->minRatio) {
|
||||
if (!is_numeric((string) $constraint->minRatio)) {
|
||||
@ -122,7 +122,7 @@ class ImageValidator extends FileValidator
|
||||
if ($ratio < $constraint->minRatio) {
|
||||
$this->context->addViolation($constraint->minRatioMessage, array(
|
||||
'{{ ratio }}' => $ratio,
|
||||
'{{ min_ratio }}' => $constraint->minRatio
|
||||
'{{ min_ratio }}' => $constraint->minRatio,
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -135,7 +135,7 @@ class ImageValidator extends FileValidator
|
||||
if ($ratio > $constraint->maxRatio) {
|
||||
$this->context->addViolation($constraint->maxRatioMessage, array(
|
||||
'{{ ratio }}' => $ratio,
|
||||
'{{ max_ratio }}' => $constraint->maxRatio
|
||||
'{{ max_ratio }}' => $constraint->maxRatio,
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -143,21 +143,21 @@ class ImageValidator extends FileValidator
|
||||
if (!$constraint->allowSquare && $width == $height) {
|
||||
$this->context->addViolation($constraint->allowSquareMessage, array(
|
||||
'{{ width }}' => $width,
|
||||
'{{ height }}' => $height
|
||||
'{{ height }}' => $height,
|
||||
));
|
||||
}
|
||||
|
||||
if (!$constraint->allowLandscape && $width > $height) {
|
||||
$this->context->addViolation($constraint->allowLandscapeMessage, array(
|
||||
'{{ width }}' => $width,
|
||||
'{{ height }}' => $height
|
||||
'{{ height }}' => $height,
|
||||
));
|
||||
}
|
||||
|
||||
if (!$constraint->allowPortrait && $width < $height) {
|
||||
$this->context->addViolation($constraint->allowPortraitMessage, array(
|
||||
'{{ width }}' => $width,
|
||||
'{{ height }}' => $height
|
||||
'{{ height }}' => $height,
|
||||
));
|
||||
}
|
||||
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 57 B |
@ -13,16 +13,21 @@ namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Image;
|
||||
use Symfony\Component\Validator\Constraints\ImageValidator;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
|
||||
class ImageValidatorTest extends AbstractConstraintValidatorTest
|
||||
{
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* @var ImageValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
protected $path;
|
||||
protected $image;
|
||||
protected $imageLandscape;
|
||||
protected $imagePortrait;
|
||||
protected $image4By3;
|
||||
|
||||
protected function createValidator()
|
||||
{
|
||||
@ -36,6 +41,7 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest
|
||||
$this->image = __DIR__.'/Fixtures/test.gif';
|
||||
$this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
|
||||
$this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
|
||||
$this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
|
||||
}
|
||||
|
||||
public function testNullIsValid()
|
||||
@ -211,6 +217,17 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest
|
||||
));
|
||||
}
|
||||
|
||||
public function testMaxRatioUsesTwoDecimalsOnly()
|
||||
{
|
||||
$constraint = new Image(array(
|
||||
'maxRatio' => 1.33,
|
||||
));
|
||||
|
||||
$this->validator->validate($this->image4By3, $constraint);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
*/
|
||||
|
Reference in New Issue
Block a user