Skip to content

Commit

Permalink
Deprecate subdivision-level postal code validation. Fixes #147.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Nov 29, 2020
1 parent 9ffacde commit 2450ca9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Subdivision/PatternType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* part of it. Used for postal code validation.
*
* @codeCoverageIgnore
* @deprecated since commerceguys/addressing 1.1.0.
*/
final class PatternType extends AbstractEnum
{
Expand Down
4 changes: 4 additions & 0 deletions src/Subdivision/Subdivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ public function getIsoCode()
* This is a regular expression pattern used to validate postal codes.
*
* @return string|null The postal code pattern.
*
* @deprecated since commerceguys/addressing 1.1.0.
*/
public function getPostalCodePattern()
{
Expand All @@ -250,6 +252,8 @@ public function getPostalCodePattern()
* Gets the postal code pattern type.
*
* @return string|null The postal code pattern type.
*
* @deprecated since commerceguys/addressing 1.1.0.
*/
public function getPostalCodePatternType()
{
Expand Down
11 changes: 11 additions & 0 deletions src/Validator/Constraints/AddressFormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class AddressFormatConstraint extends Constraint
*/
public $fieldOverrides;

/**
* Whether extended postal code validation is enabled.
*
* Extended postal code validation uses subdivision-level patterns to
* in addition to the country-level pattern supplied by the address format.
*
* This feature is deprecated, commerceguys/addressing 2.0 will only
* perform country-level validation.
*/
public $extendedPostalCodeValidation = true;

/**
* @var string
*/
Expand Down
22 changes: 12 additions & 10 deletions src/Validator/Constraints/AddressFormatConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ protected function validatePostalCode($postalCode, array $subdivisions, AddressF
// Resolve the available patterns.
$fullPattern = $addressFormat->getPostalCodePattern();
$startPattern = null;
foreach ($subdivisions as $subdivision) {
$pattern = $subdivision->getPostalCodePattern();
if (empty($pattern)) {
continue;
}

if ($subdivision->getPostalCodePatternType() == PatternType::FULL) {
$fullPattern = $pattern;
} else {
$startPattern = $pattern;
if (!empty($constraint->extendedPostalCodeValidation)) {
foreach ($subdivisions as $subdivision) {
$pattern = $subdivision->getPostalCodePattern();
if (empty($pattern)) {
continue;
}

if ($subdivision->getPostalCodePatternType() == PatternType::FULL) {
$fullPattern = $pattern;
} else {
$startPattern = $pattern;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public function testUnitedStatesNotOK()
*/
public function testUnitedStatesSubdivisionPostcodePattern()
{
// Test with subdivision-level postal code validation disabled.
$this->constraint->extendedPostalCodeValidation = false;

$address = new Address();
$address = $address
->withCountryCode('US')
Expand All @@ -168,6 +171,11 @@ public function testUnitedStatesSubdivisionPostcodePattern()
->withGivenName('John')
->withFamilyName('Smith');

$this->validator->validate($address, $this->constraint);
$this->assertNoViolation();

// Now test with the subdivision-level postal code validation enabled.
$this->constraint->extendedPostalCodeValidation = true;
$this->validator->validate($address, $this->constraint);
$this->buildViolation($this->constraint->invalidMessage)
->atPath('[postalCode]')
Expand Down

0 comments on commit 2450ca9

Please sign in to comment.