mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Create "Image" rule
This commit is contained in:
parent
799228a6e0
commit
c828420438
32
docs/Image.md
Normal file
32
docs/Image.md
Normal 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)
|
|
@ -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)
|
||||
|
|
24
library/Exceptions/ImageException.php
Normal file
24
library/Exceptions/ImageException.php
Normal 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
42
library/Rules/Image.php
Executable 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/'));
|
||||
}
|
||||
}
|
|
@ -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
BIN
tests/fixtures/invalid-image.png
vendored
Normal file
Binary file not shown.
BIN
tests/fixtures/valid-image.gif
vendored
Normal file
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
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
BIN
tests/fixtures/valid-image.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 65 KiB |
10
tests/integration/rules/image_1.phpt
Normal file
10
tests/integration/rules/image_1.phpt
Normal 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--
|
16
tests/integration/rules/image_2.phpt
Normal file
16
tests/integration/rules/image_2.phpt
Normal 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
|
16
tests/integration/rules/image_3.phpt
Normal file
16
tests/integration/rules/image_3.phpt
Normal 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
|
16
tests/integration/rules/image_4.phpt
Normal file
16
tests/integration/rules/image_4.phpt
Normal 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
|
16
tests/integration/rules/image_5.phpt
Normal file
16
tests/integration/rules/image_5.phpt
Normal 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
66
tests/unit/Rules/ImageTest.php
Executable 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],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue