mirror of
https://github.com/Respect/Validation.git
synced 2024-06-20 06:25:05 +02:00
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 <henriquemoody@gmail.com>
This commit is contained in:
parent
81d71749b7
commit
54293d4ca2
|
@ -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
|
||||
|
||||
|
|
|
@ -15,10 +15,14 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class RegexException extends ValidationException
|
||||
final class RegexException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must validate against {{regex}}',
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Respect\Validation\Rules;
|
|||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <alexandre@gaigalas.net>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,11 +18,10 @@ namespace Respect\Validation\Rules;
|
|||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
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})$/'));
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/regex.phpt
Normal file
37
tests/integration/rules/regex.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\RegexException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::regex('/^w+$/')->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"
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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 <alexandre@gaigalas.net>
|
||||
|
@ -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+$/'), []],
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue