mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 01:30:05 +02:00
Apply contribution guidelines to "PhpLabel" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
30993fc4a0
commit
73a0107349
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class PhpLabelException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Emmerson <emmersonsiqueira@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class PhpLabelException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a valid PHP label',
|
||||
|
|
|
@ -13,8 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class PhpLabel extends AbstractRule
|
||||
use function is_string;
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Validates if a value is considered a valid PHP Label, so that it can be used as a variable, function or class name.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Emmerson <emmersonsiqueira@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class PhpLabel extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return is_string($input) && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $input);
|
||||
|
|
|
@ -4,14 +4,36 @@ PhpLabel rule exception should not be thrown for valid inputs
|
|||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\PhpLabelException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::phpLabel()->check('topic01');
|
||||
v::phpLabel()->assert('access');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getMessage();
|
||||
v::phpLabel()->check('f o o');
|
||||
} catch (PhpLabelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::phpLabel())->check('correctOne');
|
||||
} catch (PhpLabelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::phpLabel()->assert('0wner');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::phpLabel())->assert('Respect');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"f o o" must be a valid PHP label
|
||||
"correctOne" must not be a valid PHP label
|
||||
- "0wner" must be a valid PHP label
|
||||
- "Respect" must not be a valid PHP label
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
--TEST--
|
||||
PhpLabel rule exception should be thrown by check() method
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\PhpLabelException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::phpLabel()->check('f o o');
|
||||
} catch (PhpLabelException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::phpLabel())->check('correctOne');
|
||||
} catch (PhpLabelException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"f o o" must be a valid PHP label
|
||||
"correctOne" must not be a valid PHP label
|
|
@ -1,24 +0,0 @@
|
|||
--TEST--
|
||||
PhpLabel rule exception should be thrown by assert() method
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::phpLabel()->assert('0wner');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::phpLabel())->assert('Respect');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- "0wner" must be a valid PHP label
|
||||
- "Respect" must not be a valid PHP label
|
|
@ -14,14 +14,25 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use function mb_strtoupper;
|
||||
use function mt_rand;
|
||||
use function random_int;
|
||||
use function uniqid;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\PhpLabelException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\PhpLabel
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Emmerson <emmersonsiqueira@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class PhpLabelTest extends RuleTestCase
|
||||
final class PhpLabelTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new PhpLabel();
|
||||
|
@ -37,6 +48,9 @@ class PhpLabelTest extends RuleTestCase
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new PhpLabel();
|
||||
|
|
Loading…
Reference in a new issue