From 54293d4ca2846de8f7af47d251fdb33e94dc6fa8 Mon Sep 17 00:00:00 2001 From: Danilo Correa Date: Sat, 8 Sep 2018 21:26:22 -0300 Subject: [PATCH] Apply contribution guidelines to "Regex" rule Because some classes extend the "Regex" class this commit will also change the implementation of those classes to use "Regex" by composition instead of extending the class. Co-authored-by: Henrique Moody --- docs/rules/Regex.md | 4 +-- library/Exceptions/RegexException.php | 6 ++++- library/Rules/No.php | 4 +-- library/Rules/PostalCode.php | 8 ++---- library/Rules/Regex.php | 25 +++++++++++++++--- library/Rules/Roman.php | 5 ++-- tests/integration/rules/regex.phpt | 37 +++++++++++++++++++++++++++ tests/unit/Rules/NoTest.php | 32 ----------------------- tests/unit/Rules/PostalCodeTest.php | 28 -------------------- tests/unit/Rules/RegexTest.php | 6 +++-- 10 files changed, 75 insertions(+), 80 deletions(-) create mode 100644 tests/integration/rules/regex.phpt diff --git a/docs/rules/Regex.md b/docs/rules/Regex.md index f738b07f..7d2fc0b8 100644 --- a/docs/rules/Regex.md +++ b/docs/rules/Regex.md @@ -2,13 +2,13 @@ - `Regex(string $regex)` -Evaluates a regex on the input and validates if matches +Validates whether the input matches a defined regular expression. ```php v::regex('/[a-z]/')->validate('a'); // true ``` -Message template for this validator includes `{{regex}}` +Message template for this validator includes `{{regex}}`. ## Changelog diff --git a/library/Exceptions/RegexException.php b/library/Exceptions/RegexException.php index 4e6d95cc..6abfaba7 100644 --- a/library/Exceptions/RegexException.php +++ b/library/Exceptions/RegexException.php @@ -15,10 +15,14 @@ namespace Respect\Validation\Exceptions; /** * @author Alexandre Gomes Gaigalas + * @author Danilo Correa * @author Henrique Moody */ -class RegexException extends ValidationException +final class RegexException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must validate against {{regex}}', diff --git a/library/Rules/No.php b/library/Rules/No.php index a9dbb426..b2fb8565 100644 --- a/library/Rules/No.php +++ b/library/Rules/No.php @@ -16,7 +16,7 @@ namespace Respect\Validation\Rules; /** * @author Henrique Moody */ -class No extends Regex +class No extends AbstractEnvelope { public function __construct($useLocale = false) { @@ -25,6 +25,6 @@ class No extends Regex $pattern = nl_langinfo(NOEXPR); } - parent::__construct('/'.$pattern.'/i'); + parent::__construct(new Regex('/'.$pattern.'/i')); } } diff --git a/library/Rules/PostalCode.php b/library/Rules/PostalCode.php index 05e4f6df..8a219422 100644 --- a/library/Rules/PostalCode.php +++ b/library/Rules/PostalCode.php @@ -18,7 +18,7 @@ use Respect\Validation\Exceptions\ComponentException; /** * @author Henrique Moody */ -class PostalCode extends Regex +class PostalCode extends AbstractEnvelope { public const DEFAULT_PATTERN = '/^$/'; @@ -188,8 +188,6 @@ class PostalCode extends Regex 'ZM' => '/^(\d{5})$/', ]; - public $countryCode; - public function __construct($countryCode, CountryCode $countryCodeRule = null) { $countryCodeRule = $countryCodeRule ?: new CountryCode(); @@ -203,8 +201,6 @@ class PostalCode extends Regex $regex = $this->postalCodes[$upperCountryCode]; } - $this->countryCode = $countryCode; - - parent::__construct($regex); + parent::__construct(new Regex($regex), ['countryCode' => $countryCode]); } } diff --git a/library/Rules/Regex.php b/library/Rules/Regex.php index c4516b38..4286a302 100644 --- a/library/Rules/Regex.php +++ b/library/Rules/Regex.php @@ -13,25 +13,42 @@ declare(strict_types=1); namespace Respect\Validation\Rules; +use function is_scalar; +use function preg_match; + /** + * Validates whether the input matches a defined regular expression. + * * @author Alexandre Gomes Gaigalas + * @author Danilo Correa * @author Henrique Moody */ -class Regex extends AbstractRule +final class Regex extends AbstractRule { - public $regex; + /** + * @var string + */ + private $regex; - public function __construct($regex) + /** + * Initializes the rule. + * + * @param string $regex + */ + public function __construct(string $regex) { $this->regex = $regex; } + /** + * {@inheritdoc} + */ public function validate($input): bool { if (!is_scalar($input)) { return false; } - return (bool) preg_match($this->regex, (string) $input); + return preg_match($this->regex, (string) $input) > 0; } } diff --git a/library/Rules/Roman.php b/library/Rules/Roman.php index 403e1d9c..5e68bfab 100644 --- a/library/Rules/Roman.php +++ b/library/Rules/Roman.php @@ -18,11 +18,10 @@ namespace Respect\Validation\Rules; * @author Henrique Moody * @author Jean Pimentel */ -class Roman extends Regex +class Roman extends AbstractEnvelope { public function __construct() { - $pattern = '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'; - parent::__construct('/'.$pattern.'/'); + parent::__construct(new Regex('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/')); } } diff --git a/tests/integration/rules/regex.phpt b/tests/integration/rules/regex.phpt new file mode 100644 index 00000000..399f6747 --- /dev/null +++ b/tests/integration/rules/regex.phpt @@ -0,0 +1,37 @@ +--FILE-- +check('w poiur'); +} catch (RegexException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::not(v::regex('/^[a-z]+$/'))->check('wpoiur'); +} catch (RegexException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::regex('/^w+$/')->assert(new \stdClass()); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::regex('/^[a-z]+$/i'))->assert('wPoiur'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} +?> +--EXPECTF-- +"w poiur" must validate against "/^w+$/" +"wpoiur" must not validate against "/^[a-z]+$/" +- `[object] (stdClass: { })` must validate against "/^w+$/" +- "wPoiur" must not validate against "/^[a-z]+$/i" diff --git a/tests/unit/Rules/NoTest.php b/tests/unit/Rules/NoTest.php index 29dc3e5b..c9dad183 100644 --- a/tests/unit/Rules/NoTest.php +++ b/tests/unit/Rules/NoTest.php @@ -25,38 +25,6 @@ use Respect\Validation\Test\TestCase; */ class NoTest extends TestCase { - /** - * @test - */ - public function shouldUseDefaultPattern(): void - { - $rule = new No(); - - $actualPattern = $rule->regex; - $expectedPattern = '/^n(o(t|pe)?|ix|ay)?$/i'; - - self::assertEquals($expectedPattern, $actualPattern); - } - - /** - * @test - */ - public function shouldUseLocalPatternForNoExpressionWhenDefined(): void - { - if (!defined('NOEXPR')) { - self::markTestSkipped('Constant NOEXPR is not defined'); - - return; - } - - $rule = new No(true); - - $actualPattern = $rule->regex; - $expectedPattern = '/'.nl_langinfo(NOEXPR).'/i'; - - self::assertEquals($expectedPattern, $actualPattern); - } - /** * @dataProvider validNoProvider * diff --git a/tests/unit/Rules/PostalCodeTest.php b/tests/unit/Rules/PostalCodeTest.php index 375ceb77..0004736e 100644 --- a/tests/unit/Rules/PostalCodeTest.php +++ b/tests/unit/Rules/PostalCodeTest.php @@ -27,34 +27,6 @@ use Respect\Validation\Test\TestCase; */ class PostalCodeTest extends TestCase { - /** - * @test - */ - public function shouldUsePatternAccordingToCountryCode(): void - { - $countryCode = 'BR'; - - $rule = new PostalCode($countryCode); - - $actualPattern = $rule->regex; - $expectedPattern = $rule->postalCodes[$countryCode]; - - self::assertEquals($expectedPattern, $actualPattern); - } - - /** - * @test - */ - public function shouldUseDefaultPatternWhenCountryCodeDoesNotHavePostalCode(): void - { - $rule = new PostalCode('ZW'); - - $actualPattern = $rule->regex; - $expectedPattern = PostalCode::DEFAULT_PATTERN; - - self::assertEquals($expectedPattern, $actualPattern); - } - /** * @test */ diff --git a/tests/unit/Rules/RegexTest.php b/tests/unit/Rules/RegexTest.php index 1932692c..74b5ce99 100644 --- a/tests/unit/Rules/RegexTest.php +++ b/tests/unit/Rules/RegexTest.php @@ -14,9 +14,11 @@ declare(strict_types=1); namespace Respect\Validation\Rules; use Respect\Validation\Test\RuleTestCase; +use stdClass; /** - * @group rule + * @group rule + * * @covers \Respect\Validation\Rules\Regex * * @author Alexandre Gomes Gaigalas @@ -43,7 +45,7 @@ final class RegexTest extends RuleTestCase { return [ [new Regex('/^w+$/'), 'w poiur'], - [new Regex('/^w+$/'), new \stdClass()], + [new Regex('/^w+$/'), new stdClass()], [new Regex('/^w+$/'), []], ]; }