Address.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Faker\Provider\en_CA; |
| 4 | |
| 5 | /** |
| 6 | * Extend US class since most fields share the same format |
| 7 | */ |
| 8 | |
| 9 | class Address extends \Faker\Provider\en_US\Address |
| 10 | { |
| 11 | protected static $postcode = array('?#? #?#', '?#?-#?#', '?#?#?#'); |
| 12 | |
| 13 | protected static $postcodeLetters = array('A','B','C','E','G','H','J','K','L','M','N','P','R','S','T','V','X','Y'); |
| 14 | |
| 15 | protected static $province = array( |
| 16 | 'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland and Labrador', 'Northwest Territories', 'Nova Scotia', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Yukon Territory', |
| 17 | ); |
| 18 | |
| 19 | protected static $provinceAbbr = array( |
| 20 | 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT' |
| 21 | ); |
| 22 | |
| 23 | protected static $addressFormats = array( |
| 24 | "{{streetAddress}}\n{{city}}, {{provinceAbbr}} {{postcode}}", |
| 25 | ); |
| 26 | |
| 27 | /** |
| 28 | * @example 'Ontario' |
| 29 | */ |
| 30 | public static function province() |
| 31 | { |
| 32 | return static::randomElement(static::$province); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @example 'ON' |
| 37 | */ |
| 38 | public static function provinceAbbr() |
| 39 | { |
| 40 | return static::randomElement(static::$provinceAbbr); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a postalcode-safe letter |
| 45 | * @example A1B 2C3 |
| 46 | */ |
| 47 | public static function randomPostcodeLetter() |
| 48 | { |
| 49 | return static::randomElement(static::$postcodeLetters); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @example A1B 2C3 |
| 54 | */ |
| 55 | public static function postcode() |
| 56 | { |
| 57 | $string = static::randomElement(static::$postcode); |
| 58 | |
| 59 | $string = preg_replace_callback('/\#/u', 'static::randomDigit', $string); |
| 60 | $string = preg_replace_callback('/\?/u', 'static::randomPostcodeLetter', $string); |
| 61 | |
| 62 | return static::toUpper($string); |
| 63 | } |
| 64 | } |
| 65 |