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

Source for file AU.php

Documentation is available at AU.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in Australia
  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_AU
  18.  * @author    Daniel O'Connor <daniel.oconnor@gmail.com>
  19.  * @author    Tho Nguyen <tho.nguyen@itexperts.com.au>
  20.  * @author    Alex Hayes <ahayes@wcg.net.au>
  21.  * @author    Byron Adams <byron.adams54@gmail.com>
  22.  * @copyright 1997-2005 Daniel O'Connor
  23.  * @copyright 2006 Alex Hayes
  24.  * @copyright 2006 Byron Adams
  25.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  26.  * @version   CVS: $Id: AU.php,v 1.18 2008/10/26 17:53:50 clockwerx Exp $
  27.  * @link      http://pear.php.net/package/Validate_AU
  28.  */
  29.  
  30. /**
  31.  * Data validation class for Australia
  32.  *
  33.  * Contains code from Validate_AT, Validate_UK and Validate_NZ
  34.  *
  35.  * This class provides methods to validate:
  36.  *  - Postal code
  37.  *  - Phone number
  38.  *  - Australian Business Number
  39.  *  - Australian Company Number
  40.  *  - Tax File Number
  41.  *  - Australian Regional codes
  42.  *
  43.  * @category  Validate
  44.  * @package   Validate_AU
  45.  * @author    Daniel O'Connor <daniel.oconnor@gmail.com>
  46.  * @author    Tho Nguyen <tho.nguyen@itexperts.com.au>
  47.  * @author    Alex Hayes <ahayes@wcg.net.au>
  48.  * @author    Byron Adams <byron.adams54@gmail.com>
  49.  * @copyright 1997-2005 Daniel O'Connor
  50.  * @copyright 2006 Alex Hayes
  51.  * @copyright 2006 Byron Adams
  52.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  53.  * @version   Release: @package_version@
  54.  * @link      http://pear.php.net/package/Validate_AU
  55.  */
  56.  
  57. {
  58.  
  59.     /**
  60.      * Validate Austrialian postal codes.
  61.      *
  62.      * @param string $postcode postcode to validate
  63.      * @param bool   $strong   optional; strong checks against a list of postcodes
  64.      * @param string $dataDir  optional; name of directory datafile is located in
  65.      *
  66.      * @access   public
  67.      * @static   string  $postcodes
  68.      * @return   bool    true if postcode is ok, false otherwise
  69.      */
  70.     function postalCode($postcode$strong = false$dataDir = null)
  71.     {
  72.         if ($strong{
  73.             static $postcodes;
  74.  
  75.             if (!isset($postcodes)) {
  76.                 if ($dataDir != null && (is_file($dataDir '/AU_postcodes.txt'))) {
  77.                     $file $dataDir '/AU_postcodes.txt';
  78.                 else {
  79.                     $file '@DATADIR@/Validate_AU/data/AU_postcodes.txt';
  80.                 }
  81.                 $postcodes array_map('trim'file($file));
  82.             }
  83.  
  84.             return in_array((string)$postcode$postcodestrue);
  85.         }
  86.         return preg_match('(^[0-9]{4}$)'$postcode);
  87.     }
  88.  
  89.     /**
  90.      * Validates Australian Regional Codes
  91.      *
  92.      * @param string $region regional code to validate
  93.      *
  94.      * @access    public
  95.      * @static    array      $regions
  96.      * @return    bool       Returns true on success, false otherwise
  97.      */
  98.     function region($region)
  99.     {
  100.         static $regions = array("ACT""NSW""NT""QLD""SA""TAS""VIC""WA");
  101.         return in_array(strtoupper($region)$regions);
  102.     }
  103.  
  104.     /**
  105.      * Validate a telephone number.
  106.      *
  107.      * Note that this function supports the following notations:
  108.      *
  109.      *     - Landline: 03 9999 9999
  110.      *     - Mobile: 0400 000 000 (as above, but usually notated differently)
  111.      *     - Indial: 131 812 / 1300 000 000 / 1800 000 000 / 1900 000 000
  112.      *     - International: +61.3 9999 9999
  113.      *
  114.      * For International numbers, only +61 will be valid, as this is
  115.      * Australia's dial code, and the format MUST be +61.3, where 3 represents
  116.      * the state dial code, in this case, Victoria.
  117.      *
  118.      * Note: If the VALIDATE_AU_PHONENUMBER_STRICT flag is not supplied, then
  119.      * all spaces, dashes and parenthesis are removed before validation. You
  120.      * will have to strip these yourself if your data storage does not allow
  121.      * these characters.
  122.      *
  123.      * @param string  $number  The telephone number
  124.      * @param mixed[] $options A list of options
  125.      *                           'strict'   => true - do not common characters
  126.      *                           'national' => true - validate national numbers
  127.      *                           'indial'   => true - 13, 1300, 1800, 1900
  128.      *                                   numbers
  129.      *                           'other'    => true - uncommon phone validations
  130.      *                                   like premium sms, data and personal numbers
  131.      *                           'international => true - international numbers
  132.      *                                   for Australia (eg. +61.3 9999 9999)
  133.      *
  134.      * @static
  135.      * @access    public
  136.      * @return    bool 
  137.      *
  138.      * @todo Check that $flags contains a valid flag.
  139.      */
  140.     function phoneNumber($number$options = array('strict'        => false,
  141.                                                    'national'      => true,
  142.                                                    'indial'        => true,
  143.                                                    'international' => true,
  144.                                                    'other'         => true))
  145.     {
  146.  
  147.         $preg = array();
  148.         if (empty($options['strict'])) {
  149.             $number str_replace(array('('')''-'' ')''$number);
  150.         }
  151.  
  152.         if (!empty($options['national'])) {
  153.              $preg["(0[3478][0-9]{8})";
  154.              $preg["(02[3-9][0-9]{7})";
  155.         }
  156.  
  157.         if (!empty($options['indial'])) {
  158.             $preg['(13[0-9]{4})';
  159.             $preg["(1[3|8|9]00[0-9]{6})";
  160.         }
  161.  
  162.         if (!empty($options['international'])) {
  163.              $preg["(\+61\.[23478][0-9]{8})";
  164.         }
  165.  
  166.         //Other numbers, like premium SMS
  167.         if (!empty($options['other'])) {
  168.  
  169.             //Premium SMS
  170.             $preg["(19[0-9]{4,6})";
  171.  
  172.             //Universial Personal Phones
  173.             $preg["(0550[0-9]{6})"//VOIP range (proposed)
  174.             $preg["(059[0-9]{7})";  //Enum testing numbers
  175.             $preg["(0500[0-9]{6})"//"Find me anywhere"
  176.                                         //(divert the number and
  177.                                         // the caller pays the bill)
  178.  
  179.  
  180.  
  181.             //Data access providers
  182.             $preg["(0198[0-3][0-9]{5})";
  183.  
  184.         }
  185.  
  186.         if (!empty($preg)) {
  187.             foreach ($preg as $pattern{
  188.                 if (preg_match("/^" $pattern "$/"$number)) {
  189.                     return true;
  190.                 }
  191.             }
  192.         }
  193.  
  194.         return false;
  195.  
  196.     }
  197.  
  198.     /**
  199.      * Validate an Australian Company Number (ACN)
  200.      *
  201.      * The ACN is a nine digit number with the last digit
  202.      * being a check digit calculated using a modified
  203.      * modulus 10 calculation.
  204.      *
  205.      * @param string $acn ACN number to validate
  206.      *
  207.      * @access public
  208.      * @return bool Returns true on success, false otherwise
  209.      * @link   http://www.asic.gov.au/asic/asic_infoco.nsf/byheadline/Australian+Company+Number+(ACN)+Check+Digit
  210.      */
  211.     function acn($acn)
  212.     {
  213.         $weights = array(876543210);
  214.  
  215.         $acn    preg_replace("/[^\d]/"""$acn);
  216.         $digits str_split($acn);
  217.         $sum    = 0;
  218.  
  219.         if (!ctype_digit($acn|| strlen($acn!= 9{
  220.             return false;
  221.         }
  222.  
  223.         foreach ($digits as $key => $digit{
  224.             $sum += $digit $weights[$key];
  225.         }
  226.  
  227.         $remainder $sum % 10;
  228.  
  229.         switch ($remainder{
  230.         case 0:
  231.             $complement = 0 - $remainder;
  232.             break;
  233.         default:
  234.             $complement = 10 - $remainder;
  235.             break;
  236.         }
  237.  
  238.         return ($digits[8== $complement);
  239.     }
  240.  
  241.     /**
  242.      * Social Security Number.
  243.      *
  244.      * Australia does not have a social security number system,
  245.      * the closest equivalent is a Tax File Number
  246.      *
  247.      * @param string $ssn ssn number to validate
  248.      *
  249.      * @access  public
  250.      * @see     Validate_AU::tfn()
  251.      * @return  bool    Returns true on success, false otherwise
  252.      */
  253.     function ssn($ssn)
  254.     {
  255.         return Validate_AU::tfn($ssn);
  256.     }
  257.  
  258.     /**
  259.      * Tax File Number (TFN)
  260.      *
  261.      * Australia does not have a social security number system,
  262.      * the closest equivalent is a Tax File Number.
  263.      *
  264.      * @param string $tfn Tax File Number
  265.      *
  266.      * @access  public
  267.      * @return  bool    Returns true on success, false otherwise
  268.      * @link    http://en.wikipedia.org/wiki/Tax_File_Number
  269.      */
  270.     function tfn($tfn)
  271.     {
  272.         $weights = array(1437586910);
  273.         $length  = array("8""9");
  274.  
  275.         $tfn preg_replace("/[^\d]/"""$tfn);
  276.         $tfn str_split($tfn);
  277.  
  278.         return Validate_AU::checkDigit($tfn11$weights$length);
  279.     }
  280.  
  281.     /**
  282.      * Australian Business Number (ABN).
  283.      *
  284.      * Validates an ABN using a modulus calculation
  285.      *
  286.      * @param string $abn ABN to validate
  287.      *
  288.      * @static
  289.      * @access  public
  290.      * @return  bool      true on success, otherwise false
  291.      * @link    http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm
  292.      */
  293.     function abn($abn)
  294.     {
  295.         $weights = array(10135791113151719);
  296.         $length  = array("11");
  297.  
  298.         $abn preg_replace("/[^\d]/"""$abn);
  299.         $abn str_split($abn);
  300.         $abn[0]--;
  301.  
  302.         return Validate_AU::checkDigit($abn89$weights$length);
  303.     }
  304.  
  305.     /**
  306.      * Validate number against decimal checksum (check digit)
  307.      *
  308.      * A check digit is a form of redundancy check used
  309.      * for error detection, the decimal equivalent of a
  310.      * binary checksum. It consists of a single digit
  311.      * computed from the other digits in the message.
  312.      *
  313.      * @param array $digits  Digits to check
  314.      * @param int   $modulus Modulus
  315.      * @param array $weights Array containing weighting
  316.      * @param array $length  Length
  317.      *
  318.      * @access public
  319.      * @return bool     true on success, otherwise false
  320.      * @link   http://en.wikipedia.org/wiki/Check_digit
  321.      */
  322.     function checkDigit($digits$modulus$weights$length)
  323.     {
  324.         $sum = 0;
  325.  
  326.         if (!in_array(count($digits)$length)) {
  327.             return false;
  328.         }
  329.  
  330.         foreach ($digits as $key => $digit{
  331.             $sum += $digit $weights[$key];
  332.         }
  333.  
  334.         return !($sum $modulus);
  335.  
  336.     }
  337. }
  338. ?>

Documentation generated on Tue, 30 Dec 2008 06:30:03 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.