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

Source for file NZ.php

Documentation is available at NZ.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2006 Byron Adams                                     |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is subject to the New BSD license, That is bundled  |
  7. // | with this package in the file LICENSE, and is available through      |
  8. // | the world-wide-web at                                                |
  9. // | http://www.opensource.org/licenses/bsd-license.php                   |
  10. // | If you did not receive a copy of the new BSDlicense and are unable   |
  11. // | to obtain it through the world-wide-web, please send a note to       |
  12. // | pajoye@php.net so we can mail you a copy immediately.                |
  13. // +----------------------------------------------------------------------+
  14. // | Author: Byron Adams <Byron.adams54@gmail.com>                        |
  15. // +----------------------------------------------------------------------+
  16. //
  17. /**
  18.  *  Data validation class for New Zealand
  19.  *
  20.  * This class provides methods to validate:
  21.  *  - IRD numbers
  22.  *  - Regional codes
  23.  *  - Telephone number
  24.  *  - Postal code
  25.  *  - Bank AC
  26.  *
  27.  * @category   Validate
  28.  * @package    Validate_NZ
  29.  * @author     Byron Adams <Byron.adams54@gmail.com>
  30.  * @copyright  (c) 2006 Byron Adams
  31.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  32.  */
  33.  
  34. {
  35.     /**
  36.      * Validate  New Zealand postal codes
  37.      *
  38.      * @static
  39.      * @access   public
  40.      * @param    string    $postcode,   postcode to validate
  41.      * @param    bool      $strong,     optional; strong checks against a list of postcodes
  42.      * @return   bool 
  43.      * @link      http://www.nzpost.co.nz/nzpost/images/addressing.nzpost/pdfs/postcodedirectory_nomaps.pdf
  44.      */
  45.     function postalCode($postcode,$strong = true)
  46.     {
  47.         if ($strong{
  48.             static $postcodes;
  49.  
  50.             $postcodes = array("0110","0420","0310","1010","0610","0600","2012","2105",
  51.                                "0505","1081","1022","2102","2010","2022","2013","0630",
  52.                                "0614","0612","2014","1025","0931","3210","3214","3204",
  53.                                "3200","3410","3118","3112","3015","4130","4122","4110",
  54.                                "4312","4501","4500","4310","4825","4820","5032","5024",
  55.                                "5510","4410","5036","5018","6022","5010","6011","6037",
  56.                                "5028","5034","7281","7173","7196","7073","7183","7005",
  57.                                "7007","7022","7025","7071","7072","7077","7081","7091",
  58.                                "7095","7096","7220","7110","7120","7282","7284","7175",
  59.                                "7182","7194","7192","7193","7195","7197","7198","7204",
  60.                                "7210","7100","7271","7272","7273","7274","7275","7276",
  61.                                "7285","7178","7201","7010","7011","7020","8013","8041",
  62.                                "7334","8011","7402","8022","8083","8062","7999","8024",
  63.                                "8053","8051","7832","7300","7802","8042","8025","8081",
  64.                                "7610","7825","8014","9810","9016","9010","9822","9400",
  65.                                "9012","9014","9022","9013","9023","9401","9812");
  66.  
  67.             return in_array((int)$postcode$postcodes);
  68.         }
  69.         
  70.         return (bool)ereg('^[0-9]{4}$'$postcode);
  71.     }
  72.  
  73.     /**
  74.      * Validates a New Zealand IRD Number (ssn equiv)
  75.      *
  76.      * recently the format has changed to having a
  77.      * prefix of 0, this will work with both new and old IRD numbers.
  78.      *
  79.      * @param   string     $ssn;  IRD number to validate
  80.      * @return  bool 
  81.      */
  82.     function ssn($ssn)
  83.     {
  84.         $ssn str_replace(array("-"," ","."),'',trim($ssn));
  85.  
  86.         if (!ctype_digit($ssn)) {
  87.             return false;
  88.         }
  89.         
  90.         switch ($ssn{
  91.             case 8:
  92.                 return true;
  93.             break;
  94.             case 9:
  95.                 if ($ssn{0== "0"{
  96.                     return true;
  97.                 }
  98.             break;
  99.         }
  100.         
  101.         return false;
  102.         
  103.     }
  104.  
  105.     /**
  106.      * Validates a New Zealand Regional Code
  107.      *
  108.      * @param     string     $region, regional code to validate
  109.      * @returns   bool
  110.      * @link      http://www.google.com/apis/adwords/developer/adwords_api_regions.html
  111.      */
  112.     function region($region)
  113.     {
  114.        $region strtoupper($region);    
  115.        
  116.        
  117.        $regions = array("AUK""AUCKLAND",
  118.                         "BOP""BAY OF PLENTY",
  119.                         "CAN""CANTERBURY",
  120.                         "GIS""GISBORNE",
  121.                         "HKB""HAWKES BAY",
  122.                         "MBH""MARLBOROUGH",
  123.                         "MWT""MANAWATU WANGANUI",
  124.                         "NSN""NELSON",
  125.                         "NTL""NORTHLAND",
  126.                         "OTA""OTAGO",
  127.                         "STL""SOUTHLAND",
  128.                         "TAS""TASMAN",
  129.                         "TKI""TARANAKI",
  130.                         "WGN""WELLINGTON",
  131.                         "WKO""WAIKATO",
  132.                         "WTC""WEST COAST");
  133.        
  134.        if (in_array($region,$regions)) {
  135.            return true;    
  136.        else {
  137.            $region str_replace(array("NORTH","SOUTH","EAST","WEST","CENTRAL","'","-"," "),array("","","","","",""," ",""),trim($region));
  138.           
  139.            return in_array($region,$regions);
  140.        }
  141.        
  142.        return false;
  143.     }
  144.  
  145.     /**
  146.      * Validates a New Zealand phone number
  147.      *
  148.      * This function validates all New Zealand phone numbers
  149.      * checks for landline,0800,0900,0508,021,027 and 025 numbers
  150.      * allows for various combinations with spaces dashes and parentheses.
  151.      *
  152.      * @param     string     $number, the number to validate
  153.      * @returns   bool
  154.      * @link
  155.      */
  156.     function phoneNumber($number$requireAreaCode = true)
  157.     {
  158.         
  159.        $number str_replace(array("+"," ","(",")","-"),array("00","","","",""),trim($number));
  160.        
  161.        if (!ctype_digit($number)) {
  162.            return false;
  163.        else {
  164.            
  165.            $numlength strlen($number);
  166.            
  167.            switch ($numlength{
  168.                   case 7:
  169.                       if (!$requireAreaCode{
  170.                           $regexp "(^[0-9]{7}$)"// Is land line w/o area code
  171.                       }
  172.                   break;
  173.                case 9:
  174.                       $regexp "(^0(3|4|6|7|9)[0-9]{7}$)";   // Is land line with area code
  175.                   break; 
  176.                   case 10:
  177.                       if (in_array(substr($number,0,4),array("0800","0900","0508"))) 
  178.                           $regexp "(^0(8|9|5)0(0|8)[0-9]{6}$)"// Is 0800,0900 or 0508 number
  179.                       elseif (in_array(substr($number,0,3),array("021","025","027"))) {
  180.                           $regexp "(^02(1|5|7)[0-9]{3}[0-9]{4}$)"//Is Mobile number
  181.                       }
  182.                   break;
  183.                   case 11:
  184.                       if (substr($number,0,3== "640"{
  185.                           $regexp "(^640(3|4|6|7|9)[0-9]{7})";   // Is land line with country code
  186.                       }
  187.                   break;
  188.                   case 13:
  189.                       if (substr($number,0,4== "0064"{
  190.                           $regexp "(^00640(3|4|6|7|9)[0-9]{7})";     // Is land line with country code and 00
  191.                       }
  192.                   break;
  193.               }
  194.        }
  195.        
  196.        if ($regexp{
  197.            return preg_match($regexp,$number);
  198.        }
  199.  
  200.         return false;
  201.     }
  202.  
  203.  
  204.     /**
  205.      * Validates a New Zealand Bank Account Number
  206.      *
  207.      * This function checks wheather the given value
  208.      * is a valid New Zealand bank account number.
  209.      * allows several formats.
  210.      *
  211.      *
  212.      * @param     string     $Value number to validate
  213.      * @returns   bool
  214.      */
  215.     function bankCode($bankcode)
  216.     {
  217.         $bankcode str_replace(array("-"," ","."),'',trim($bankcode));
  218.         
  219.         if (ctype_digit($bankcode&& strlen($bankcode== 15{
  220.             return true;
  221.         }
  222.         
  223.         return false;
  224.     }
  225. }

Documentation generated on Mon, 11 Mar 2019 14:41:42 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.