Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ImageValidator] Added dedicated ImageValidator class with min width,…
… max width, min height and max height validations
- Loading branch information
1 parent
dccd2d5
commit a5a2dfa
Showing
3 changed files
with
284 additions
and
8 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
108 changes: 108 additions & 0 deletions
108
src/Symfony/Component/Validator/Constraints/ImageValidator.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,108 @@ | ||
<?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\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* Validates whether a value is a valid image file and is valid | ||
* against minWidth, maxWidth, minHeight and maxHeight constraints | ||
* | ||
* @author Benjamin Dulau <benjamin.dulau@gmail.com> | ||
*/ | ||
class ImageValidator extends FileValidator | ||
{ | ||
public function isValid($value, Constraint $constraint) | ||
{ | ||
$isValid = parent::isValid($value, $constraint); | ||
if (!$isValid) { | ||
return false; | ||
} | ||
|
||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
$size = @getimagesize($value); | ||
if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) { | ||
$this->setMessage($constraint->notDetectedMessage); | ||
|
||
return false; | ||
} | ||
|
||
$width = $size[0]; | ||
$height = $size[1]; | ||
|
||
if ($constraint->minWidth) { | ||
if (!ctype_digit((string)$constraint->minWidth)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth)); | ||
} | ||
|
||
if ($width < $constraint->minWidth) { | ||
$this->setMessage($constraint->minWidthMessage, array( | ||
'{{ width }}' => $width, | ||
'{{ minWidth }}' => $constraint->minWidth | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if ($constraint->maxWidth) { | ||
if (!ctype_digit((string)$constraint->maxWidth)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth)); | ||
} | ||
|
||
if ($width > $constraint->maxWidth) { | ||
$this->setMessage($constraint->maxWidthMessage, array( | ||
'{{ width }}' => $width, | ||
'{{ maxWidth }}' => $constraint->maxWidth | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if ($constraint->minHeight) { | ||
if (!ctype_digit((string)$constraint->minHeight)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight)); | ||
} | ||
|
||
if ($height < $constraint->minHeight) { | ||
$this->setMessage($constraint->minHeightMessage, array( | ||
'{{ height }}' => $height, | ||
'{{ minHeight }}' => $constraint->minHeight | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if ($constraint->maxHeight) { | ||
if (!ctype_digit((string)$constraint->maxHeight)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight)); | ||
} | ||
|
||
if ($height > $constraint->maxHeight) { | ||
$this->setMessage($constraint->maxHeightMessage, array( | ||
'{{ height }}' => $height, | ||
'{{ maxHeight }}' => $constraint->maxHeight | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
tests/Symfony/Tests/Component/Validator/Constraints/ImageValidatorTest.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,166 @@ | ||
<?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\Image; | ||
use Symfony\Component\Validator\Constraints\ImageValidator; | ||
|
||
class ImageValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $validator; | ||
protected $path; | ||
protected $image; | ||
|
||
protected function setUp() | ||
{ | ||
$this->validator = new ImageValidator(); | ||
$this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'ImageValidatorTest'; | ||
$this->image = imagecreatetruecolor(2, 2); | ||
imagejpeg($this->image, $this->path); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
imagedestroy($this->image); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$this->assertTrue($this->validator->isValid(null, new Image())); | ||
} | ||
|
||
public function testEmptyStringIsValid() | ||
{ | ||
$this->assertTrue($this->validator->isValid('', new Image())); | ||
} | ||
|
||
public function testValidImage() | ||
{ | ||
$this->assertTrue($this->validator->isValid($this->path, new Image())); | ||
} | ||
|
||
public function testValidSize() | ||
{ | ||
$constraint = new Image(array( | ||
'minWidth' => 1, | ||
'maxWidth' => 2, | ||
'minHeight' => 1, | ||
'maxHeight' => 2, | ||
)); | ||
|
||
$this->assertTrue($this->validator->isValid($this->path, $constraint)); | ||
} | ||
|
||
public function testWidthTooSmall() | ||
{ | ||
$constraint = new Image(array( | ||
'minWidth' => 3, | ||
'minWidthMessage' => 'myMessage', | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid($this->path, $constraint)); | ||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage'); | ||
$this->assertEquals($this->validator->getMessageParameters(), array( | ||
'{{ width }}' => '2', | ||
'{{ minWidth }}' => '3', | ||
)); | ||
} | ||
|
||
public function testWidthTooBig() | ||
{ | ||
$constraint = new Image(array( | ||
'maxWidth' => 1, | ||
'maxWidthMessage' => 'myMessage', | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid($this->path, $constraint)); | ||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage'); | ||
$this->assertEquals($this->validator->getMessageParameters(), array( | ||
'{{ width }}' => '2', | ||
'{{ maxWidth }}' => '1', | ||
)); | ||
} | ||
|
||
public function testHeightTooSmall() | ||
{ | ||
$constraint = new Image(array( | ||
'minHeight' => 3, | ||
'minHeightMessage' => 'myMessage', | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid($this->path, $constraint)); | ||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage'); | ||
$this->assertEquals($this->validator->getMessageParameters(), array( | ||
'{{ height }}' => '2', | ||
'{{ minHeight }}' => '3', | ||
)); | ||
} | ||
|
||
public function testHeightTooBig() | ||
{ | ||
$constraint = new Image(array( | ||
'maxHeight' => 1, | ||
'maxHeightMessage' => 'myMessage', | ||
)); | ||
|
||
$this->assertFalse($this->validator->isValid($this->path, $constraint)); | ||
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage'); | ||
$this->assertEquals($this->validator->getMessageParameters(), array( | ||
'{{ height }}' => '2', | ||
'{{ maxHeight }}' => '1', | ||
)); | ||
} | ||
|
||
public function testInvalidMinWidth() | ||
{ | ||
$constraint = new Image(array( | ||
'minWidth' => '1abc', | ||
)); | ||
|
||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); | ||
|
||
$this->validator->isValid($this->path, $constraint); | ||
} | ||
|
||
public function testInvalidMaxWidth() | ||
{ | ||
$constraint = new Image(array( | ||
'maxWidth' => '1abc', | ||
)); | ||
|
||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); | ||
|
||
$this->validator->isValid($this->path, $constraint); | ||
} | ||
|
||
public function testInvalidMinHeight() | ||
{ | ||
$constraint = new Image(array( | ||
'minHeight' => '1abc', | ||
)); | ||
|
||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); | ||
|
||
$this->validator->isValid($this->path, $constraint); | ||
} | ||
|
||
public function testInvalidMaxHeight() | ||
{ | ||
$constraint = new Image(array( | ||
'maxHeight' => '1abc', | ||
)); | ||
|
||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); | ||
|
||
$this->validator->isValid($this->path, $constraint); | ||
} | ||
} |