Create "Image" rule

This commit is contained in:
Guilherme Siani 2015-11-05 23:21:42 -02:00 committed by Henrique Moody
parent 799228a6e0
commit c828420438
15 changed files with 242 additions and 0 deletions

32
docs/Image.md Normal file
View file

@ -0,0 +1,32 @@
# Image
- `v::image()`
- `v::image(finfo $fileInfo)`
Validates if the file is a valid image by checking its MIME type.
```php
v::image()->validate('image.gif'); // true
v::image()->validate('image.jpg'); // true
v::image()->validate('image.png'); // true
```
All the validations above must return `false` if the input is not a valid file
or of the MIME doesn't match with the file extension.
This rule relies on [fileinfo](http://php.net/fileinfo) PHP extension.
***
See also:
* [Directory](Directory.md)
* [Executable](Executable.md)
* [Exists](Exists.md)
* [Extension](Extension.md)
* [File](File.md)
* [Mimetype](Mimetype.md)
* [Readable](Readable.md)
* [Size](Size.md)
* [SymbolicLink](SymbolicLink.md)
* [Uploaded](Uploaded.md)
* [Writable](Writable.md)

View file

@ -151,6 +151,7 @@
* [Exists](Exists.md)
* [Extension](Extension.md)
* [File](File.md)
* [Image](Image.md)
* [Mimetype](Mimetype.md)
* [Readable](Readable.md)
* [Size](Size.md)
@ -242,6 +243,7 @@
* [FloatType](FloatType.md)
* [Graph](Graph.md)
* [HexRgbColor](HexRgbColor.md)
* [Image](Image.md)
* [Imei](Imei.md)
* [In](In.md)
* [Infinite](Infinite.md)

View file

@ -0,0 +1,24 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Exceptions;
class ImageException extends ValidationException
{
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid image',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a valid image',
],
];
}

42
library/Rules/Image.php Executable file
View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
use finfo;
use SplFileInfo;
class Image extends AbstractRule
{
public $fileInfo;
public function __construct(finfo $fileInfo = null)
{
$this->fileInfo = $fileInfo ?: new finfo(FILEINFO_MIME_TYPE);
}
public function validate($input)
{
if ($input instanceof SplFileInfo) {
$input = $input->getPathname();
}
if (!is_string($input)) {
return false;
}
if (!is_file($input)) {
return false;
}
return (0 === strpos($this->fileInfo->file($input), 'image/'));
}
}

View file

@ -11,6 +11,7 @@
namespace Respect\Validation;
use finfo;
use ReflectionClass;
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\ComponentException;
@ -69,6 +70,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator hexRgbColor()
* @method static Validator image(finfo $fileInfo = null)
* @method static Validator imei()
* @method static Validator in(mixed $haystack, bool $compareIdentical = false)
* @method static Validator infinite()

BIN
tests/fixtures/invalid-image.png vendored Normal file

Binary file not shown.

BIN
tests/fixtures/valid-image.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 KiB

BIN
tests/fixtures/valid-image.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
tests/fixtures/valid-image.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View file

@ -0,0 +1,10 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::image()->assert('tests/fixtures/valid-image.png');
v::image()->check(new SplFileInfo('tests/fixtures/valid-image.gif'));
?>
--EXPECTF--

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ImageException;
use Respect\Validation\Validator as v;
try {
v::image()->check('tests/fixtures/invalid-image.png');
} catch (ImageException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
"tests/fixtures/invalid-image.png" must be a valid image

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::image()->assert(new stdClass());
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- `[object] (stdClass: { })` must be a valid image

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ImageException;
use Respect\Validation\Validator as v;
try {
v::not(v::image())->check('tests/fixtures/valid-image.png');
} catch (ImageException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
"tests/fixtures/valid-image.png" must not be a valid image

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::not(v::image())->assert('tests/fixtures/valid-image.gif');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- "tests/fixtures/valid-image.gif" must not be a valid image

66
tests/unit/Rules/ImageTest.php Executable file
View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
use finfo;
use SplFileInfo;
use SplFileObject;
/**
* @group rule
* @covers Respect\Validation\Rules\Image
*/
class ImageTest extends RuleTestCase
{
public function testShouldAcceptAnInstanceOfFinfoOnConstructor()
{
$finfo = new finfo(FILEINFO_MIME_TYPE);
$rule = new Image($finfo);
$this->assertSame($rule->fileInfo, $finfo);
}
public function testShouldHaveAnInstanceOfFinfoByDefault()
{
$rule = new Image();
$this->assertInstanceOf('finfo', $rule->fileInfo);
}
public function providerForValidInput()
{
$rule = new Image();
$fixturesDirectory = realpath(__DIR__.'/../../fixtures/');
return [
[$rule, $fixturesDirectory.'/valid-image.gif'],
[$rule, $fixturesDirectory.'/valid-image.jpg'],
[$rule, $fixturesDirectory.'/valid-image.png'],
[$rule, new SplFileInfo($fixturesDirectory.'/valid-image.gif')],
[$rule, new SplFileInfo($fixturesDirectory.'/valid-image.jpg')],
[$rule, new SplFileObject($fixturesDirectory.'/valid-image.png')],
];
}
public function providerForInvalidInput()
{
$rule = new Image();
$fixturesDirectory = realpath(__DIR__.'/../../fixtures/');
return [
[$rule, $fixturesDirectory.'/invalid-image.png'],
[$rule, 'asdf'],
[$rule, 1],
[$rule, true],
];
}
}