italiamultimedia / postal-code-validator
A postal code validator
v1.0.1
2024-06-22 19:57 UTC
Requires
- php: ^8.3
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^5
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpcompatibility/php-compatibility: @dev
- phpmd/phpmd: ^2
- phpstan/phpstan: ^1
- phpstan/phpstan-strict-rules: ^1
- phpunit/phpunit: ^9
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^3
- vimeo/psalm: ^4
Suggests
- phpdocumentor/phpdocumentor: phpDocumentor v3 (global installation)
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