dank00 / phpostcode
Represent uk postcodes in PHP
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/dank00/phpostcode
Requires (Dev)
This package is auto-updated.
Last update: 2020-01-25 18:45:31 UTC
README
A Postcode value object based on this https://www.mrs.org.uk/pdf/postcodeformat.pdf
If you want a postcode library to acutally use, I recommend this instead - https://github.com/vasildakov/postcode
Installation and Usage
The package is available on packagist.org, so use composer:
composer require dank00/phpostcode
Most applications will store postcodes as strings so you will typically use the fromString method:
$string = $address->getPostcode(); // 'CW8 4BW' $postcode = Postcode::fromString($string); echo $postcode->toString(); // 'CW8 4BW' echo $postcode->getOutwardCode()->toString(); // 'CW8' echo $postcode->getInwardCode()->getUnit(); // `BW`
If you need more precision, then OutwardCode and InwardCode also have fromString methods, or use the constructors directly.
An InvalidCodeException is thrown from the constructors of OutwardCode and InwardCode in the case that the inputs do not match the fixed patterns (which are also defined in those classes).