mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Apply contribution guidelines to "PostalCode" rule
This commit will also remove the second argument from the constructor of the rule because there is no much use for that, instead if will simply perform the CountryCode validation. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
6d3eb6afed
commit
21aa8eec62
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `PostalCode(string $countryCode)`
|
||||
|
||||
Validates a postal code according to the given country code.
|
||||
Validates whether the input is a valid postal code or not.
|
||||
|
||||
```php
|
||||
v::postalCode('BR')->validate('02179000'); // true
|
||||
|
|
|
@ -16,20 +16,17 @@ namespace Respect\Validation\Rules;
|
|||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
/**
|
||||
* Validates whether the input is a valid postal code or not.
|
||||
*
|
||||
* @see http://download.geonames.org/export/dump/countryInfo.txt
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class PostalCode extends AbstractEnvelope
|
||||
final class PostalCode extends AbstractEnvelope
|
||||
{
|
||||
public const DEFAULT_PATTERN = '/^$/';
|
||||
|
||||
// phpcs:disable Squiz.WhiteSpace.MemberVarSpacing.Incorrect
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
/**
|
||||
* @see http://download.geonames.org/export/dump/countryInfo.txt
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public $postalCodes = [
|
||||
private const DEFAULT_PATTERN = '/^$/';
|
||||
private const POSTAL_CODES = [
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
'AD' => '/^(?:AD)*(\d{3})$/',
|
||||
'AL' => '/^(\d{4})$/',
|
||||
'AM' => '/^(\d{6})$/',
|
||||
|
@ -190,22 +187,19 @@ class PostalCode extends AbstractEnvelope
|
|||
'YT' => '/^(\d{5})$/',
|
||||
'ZA' => '/^(\d{4})$/',
|
||||
'ZM' => '/^(\d{5})$/',
|
||||
// phpcs:enable Generic.Files.LineLength.TooLong
|
||||
];
|
||||
// phpcs:enable Generic.Files.LineLength.TooLong
|
||||
|
||||
public function __construct(string $countryCode, ?CountryCode $countryCodeRule = null)
|
||||
public function __construct(string $countryCode)
|
||||
{
|
||||
$countryCodeRule = $countryCodeRule ?: new CountryCode();
|
||||
$countryCodeRule = new CountryCode();
|
||||
if (!$countryCodeRule->validate($countryCode)) {
|
||||
throw new ComponentException(sprintf('Cannot validate postal code from "%s" country', $countryCode));
|
||||
}
|
||||
|
||||
$regex = self::DEFAULT_PATTERN;
|
||||
$upperCountryCode = mb_strtoupper($countryCode);
|
||||
if (isset($this->postalCodes[$upperCountryCode])) {
|
||||
$regex = $this->postalCodes[$upperCountryCode];
|
||||
}
|
||||
|
||||
parent::__construct(new Regex($regex), ['countryCode' => $countryCode]);
|
||||
parent::__construct(
|
||||
new Regex(self::POSTAL_CODES[$countryCode] ?? self::DEFAULT_PATTERN),
|
||||
['countryCode' => $countryCode]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
42
tests/integration/rules/postalCode.phpt
Normal file
42
tests/integration/rules/postalCode.phpt
Normal file
|
@ -0,0 +1,42 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\PostalCodeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::postalCode('BR')->check('1057BV');
|
||||
} catch (PostalCodeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::postalCode('NL'))->check('1057BV');
|
||||
} catch (PostalCodeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::postalCode('BR')->assert('1057BV');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::postalCode('NL'))->assert('1057BV');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"1057BV" must be a valid postal code on "BR"
|
||||
"1057BV" must not be a valid postal code on "NL"
|
||||
- "1057BV" must be a valid postal code on "BR"
|
||||
- "1057BV" must not be a valid postal code on "NL"
|
|
@ -13,11 +13,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\PostalCodeException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\PostalCode
|
||||
*
|
||||
* @author Axel Wargnier <axel@axessweb.fr>
|
||||
|
@ -25,7 +25,7 @@ use Respect\Validation\Test\TestCase;
|
|||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Sebastian <me@sebastianpontow.de>
|
||||
*/
|
||||
final class PostalCodeTest extends TestCase
|
||||
final class PostalCodeTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
@ -34,7 +34,7 @@ final class PostalCodeTest extends TestCase
|
|||
{
|
||||
$rule = new PostalCode('ZW');
|
||||
|
||||
self::assertTrue($rule->validate(''));
|
||||
self::assertValidInput($rule, '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +44,7 @@ final class PostalCodeTest extends TestCase
|
|||
{
|
||||
$rule = new PostalCode('ZW');
|
||||
|
||||
self::assertFalse($rule->validate(' '));
|
||||
self::assertInvalidInput($rule, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,83 +59,45 @@ final class PostalCodeTest extends TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validPostalCodesProvider
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldValidatePatternAccordingToTheDefinedCountryCode(string $countryCode, string $postalCode): void
|
||||
{
|
||||
$rule = new PostalCode($countryCode);
|
||||
|
||||
self::assertTrue($rule->validate($postalCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function validPostalCodesProvider(): array
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
['BR', '02179-000'],
|
||||
['BR', '02179000'],
|
||||
['CA', 'A1A 2B2'],
|
||||
['GB', 'GIR 0AA'],
|
||||
['GB', 'PR1 9LY'],
|
||||
['US', '02179'],
|
||||
['YE', ''],
|
||||
['PL', '99-300'],
|
||||
['NL', '1012 GX'],
|
||||
['NL', '1012GX'],
|
||||
['PT', '3660-606'],
|
||||
['PT', '3660606'],
|
||||
['CO', '110231'],
|
||||
['KR', '03187'],
|
||||
[new PostalCode('BR'), '02179-000'],
|
||||
[new PostalCode('BR'), '02179000'],
|
||||
[new PostalCode('CA'), 'A1A 2B2'],
|
||||
[new PostalCode('GB'), 'GIR 0AA'],
|
||||
[new PostalCode('GB'), 'PR1 9LY'],
|
||||
[new PostalCode('US'), '02179'],
|
||||
[new PostalCode('YE'), ''],
|
||||
[new PostalCode('PL'), '99-300'],
|
||||
[new PostalCode('NL'), '1012 GX'],
|
||||
[new PostalCode('NL'), '1012GX'],
|
||||
[new PostalCode('PT'), '3660-606'],
|
||||
[new PostalCode('PT'), '3660606'],
|
||||
[new PostalCode('CO'), '110231'],
|
||||
[new PostalCode('KR'), '03187'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidPostalCodesProvider
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldNotValidatePatternAccordingToTheDefinedCountryCode(
|
||||
string $countryCode,
|
||||
string $postalCode
|
||||
): void {
|
||||
$rule = new PostalCode($countryCode);
|
||||
|
||||
self::assertFalse($rule->validate($postalCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\PostalCodeException
|
||||
* @expectedExceptionMessage "02179-000" must be a valid postal code on "US"
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldThrowsPostalCodeExceptionWhenValidationFails(): void
|
||||
{
|
||||
$rule = new PostalCode('US');
|
||||
$rule->check('02179-000');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function invalidPostalCodesProvider(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['BR', '02179'],
|
||||
['BR', '02179.000'],
|
||||
['CA', '1A1B2B'],
|
||||
['GB', 'GIR 00A'],
|
||||
['GB', 'GIR0AA'],
|
||||
['GB', 'PR19LY'],
|
||||
['US', '021 79'],
|
||||
['YE', '02179'],
|
||||
['PL', '99300'],
|
||||
['KR', '548940'],
|
||||
['KR', '548-940'],
|
||||
[new PostalCode('BR'), '02179'],
|
||||
[new PostalCode('BR'), '02179.000'],
|
||||
[new PostalCode('CA'), '1A1B2B'],
|
||||
[new PostalCode('GB'), 'GIR 00A'],
|
||||
[new PostalCode('GB'), 'GIR0AA'],
|
||||
[new PostalCode('GB'), 'PR19LY'],
|
||||
[new PostalCode('US'), '021 79'],
|
||||
[new PostalCode('YE'), '02179'],
|
||||
[new PostalCode('PL'), '99300'],
|
||||
[new PostalCode('KR'), '548940'],
|
||||
[new PostalCode('KR'), '548-940'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue