SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Locales/LocaleInfoUSA.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers\Locales;
  15.  
  16. use Sprout\Exceptions\ValidationException;
  17. use Sprout\Helpers\Validator;
  18.  
  19.  
  20. /**
  21.  * Locale info for USA; see {@see LocaleInfo}
  22.  */
  23. class LocaleInfoUSA extends LocaleInfo
  24. {
  25. protected $state_name = 'State';
  26. protected $state_list = array(
  27. 'AL'=>"Alabama",
  28. 'AK'=>"Alaska",
  29. 'AZ'=>"Arizona",
  30. 'AR'=>"Arkansas",
  31. 'CA'=>"California",
  32. 'CO'=>"Colorado",
  33. 'CT'=>"Connecticut",
  34. 'DE'=>"Delaware",
  35. 'DC'=>"District Of Columbia",
  36. 'FL'=>"Florida",
  37. 'GA'=>"Georgia",
  38. 'HI'=>"Hawaii",
  39. 'ID'=>"Idaho",
  40. 'IL'=>"Illinois",
  41. 'IN'=>"Indiana",
  42. 'IA'=>"Iowa",
  43. 'KS'=>"Kansas",
  44. 'KY'=>"Kentucky",
  45. 'LA'=>"Louisiana",
  46. 'ME'=>"Maine",
  47. 'MD'=>"Maryland",
  48. 'MA'=>"Massachusetts",
  49. 'MI'=>"Michigan",
  50. 'MN'=>"Minnesota",
  51. 'MS'=>"Mississippi",
  52. 'MO'=>"Missouri",
  53. 'MT'=>"Montana",
  54. 'NE'=>"Nebraska",
  55. 'NV'=>"Nevada",
  56. 'NH'=>"New Hampshire",
  57. 'NJ'=>"New Jersey",
  58. 'NM'=>"New Mexico",
  59. 'NY'=>"New York",
  60. 'NC'=>"North Carolina",
  61. 'ND'=>"North Dakota",
  62. 'OH'=>"Ohio",
  63. 'OK'=>"Oklahoma",
  64. 'OR'=>"Oregon",
  65. 'PA'=>"Pennsylvania",
  66. 'RI'=>"Rhode Island",
  67. 'SC'=>"South Carolina",
  68. 'SD'=>"South Dakota",
  69. 'TN'=>"Tennessee",
  70. 'TX'=>"Texas",
  71. 'UT'=>"Utah",
  72. 'VT'=>"Vermont",
  73. 'VA'=>"Virginia",
  74. 'WA'=>"Washington",
  75. 'WV'=>"West Virginia",
  76. 'WI'=>"Wisconsin",
  77. 'WY'=>"Wyoming"
  78. );
  79.  
  80. protected $town_name = 'Suburb';
  81.  
  82. protected $postcode_name = 'ZIP Code';
  83.  
  84.  
  85. /**
  86.   * Validate a ZIP Code, as a 5-digit number with an optional appended hyphen with 4 additional digits
  87.   * E.g. 20521 or 20521-9000
  88.   *
  89.   * @param string $code The postcode to validate
  90.   * @throws ValidationException If the format isn't correct
  91.   */
  92. public static function validatePostcode($code)
  93. {
  94. if (!preg_match('/^[0-9]{5}(?:-[0-9]{4})?$/i', $code)) {
  95. throw new ValidationException('Incorrect format');
  96. }
  97. }
  98.  
  99.  
  100. /**
  101.   * Validate address fields
  102.   *
  103.   * @param Validator $valid The validation object to add rules to
  104.   * @param bool $required Are the address fields required?
  105.   */
  106. public function validateAddress(Validator $valid, $required = false)
  107. {
  108. parent::validateAddress($valid, $required);
  109.  
  110. $valid->check('postcode', __CLASS__ . '::validatePostcode');
  111.  
  112. // Parent validation checks for postcode length <= 10, so don't double up the max length error message
  113. $valid->check('postcode', 'Validity::length', 5, PHP_INT_MAX);
  114. }
  115. }
  116.  
  117.  
  118.