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:
Danilo Correa 2018-09-08 21:26:22 -03:00 committed by Henrique Moody
parent 81d71749b7
commit 54293d4ca2
No known key found for this signature in database
GPG key ID: 221E9281655813A6
10 changed files with 75 additions and 80 deletions

View file

@ -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

View file

@ -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}}',

View file

@ -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'));
}
}

View file

@ -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]);
}
}

View file

@ -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;
}
}

View file

@ -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})$/'));
}
}

View 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"

View file

@ -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
*

View file

@ -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
*/

View file

@ -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+$/'), []],
];
}