italiamultimedia/postal-code-validator

v1.0.1 2024-06-22 19:57 UTC

This package is auto-updated.

Last update: 2024-10-22 20:42:56 UTC


README

Basic postal code validation (format only).

Regular expression patterns are taken from https://i18napis.appspot.com/address/data/{COUNTRY_CODE}, however they are customized.

Usage example

composer require italiamultimedia/postal-code-validator
// Postal code validation
$postalCodeValidator = new \ItaliaMultimedia\PostalCodeValidator\PostalCodeValidator();
$postalCodeFormatHelper = new \ItaliaMultimedia\PostalCodeValidator\PostalCodeFormatHelper();
try {
    // Check "to"
    if ($this->data('toPostalCode')) {
        if (!$postalCodeValidator->isValid($this->data('toCountryId'), $this->data('toPostalCode'))) {
            $this->errors['toPostalCode'][] = \sprintf(
                '%s %s',
                \sprintf(\__('This field is not valid: %s.'), $this->setting('meta/toPostalCode')),
                \sprintf(
                    \__('Correct format: %s'),
                    $postalCodeFormatHelper->getFormat($this->data('toCountryId')),
                ),
            );
        }
    }
} catch (\ItaliaMultimedia\PostalCodeValidator\PostalCodeValidatorException $e) {
// Validation not available. Nothing to do.
}
// Postal code validation

Development

Implement new country

Add test case

  • tests/unit/ItaliaMultimedia/PostalCodeValidator/COUNTRYTest.php

Add code (Format helper)

  • src/ItaliaMultimedia/PostalCodeValidator/PostalCodeFormatHelper.php

Add code (Validator)

  • src/ItaliaMultimedia/PostalCodeValidator/PostalCodeValidator.php