mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Charset Validator
This commit is contained in:
parent
18a7a53362
commit
4625d1c24e
12
README.md
12
README.md
|
@ -204,6 +204,7 @@ Reference
|
|||
* v::alnum()
|
||||
* v::alpha()
|
||||
* v::between()
|
||||
* v::charset()
|
||||
* v::consonants()
|
||||
* v::contains()
|
||||
* v::digits()
|
||||
|
@ -485,6 +486,17 @@ See also:
|
|||
|
||||
* v::call() - A more elaborated building block validator
|
||||
|
||||
#### v::charset()
|
||||
|
||||
Validates if a string is in a specific charset.
|
||||
|
||||
|
||||
v::charset('ASCII', 'açúcar'); //false
|
||||
v::charset('ASCII', 'sugar'); //true
|
||||
v::charset(array('ISO-8859-1', 'EUC-JP'), '日本国'); // true
|
||||
|
||||
The array format is a logic OR, not AND.
|
||||
|
||||
#### v::countryCode
|
||||
|
||||
Validates an ISO country code like US or BR.
|
||||
|
|
18
library/Respect/Validation/Exceptions/CharsetException.php
Normal file
18
library/Respect/Validation/Exceptions/CharsetException.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class CharsetException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be in the {{charset}} charset'
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be in the {{charset}} charset'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
36
library/Respect/Validation/Rules/Charset.php
Normal file
36
library/Respect/Validation/Rules/Charset.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Charset extends AbstractRule
|
||||
{
|
||||
|
||||
public $charset = null;
|
||||
|
||||
public function __construct($charset)
|
||||
{
|
||||
$available = mb_list_encodings();
|
||||
$charset = is_array($charset) ? $charset : array($charset);
|
||||
$charset = array_filter($charset, function ($c) use ($available) {
|
||||
return in_array($c, $available, true);
|
||||
});
|
||||
|
||||
if (!$charset) {
|
||||
throw new ComponentException(
|
||||
'Invalid charset'
|
||||
);
|
||||
}
|
||||
$this->charset = $charset;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$detectedEncoding = mb_detect_encoding($input, $this->charset, true);
|
||||
|
||||
return in_array($detectedEncoding, $this->charset, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
73
tests/library/Respect/Validation/Rules/CharsetTest.php
Normal file
73
tests/library/Respect/Validation/Rules/CharsetTest.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class CharsetTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidCharset
|
||||
*/
|
||||
public function test_valid_data_with_charset_should_return_true($charset, $input)
|
||||
{
|
||||
$validator = new Charset($charset);
|
||||
$this->assertTrue($validator->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidCharset
|
||||
* @expectedException Respect\Validation\Exceptions\CharsetException
|
||||
*/
|
||||
public function test_invalid_charset_should_fail_and_throw_CharsetException($charset, $input)
|
||||
{
|
||||
$validator = new Charset($charset);
|
||||
$this->assertFalse($validator->validate($input));
|
||||
$this->assertFalse($validator->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function test_invalid_constructor_params_should_throw_ComponentException_upon_instantiation($charset)
|
||||
{
|
||||
$validator = new Charset($charset);
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return array(
|
||||
array(new \stdClass),
|
||||
array(array()),
|
||||
array(null),
|
||||
array('16'),
|
||||
array('aeiou'),
|
||||
array('a'),
|
||||
array('Foo'),
|
||||
array('basic'),
|
||||
array(10)
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForValidCharset()
|
||||
{
|
||||
return array(
|
||||
array('ISO-8859-1', mb_convert_encoding('açaí', 'ISO-8859-1')),
|
||||
array(array('UTF-8', 'ASCII'), 'strawberry'),
|
||||
array('ASCII', mb_convert_encoding('strawberry', 'ASCII')),
|
||||
array('UTF-8', '日本国'),
|
||||
array(array('ISO-8859-1', 'EUC-JP'), '日本国'),
|
||||
array('UTF-8', 'açaí'),
|
||||
array('ISO-8859-1', 'açaí'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidCharset()
|
||||
{
|
||||
return array(
|
||||
array('ASCII', '日本国'),
|
||||
array('ASCII', 'açaí')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue