Apply contribution guidelines to "Charset" rule

Also change the constructor of the rule to accept charsets as arguments
instead of being either an array or a string.

Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
William Espindola 2018-06-10 13:34:14 -03:00 committed by Henrique Moody
parent 9b4c4ddb4b
commit 8272f43207
No known key found for this signature in database
GPG key ID: 221E9281655813A6
6 changed files with 114 additions and 69 deletions

View file

@ -1,13 +1,13 @@
# Charset
- `Charset(mixed $charset)`
- `Charset(string ...$charset)`
Validates if a string is in a specific charset.
```php
v::charset('ASCII')->validate('açúcar'); // false
v::charset('ASCII')->validate('sugar'); //true
v::charset(['ISO-8859-1', 'EUC-JP'])->validate('日本国'); // true
v::charset('ISO-8859-1', 'EUC-JP')->validate('日本国'); // true
```
The array format is a logic OR, not AND.
@ -16,6 +16,7 @@ The array format is a logic OR, not AND.
Version | Description
--------|-------------
2.0.0 | Charset supports multiple charsets on its constructor
0.5.0 | Created
***

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class CharsetException extends ValidationException
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class CharsetException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be in the {{charset}} charset',

View file

@ -14,27 +14,45 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use function array_diff;
use function in_array;
use function mb_detect_encoding;
use function mb_list_encodings;
class Charset extends AbstractRule
/**
* Validates if a string is in a specific charset.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Charset extends AbstractRule
{
public $charset = null;
/**
* @var string[]
*/
private $charset;
public function __construct($charset)
/**
* Initializes the rule.
*
* @param string ...$charset
*
* @throws ComponentException
*/
public function __construct(string ...$charset)
{
$available = mb_list_encodings();
$charset = is_array($charset) ? $charset : [$charset];
$charset = array_filter($charset, function ($c) use ($available) {
return in_array($c, $available, true);
});
if (!$charset) {
throw new ComponentException(
'Invalid charset'
);
if (!empty(array_diff($charset, $available))) {
throw new ComponentException('Invalid charset');
}
$this->charset = $charset;
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
$detectedEncoding = mb_detect_encoding($input, $this->charset, true);

View file

@ -40,7 +40,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator call()
* @method static Validator callableType()
* @method static Validator callback(callable $callback)
* @method static Validator charset($charset)
* @method static Validator charset(string ...$charset)
* @method static Validator cnh()
* @method static Validator cnpj()
* @method static Validator consonant(string $additionalChars = null)

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CharsetException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::charset('ASCII')->check('açaí');
} catch (CharsetException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::charset('UTF-8'))->check('açaí');
} catch (CharsetException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::charset('ASCII')->assert('açaí');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::charset('UTF-8'))->assert('açaí');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"açaí" must be in the `{ "ASCII" }` charset
"açaí" must not be in the `{ "UTF-8" }` charset
- "açaí" must be in the `{ "ASCII" }` charset
- "açaí" must not be in the `{ "UTF-8" }` charset

View file

@ -13,78 +13,59 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Test\RuleTestCase;
use function mb_convert_encoding;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Charset
* @covers \Respect\Validation\Exceptions\CharsetException
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
class CharsetTest extends TestCase
final class CharsetTest extends RuleTestCase
{
/**
* @dataProvider providerForValidCharset
*/
public function testValidDataWithCharsetShouldReturnTrue($charset, $input): void
{
$validator = new Charset($charset);
self::assertTrue($validator->__invoke($input));
}
/**
* @dataProvider providerForInvalidCharset
* @expectedException \Respect\Validation\Exceptions\CharsetException
*/
public function testInvalidCharsetShouldFailAndThrowCharsetException($charset, $input): void
{
$validator = new Charset($charset);
self::assertFalse($validator->__invoke($input));
$validator->assert($input);
}
/**
* @dataProvider providerForInvalidParams
* @test
*
* @expectedException \Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Invalid charset
*/
public function testInvalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($charset): void
public function itShouldThrowsExceptionWhenCharsetIsNotValid(): void
{
$validator = new Charset($charset);
new Charset('UTF-8', 'UTF-9');
}
public function providerForInvalidParams()
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
return [
[new \stdClass()],
[[]],
[null],
['16'],
['aeiou'],
['a'],
['Foo'],
['basic'],
[10],
[new Charset('UTF-8'), ''],
[new Charset('ISO-8859-1'), mb_convert_encoding('açaí', 'ISO-8859-1')],
[new Charset('UTF-8', 'ASCII'), 'strawberry'],
[new Charset('ASCII'), mb_convert_encoding('strawberry', 'ASCII')],
[new Charset('UTF-8'), '日本国'],
[new Charset('ISO-8859-1', 'EUC-JP'), '日本国'],
[new Charset('UTF-8'), 'açaí'],
[new Charset('ISO-8859-1'), 'açaí'],
];
}
public function providerForValidCharset()
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
['UTF-8', ''],
['ISO-8859-1', mb_convert_encoding('açaí', 'ISO-8859-1')],
[['UTF-8', 'ASCII'], 'strawberry'],
['ASCII', mb_convert_encoding('strawberry', 'ASCII')],
['UTF-8', '日本国'],
[['ISO-8859-1', 'EUC-JP'], '日本国'],
['UTF-8', 'açaí'],
['ISO-8859-1', 'açaí'],
];
}
$rule = new Charset('ASCII');
public function providerForInvalidCharset()
{
return [
['ASCII', '日本国'],
['ASCII', 'açaí'],
[$rule, '日本国'],
[$rule, 'açaí'],
];
}
}