SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Locales/LocaleInfoGBR.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 United Kingdom; see {@see LocaleInfo}
  22.  */
  23. class LocaleInfoGBR extends LocaleInfo
  24. {
  25. protected $state_name = 'County';
  26. protected $state_list = array(
  27. 'Aberdeenshire',
  28. 'Anglesey',
  29. 'Angus',
  30. 'Antrim',
  31. 'Argyll (Argyllshire)',
  32. 'Armagh',
  33. 'Ayrshire',
  34. 'Banffshire',
  35. 'Bedfordshire',
  36. 'Berkshire',
  37. 'Berwickshire',
  38. 'Brecknockshire (Breconshire)',
  39. 'Buckinghamshire',
  40. 'Buteshire',
  41. 'Caernarfonshire (Carnarvonshire)',
  42. 'Caithness',
  43. 'Cambridgeshire',
  44. 'Cardiganshire',
  45. 'Carmarthenshire',
  46. 'Cheshire',
  47. 'Clackmannanshire',
  48. 'Cornwall',
  49. 'Cromartyshire',
  50. 'Cumberland',
  51. 'Denbighshire',
  52. 'Derbyshire',
  53. 'Devon',
  54. 'Dorset',
  55. 'Down',
  56. 'Dumbartonshire',
  57. 'Dumfriesshire',
  58. 'Durham',
  59. 'East Lothian',
  60. 'Essex',
  61. 'Fermanagh',
  62. 'Fife',
  63. 'Flintshire',
  64. 'Glamorgan',
  65. 'Gloucestershire',
  66. 'Hampshire',
  67. 'Herefordshire',
  68. 'Hertfordshire',
  69. 'Huntingdonshire',
  70. 'Inverness-shire',
  71. 'Kent',
  72. 'Kincardineshire',
  73. 'Kirkcudbrightshire',
  74. 'Lanarkshire',
  75. 'Lancashire',
  76. 'Leicestershire',
  77. 'Lincolnshire',
  78. 'Londonderry',
  79. 'Merionethshire',
  80. 'Middlesex',
  81. 'Midlothian',
  82. 'Monmouthshire',
  83. 'Montgomeryshire',
  84. 'Morayshire',
  85. 'Nairnshire',
  86. 'Norfolk',
  87. 'Northamptonshire',
  88. 'Northumberland',
  89. 'Nottinghamshire',
  90. 'Orkney',
  91. 'Oxfordshire',
  92. 'Peeblesshire',
  93. 'Pembrokeshire',
  94. 'Perthshire',
  95. 'Radnorshire',
  96. 'Renfrewshire',
  97. 'Ross-shire',
  98. 'Roxburghshire',
  99. 'Rutland',
  100. 'Selkirkshire',
  101. 'Shetland',
  102. 'Shropshire',
  103. 'Somerset',
  104. 'Staffordshire',
  105. 'Stirlingshire',
  106. 'Suffolk',
  107. 'Surrey',
  108. 'Sussex',
  109. 'Sutherland',
  110. 'Tyrone',
  111. 'Warwickshire',
  112. 'West Lothian (Linlithgowshire)',
  113. 'Westmorland',
  114. 'Wigtownshire',
  115. 'Wiltshire',
  116. 'Worcestershire',
  117. 'Yorkshire',
  118. );
  119.  
  120. protected $town_name = 'Suburb';
  121.  
  122. protected $postcode_name = 'Postcode';
  123.  
  124. protected $currency_symbol = '£';
  125. protected $currency_name = 'Pound';
  126.  
  127.  
  128. /**
  129.   * Validate a UK postcode
  130.   *
  131.   * The supported formats are as follows:
  132.   * AA9A 9AA
  133.   * A9A 9AA
  134.   * A9 9AA
  135.   * A99 9AA
  136.   * AA9 9AA
  137.   * AA99 9AA
  138.   *
  139.   * @param string $code The postcode to validate
  140.   * @throws ValidationException If the format isn't correct
  141.   */
  142. public static function validatePostcode($code)
  143. {
  144. $allowed_formats = ['AA9A 9AA', 'A9A 9AA', 'A9 9AA', 'A99 9AA', 'AA9 9AA', 'AA99 9AA'];
  145.  
  146. foreach ($allowed_formats as $short_format) {
  147. $format = '/^' . str_replace(['A', '9'], ['[A-Z]', '[0-9]'], $short_format) . '$/';
  148. if (preg_match($format, $code)) {
  149. return;
  150. }
  151. }
  152.  
  153. $err = 'Incorrect format';
  154.  
  155. $details = [];
  156. if (strpos($code, ' ') === false) {
  157. $details[] = 'space required';
  158. }
  159. if (preg_match('/[a-z]/', $code)) {
  160. $details[] = 'must be uppercase';
  161. }
  162.  
  163. if (count($details) > 0) {
  164. $err .= ' - ' . implode(', ', $details);
  165. }
  166.  
  167. throw new ValidationException($err);
  168. }
  169.  
  170.  
  171. /**
  172.   * Validate address fields
  173.   *
  174.   * @param Validator $valid The validation object to add rules to
  175.   * @param bool $required Are the address fields required?
  176.   */
  177. public function validateAddress(Validator $valid, $required = false)
  178. {
  179. parent::validateAddress($valid, $required);
  180.  
  181. $valid->check('postcode', __CLASS__ . '::validatePostcode');
  182. $valid->check('postcode', 'Validity::length', 6, 8);
  183. }
  184. }
  185.