SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Locales/LocaleInfoCAN.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 Canada; see {@see LocaleInfo}
  22.  */
  23. class LocaleInfoCAN extends LocaleInfo
  24. {
  25. protected $state_name = 'Province';
  26. protected $state_list = array(
  27. 'AB' => 'Alberta',
  28. 'BC' => 'British Columbia',
  29. 'MB' => 'Manitoba',
  30. 'NB' => 'New Brunswick',
  31. 'NL' => 'Newfoundland and Labrador',
  32. 'NT' => 'Northwest Territories',
  33. 'NS' => 'Nova Scotia',
  34. 'NU' => 'Nunavut',
  35. 'ON' => 'Ontario',
  36. 'PE' => 'Prince Edward Island',
  37. 'QC' => 'Quebec',
  38. 'SK' => 'Saskatchewan',
  39. 'YT' => 'Yukon',
  40. );
  41.  
  42. // English speaking uses period and space
  43. // French speaking uses comma and space
  44. protected $decimal_seperator = '.';
  45. protected $group_seperator = ' ';
  46.  
  47.  
  48. /**
  49.   * Validate a Canadian postcode, which must match the format 'A1A 1A1'
  50.   *
  51.   * @param string $code The postcode to validate
  52.   * @throws ValidationException If the format isn't correct
  53.   */
  54. public static function validatePostcode($code)
  55. {
  56. if (!preg_match('/^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/', $code)) {
  57. $err = 'Incorrect format';
  58.  
  59. $details = [];
  60. if (strpos($code, ' ') === false) {
  61. $details[] = 'space required';
  62. }
  63. if (preg_match('/[a-z]/', $code)) {
  64. $details[] = 'must be uppercase';
  65. }
  66.  
  67. if (count($details) > 0) {
  68. $err .= ' - ' . implode(', ', $details);
  69. }
  70.  
  71. throw new ValidationException($err);
  72. }
  73. }
  74.  
  75.  
  76. /**
  77.   * Validate address fields
  78.   *
  79.   * @param Validator $valid The validation object to add rules to
  80.   * @param bool $required Are the address fields required?
  81.   */
  82. public function validateAddress(Validator $valid, $required = false)
  83. {
  84. parent::validateAddress($valid, $required);
  85.  
  86. $valid->check('postcode', __CLASS__ . '::validatePostcode');
  87. $valid->check('postcode', 'Validity::length', 7, 7);
  88. }
  89. }
  90.