Apply contribution guidelines to "Base64" rule

Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
William Espindola 2018-06-09 02:18:55 -03:00 committed by Henrique Moody
parent f817655a82
commit 0010ffffb5
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 53 additions and 30 deletions

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class Base64Exception extends ValidationException
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jens Segers <segers.jens@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Base64Exception extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be Base64-encoded',

View file

@ -13,8 +13,22 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Base64 extends AbstractRule
use function is_string;
use function mb_strlen;
use function preg_match;
/**
* Validate if a string is Base64-encoded.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jens Segers <segers.jens@gmail.com>
* @author William Espindola <oi@william.espindola.com.br>
*/
final class Base64 extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (!is_string($input)) {

View file

@ -3,22 +3,36 @@
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\Base64Exception;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::base64()->check('=c3VyZS4');
} catch (Base64Exception $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::base64())->check('c3VyZS4=');
} catch (Base64Exception $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::base64()->assert('=c3VyZS4');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::base64())->assert('c3VyZS4=');
} catch (AllOfException $exception) {
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"=c3VyZS4" must be Base64-encoded
"c3VyZS4=" must not be Base64-encoded
- "=c3VyZS4" must be Base64-encoded
- "c3VyZS4=" must not be Base64-encoded

View file

@ -1,24 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\Base64Exception;
use Respect\Validation\Validator as v;
try {
v::base64()->check('=c3VyZS4');
} catch (Base64Exception $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::base64()->assert('=c3VyZS4');
} catch (AllOfException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"=c3VyZS4" must be Base64-encoded
- "=c3VyZS4" must be Base64-encoded

View file

@ -16,11 +16,19 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Base64
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jens Segers <segers.jens@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
class Base64Test extends RuleTestCase
final class Base64Test extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new Base64();
@ -50,6 +58,9 @@ class Base64Test extends RuleTestCase
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Base64();