Skip to content

Commit 29d3fcc

Browse files
committed
Required improvements on "PostalCode" rule
- Turn rule case insensitive - Use default pattern for countries who doesn't have postal code
1 parent cbd0695 commit 29d3fcc

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

library/Rules/PostalCode.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class PostalCode extends Regex
88
{
9+
const DEFAULT_PATTERN = '/^$/';
10+
911
/**
1012
* @link http://download.geonames.org/export/dump/countryInfo.txt
1113
*/
@@ -32,6 +34,7 @@ class PostalCode extends Regex
3234
"CL" => "/^(\d{7})$/",
3335
"CN" => "/^(\d{6})$/",
3436
"CR" => "/^(\d{4})$/",
37+
"CS" => "/^(\d{5})$/",
3538
"CU" => "/^(?:CP)*(\d{5})$/",
3639
"CV" => "/^(\d{4})$/",
3740
"CX" => "/^(\d{4})$/",
@@ -160,15 +163,21 @@ class PostalCode extends Regex
160163
"YT" => "/^(\d{5})$/",
161164
"ZA" => "/^(\d{4})$/",
162165
"ZM" => "/^(\d{5})$/",
163-
"CS" => "/^(\d{5})$/",
164166
);
165167

166-
public function __construct($countryCode)
168+
public function __construct($countryCode, CountryCode $countryCodeRule = null)
167169
{
168-
if (!isset($this->postalCodes[$countryCode])) {
170+
$countryCodeRule = $countryCodeRule ?: new CountryCode();
171+
if (! $countryCodeRule->validate($countryCode)) {
169172
throw new ComponentException(sprintf('Cannot validate postal code from "%s" country', $countryCode));
170173
}
171174

172-
parent::__construct($this->postalCodes[$countryCode]);
175+
$regex = self::DEFAULT_PATTERN;
176+
$upperCountryCode = strtoupper($countryCode);
177+
if (isset($this->postalCodes[$upperCountryCode])) {
178+
$regex = $this->postalCodes[$upperCountryCode];
179+
}
180+
181+
parent::__construct($regex);
173182
}
174183
}
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,90 @@
11
<?php
22
namespace Respect\Validation\Rules;
33

4+
/**
5+
* @covers Respect\Validation\Rules\PostalCode
6+
*/
47
class PostalCodeTest extends \PHPUnit_Framework_TestCase
58
{
6-
public function testShouldUsePatternAccordingToLocale()
9+
public function testShouldUsePatternAccordingToCountryCode()
710
{
8-
$locale = 'BR';
11+
$countryCode = 'BR';
912

10-
$rule = new PostalCode($locale);
13+
$rule = new PostalCode($countryCode);
1114

1215
$actualPattern = $rule->regex;
13-
$expectedPattern = $rule->postalCodes[$locale];
16+
$expectedPattern = $rule->postalCodes[$countryCode];
1417

1518
$this->assertEquals($expectedPattern, $actualPattern);
1619
}
1720

21+
public function testShouldNotBeCaseSensitiveWhenChoosingPatternAccordingToCountryCode()
22+
{
23+
$rule1 = new PostalCode('BR');
24+
$rule2 = new PostalCode('br');
25+
26+
$this->assertEquals($rule1->regex, $rule2->regex);
27+
}
28+
29+
public function testShouldUseDefaultPatternWhenCountryCodeDoesNotHavePostalCode()
30+
{
31+
$rule = new PostalCode('ZW');
32+
33+
$actualPattern = $rule->regex;
34+
$expectedPattern = PostalCode::DEFAULT_PATTERN;
35+
36+
$this->assertEquals($expectedPattern, $actualPattern);
37+
}
38+
39+
public function testShouldValidateEmptyStringsWhenUsingDefaultPattern()
40+
{
41+
$rule = new PostalCode('ZW');
42+
43+
$this->assertTrue($rule->validate(''));
44+
}
45+
46+
public function testShouldNotValidateNonEmptyStringsWhenUsingDefaultPattern()
47+
{
48+
$rule = new PostalCode('ZW');
49+
50+
$this->assertFalse($rule->validate(' '));
51+
}
52+
1853
/**
1954
* @expectedException Respect\Validation\Exceptions\ComponentException
2055
* @expectedExceptionMessage Cannot validate postal code from "Whatever" country
2156
*/
22-
public function testShouldThrowsExceptionWhenCannotFindLocalePattern()
57+
public function testShouldThrowsExceptionWhenCountryCodeIsNotValid()
2358
{
2459
new PostalCode('Whatever');
2560
}
2661

2762
/**
2863
* @dataProvider validPostalCodesProvider
2964
*/
30-
public function testShouldValidatePatternAccordingToTheDefinedLocale($locale, $postalCode)
65+
public function testShouldValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
3166
{
32-
$rule = new PostalCode($locale);
67+
$rule = new PostalCode($countryCode);
3368

3469
$this->assertTrue($rule->validate($postalCode));
3570
}
3671

3772
public function validPostalCodesProvider()
3873
{
3974
return array(
40-
array('BR', '02179000'),
4175
array('BR', '02179-000'),
76+
array('BR', '02179000'),
4277
array('US', '02179'),
78+
array('YE', ''),
4379
);
4480
}
4581

4682
/**
4783
* @dataProvider invalidPostalCodesProvider
4884
*/
49-
public function testShouldNotValidatePatternAccordingToTheDefinedLocale($locale, $postalCode)
85+
public function testShouldNotValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
5086
{
51-
$rule = new PostalCode($locale);
87+
$rule = new PostalCode($countryCode);
5288

5389
$this->assertFalse($rule->validate($postalCode));
5490
}
@@ -59,6 +95,7 @@ public function invalidPostalCodesProvider()
5995
array('BR', '02179'),
6096
array('BR', '02179.000'),
6197
array('US', '021 79'),
98+
array('YE', '02179'),
6299
);
63100
}
64101
}

0 commit comments

Comments
 (0)