Create "Mimetype" rule

This commit is contained in:
Henrique Moody 2015-06-16 01:16:29 -03:00
parent 96f4fc2e6f
commit fb742375b4
6 changed files with 196 additions and 0 deletions

20
docs/Mimetype.md Normal file
View file

@ -0,0 +1,20 @@
# Mimetype
- `v::mimetype(string $mimetype)`
Validates if the file mimetype matches the expected one:
```php
v::mimetype('image/png')->validate('image.png'); //true
```
This rule is case-sensitive and requires [fileinfo](http://php.net/fileinfo) PHP extension.
See also
* [Executable](Executable.md)
* [File](File.md)
* [Readable](Readable.md)
* [SymbolicLink](SymbolicLink.md)
* [Uploaded](Uploaded.md)
* [Writable](Writable.md)

View file

@ -128,6 +128,7 @@
* [Exists](Exists.md)
* [Extension](Extension.md)
* [File](File.md)
* [Mimetype](Mimetype.md)
* [Readable](Readable.md)
* [Size](Size.md)
* [SymbolicLink](SymbolicLink.md)
@ -218,6 +219,7 @@
* [Lowercase](Lowercase.md)
* [MacAddress](MacAddress.md)
* [Max](Max.md)
* [Mimetype](Mimetype.md)
* [Min](Min.md)
* [MinimumAge](MinimumAge.md)
* [Multiple](Multiple.md)

View file

@ -0,0 +1,32 @@
<?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;
/**
* Exception class for Mimetype rule.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class MimetypeException extends ValidationException
{
/**
* @var array
*/
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must have "{{mimetype}}" mimetype',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not have "{{mimetype}}" mimetype',
),
);
}

View file

@ -0,0 +1,59 @@
<?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;
/**
* Validate file mimetypes.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Mimetype extends AbstractRule
{
/**
* @var string
*/
public $mimetype;
/**
* @var finfo
*/
private $fileInfo;
/**
* @param string $mimetype
* @param finfo $fileInfo
*/
public function __construct($mimetype, finfo $fileInfo = null)
{
$this->mimetype = $mimetype;
$this->fileInfo = $fileInfo ?: new finfo(FILEINFO_MIME_TYPE);
}
/**
* {@inheritdoc}
*/
public function validate($input)
{
if ($input instanceof SplFileInfo) {
$input = $input->getPathname();
}
if (!is_string($input)) {
return false;
}
return ($this->fileInfo->file($input) == $this->mimetype);
}
}

View file

@ -71,6 +71,7 @@ use Respect\Validation\Rules\AllOf;
* @method static Validator lowercase()
* @method static Validator macAddress()
* @method static Validator max(mixed $maxValue, bool $inclusive = false)
* @method static Validator mimetype(string $mimetype)
* @method static Validator min(mixed $minValue, bool $inclusive = false)
* @method static Validator minimumAge(int $age)
* @method static Validator multiple(int $multipleOf)

View file

@ -0,0 +1,82 @@
<?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 PHPUnit_Framework_TestCase;
use SplFileInfo;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class MimetypeTest extends PHPUnit_Framework_TestCase
{
public function testShouldValidateMimetype()
{
$filename = 'filename.txt';
$mimetype = 'plain/text';
$fileInfoMock = $this
->getMockBuilder('finfo')
->disableOriginalConstructor()
->setMethods(array('file'))
->getMock();
$fileInfoMock
->expects($this->once())
->method('file')
->with($filename)
->will($this->returnValue($mimetype));
$rule = new Mimetype($mimetype, $fileInfoMock);
$this->assertTrue($rule->validate($filename));
}
public function testShouldValidateSplFileInfoMimetype()
{
$fileInfo = new SplFileInfo('filename.png');
$mimetype = 'image/png';
$fileInfoMock = $this
->getMockBuilder('finfo')
->disableOriginalConstructor()
->setMethods(array('file'))
->getMock();
$fileInfoMock
->expects($this->once())
->method('file')
->with($fileInfo->getPathname())
->will($this->returnValue($mimetype));
$rule = new Mimetype($mimetype, $fileInfoMock);
$this->assertTrue($rule->validate($fileInfo));
}
public function testShouldInvalidateWhenNotStringNorSplFileInfo()
{
$rule = new Mimetype('application/octet-stream');
$this->assertFalse($rule->validate(array(__FILE__)));
}
/**
* @expectedException Respect\Validation\Exceptions\MimetypeException
* @expectedExceptionMessage MimetypeTest.php" must have "application/json" mimetype
*/
public function testShouldThowsMimetypeExceptionWhenCheckingValue()
{
$rule = new Mimetype('application/json');
$rule->check(__FILE__);
}
}