Validate_CA
[ class tree: Validate_CA ] [ index: Validate_CA ] [ all elements ]

Source for file CA.php

Documentation is available at CA.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in Canada
  5.  *
  6.  * PHP Versions 4 and 5
  7.  *
  8.  * This source file is subject to the New BSD license, That is bundled
  9.  * with this package in the file LICENSE, and is available through
  10.  * the world-wide-web at
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the new BSDlicense and are unable
  13.  * to obtain it through the world-wide-web, please send a note to
  14.  * pajoye@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category  Validate
  17.  * @package   Validate_CA
  18.  * @author    Philippe Jausions <Philippe.Jausions@11abacus.com>
  19.  * @copyright 1997-2005 Philippe Jausions
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  21.  * @version   CVS: $Id: CA.php,v 1.12 2008/09/01 17:17:41 jausions Exp $
  22.  * @link      http://pear.php.net/package/Validate_CA
  23.  */
  24.  
  25. /**
  26.  * Data validation class for Canada
  27.  *
  28.  * This class provides methods to validate:
  29.  *  - Social Insurance Number (aka SIN)
  30.  *  - Province code
  31.  *  - Telephone number
  32.  *  - Postal code
  33.  *
  34.  * @category  Validate
  35.  * @package   Validate_CA
  36.  * @author    Philippe Jausions <Philippe.Jausions@11abacus.com>
  37.  * @copyright 1997-2005 Philippe Jausions
  38.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  39.  * @version   Release: @package_version@
  40.  * @link      http://pear.php.net/package/Validate_CA
  41.  */
  42. {
  43.     /**
  44.      * Validates a number according to Luhn check algorithm
  45.      *
  46.      * This function checks given number according Luhn check
  47.      * algorithm. It is published on several places, see links.
  48.      *
  49.      * @param string $number number to check
  50.      *
  51.      * @return bool    TRUE if number is valid, FALSE otherwise
  52.      * @access public
  53.      * @static
  54.      * @deprecated Scheduled for removal before beta release
  55.      * @link http://www.webopedia.com/TERM/L/Luhn_formula.html
  56.      * @link http://www.merriampark.com/anatomycc.htm
  57.      * @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
  58.      * @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
  59.      */
  60.     function Luhn($number)
  61.     {
  62.         return Validate_CA::_luhn($number);
  63.     }
  64.  
  65.     /**
  66.      * Validates a number according to Luhn check algorithm
  67.      *
  68.      * This function checks given number according Luhn check
  69.      * algorithm. It is published on several places, see links:
  70.      *
  71.      * @param string $number number to check
  72.      *
  73.      * @return bool    TRUE if number is valid, FALSE otherwise
  74.      * @access protected
  75.      * @static
  76.      * @link http://www.webopedia.com/TERM/L/Luhn_formula.html
  77.      * @link http://www.merriampark.com/anatomycc.htm
  78.      * @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
  79.      * @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
  80.      */
  81.     function _luhn($number)
  82.     {
  83.         $len_number strlen($number);
  84.         $sum        = 0;
  85.         for ($k $len_number % 2; $k $len_number$k += 2{
  86.             if ((intval($number{$k}* 2> 9{
  87.                 $sum += (intval($number{$k}* 2- 9;
  88.             else {
  89.                 $sum += intval($number{$k}* 2;
  90.             }
  91.         }
  92.         for ($k ($len_number % 2^ 1; $k $len_number$k += 2{
  93.             $sum += intval($number{$k});
  94.         }
  95.         return ($sum % 10? false : true;
  96.     }
  97.  
  98.     /**
  99.      * Validates a Canadian social insurance number (SIN)
  100.      *
  101.      * For unification between country-based validation packages,
  102.      * this method is named ssn()
  103.      *
  104.      * @param string $ssn        number to validate
  105.      * @param int    $expiryDate expiry date for SIN starting
  106.      *                            with a 9 (UNIX timestamp)
  107.      *
  108.      * @return bool 
  109.      * @link http://www.hrsdc.gc.ca/en/hip/lld/cesg/promotersection/files/Interface_Transaction_Standards_V301_English.pdf
  110.      */
  111.     function ssn($ssn$expiryDate = null)
  112.     {
  113.         // remove any dashes, spaces, returns, tabs or slashes
  114.         $ssn str_replace(array('-','/',' ',"\t")''trim($ssn));
  115.  
  116.         // Basic checking
  117.         if (($len strlen($ssn)) != 9
  118.             || strspn($ssn'0123456789'!= $len
  119.             || ($ssn{0== '9' && $expiryDate <= time())
  120.             || $ssn{0== '0' || $ssn{0== '3' || $ssn{0== '8'{
  121.             return false;
  122.         }
  123.  
  124.         return Validate_CA::_luhn($ssn);
  125.     }
  126.  
  127.     /**
  128.      * Validates a Canadian Postal Code
  129.      *
  130.      * @param string $postalCode the postal code to validate
  131.      * @param string $province   the province code
  132.      *
  133.      * @return boolean TRUE if code is valid, FALSE otherwise
  134.      * @access public
  135.      * @static
  136.      * @link www.canadapost.ca/business/tools/pg/preparation/mpp2-04-e.asp#c154
  137.      */
  138.     function postalCode($postalCode$province '')
  139.     {
  140.         $letters 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  141.         if (!$province{
  142.             $sRegExp = "[ABCEGHJKLMNPRSTVXY][0-9][$letters]"
  143.                      . "[ \t-]*[0-9][$letters][0-9]";
  144.  
  145.         else {
  146.             switch (strtoupper($province)) {
  147.             case 'NL':          // Newfoundland and Labrador
  148.             case 'NF':          // Newfoundland (kept for BC)
  149.                 $sRegExp 'A';
  150.                 break;
  151.             case 'NS':          // Nova Scotia
  152.                 $sRegExp 'B';
  153.                 break;
  154.             case 'PE':          // Prince Edward Island
  155.                 $sRegExp 'C';
  156.                 break;
  157.             case 'NB':          // New Brunswick
  158.                 $sRegExp 'E';
  159.                 break;
  160.             case 'QC':          // Quebec
  161.                 $sRegExp '[GHJ]';
  162.                 break;
  163.             case 'ON':          // Ontario
  164.                 $sRegExp '[KLMNP]';
  165.                 break;
  166.             case 'MB':          // Manitoba
  167.                 $sRegExp 'R';
  168.                 break;
  169.             case 'SK':          // Saskatchewan
  170.                 $sRegExp 'S';
  171.                 break;
  172.             case 'AB':          // Alberta
  173.                 $sRegExp 'T';
  174.                 break;
  175.             case 'BC':          // British Columbia
  176.                 $sRegExp 'V';
  177.                 break;
  178.             case 'NT':          // Northwest Territories
  179.             case 'NU':          // Nunavut
  180.                 $sRegExp 'X';
  181.                 break;
  182.             case 'YK':          // Yukon Territory
  183.             case 'YT':          // Yukon Territory (Canada Post)
  184.                 $sRegExp 'Y';
  185.                 break;
  186.             default:
  187.                 return false;
  188.             }
  189.  
  190.             $sRegExp .= "[0-9][$letters][ \t-]*[0-9][ $letters][0-9]";
  191.         }
  192.  
  193.         $sRegExp '/^' $sRegExp '$/';
  194.  
  195.         return (bool) preg_match($sRegExpstrtoupper($postalCode));
  196.     }
  197.  
  198.     /**
  199.      * Validates a "region" (i.e. province) code
  200.      *
  201.      * @param string $region 2-letter province code
  202.      *
  203.      * @return bool Whether the code is a valid province
  204.      * @access public
  205.      * @static
  206.      */
  207.     function region($region)
  208.     {
  209.         switch (strtoupper($region)) {
  210.         case 'AB':
  211.         case 'BC':
  212.         case 'MB':
  213.         case 'NB':
  214.         case 'NF':    // Newfoundland (kept for BC)
  215.         case 'NL':
  216.         case 'NT':
  217.         case 'NS':
  218.         case 'NU':
  219.         case 'ON':
  220.         case 'PE':
  221.         case 'QC':
  222.         case 'SK':
  223.         case 'YK':
  224.         case 'YT':    // Yukon (Canada Post)
  225.             return true;
  226.         }
  227.         return false;
  228.     }
  229.  
  230.     /**
  231.      * Validates a Canadian phone number.
  232.      *
  233.      * Canada and the United States share the same numbering plan,
  234.      * hence you can also call Validate_US::phoneNumber()
  235.      *
  236.      * Can allow only seven digit numbers.
  237.      * Also allows the formats, (xxx) xxx-xxxx, xxx xxx-xxxx,
  238.      * And now x (xxx) xxx-xxxx
  239.      * or various combination without spaces, dashes.
  240.      * THIS SHOULD EVENTUALLY take a FORMAT in the options, instead
  241.      *
  242.      * @param string $number       phone to validate
  243.      * @param bool   $withAreaCode require the area code?
  244.      *
  245.      * @return bool Whether the phone number is valid.
  246.      * @access public
  247.      * @static
  248.      */
  249.     function phoneNumber($number$withAreaCode = true)
  250.     {
  251.         if ($number == ''{
  252.             return true;
  253.         }
  254.  
  255.         if (!$withAreaCode{
  256.             // just seven digits, maybe a space or dash
  257.             return (boolean)preg_match('/^[2-9]\d{2}[- ]?\d{4}$/'$number);
  258.         }
  259.         // ten digits, maybe  spaces and/or dashes and/or parentheses
  260.         // maybe a 1 or a 0..
  261.         $reg '/^[0-1]?[- ]?(\()?[2-9]\d{2}(?(1)\))[- ]?[2-9]\d{2}[- ]?\d{4}$/';
  262.         return (boolean)preg_match($reg$number);
  263.     }
  264. }
  265. ?>

Documentation generated on Mon, 01 Sep 2008 13:30:03 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.