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

Source for file IE.php

Documentation is available at IE.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Data validation class for Ireland
  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_IE
  18.  * @author    David Coallier <davidc@php.net>
  19.  * @copyright 1997-2008 Agora Production (http://agoraproduction.com)
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  21.  * @version   CVS: $Id: IE.php,v 1.26 2008/04/09 22:27:26 kguest Exp $
  22.  * @link      http://pear.php.net/package/Validate_IE
  23.  */
  24.  
  25. /**
  26.  * Data validation class for Ireland
  27.  *
  28.  * This class provides methods to validate:
  29.  *  - Postal code
  30.  *
  31.  * @category  Validate
  32.  * @package   Validate_IE
  33.  * @author    David Coallier <davidc@php.net>
  34.  * @author    Ken Guest      <ken@linux.ie>
  35.  * @copyright 1997-2008 Agora Production (http://agoraproduction.com)
  36.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  37.  * @version   Release: @package_version@
  38.  * @link      http://pear.php.net/package/Validate_IE
  39.  */
  40. {
  41.     // {{{ public function swift
  42.     /**
  43.      * Validate an Irish SWIFT code
  44.      *
  45.      * @param string $swift swift code
  46.      *
  47.      * @return bool   true if number is valid, false if not.
  48.      * @static
  49.      * @access public
  50.      */
  51.     function swift($swift)
  52.     {
  53.         return preg_match('/^[a-z0-9]{4}IE[a-z0-9]{2}$/i'$swift);
  54.     }
  55.     // }}}
  56.     // {{{ public function IBAN
  57.     /**
  58.      * Validate Irish IBAN
  59.      *
  60.      * @param string $iban  The account number to be validated
  61.      * @param string $swift swift code to compare against IBAN
  62.      *
  63.      * @return    bool 
  64.      * @access  public
  65.      */
  66.     function IBAN($iban$swift = false)
  67.     {
  68.         if ($swift{
  69.             $swift substr($swift04);
  70.             if (substr($iban44!= $swift{
  71.                 return false;
  72.             }
  73.         }
  74.  
  75.         if (substr($iban02== 'IE'{
  76.             include_once 'Validate/Finance/IBAN.php';
  77.             return Validate_Finance_IBAN::validate($iban);
  78.         }
  79.  
  80.         return false;
  81.     }
  82.     // }}}
  83.     // {{{ public function phoneNumber
  84.     /**
  85.      * Validate an irish phone number
  86.      *
  87.      * This function validates an irish phone number.
  88.      * You can either use the requiredAreaCode or not.
  89.      * by default this is set to true.
  90.      *
  91.      * @param string $number           The phone number
  92.      * @param bool   $requiredAreaCode defaults to true - to require area code checks
  93.      *
  94.      *  <code>
  95.      *  <?php
  96.      *  // Include the package
  97.      *  require_once('Validate/IE.php');
  98.      *
  99.      *  $phoneNumber = '+353 1 213 4567';
  100.      *  if ( Validate_IE::phoneNumber($phoneNumber) ) {
  101.      *      print 'Valid';
  102.      *  } else {
  103.      *      print 'Not valid!';
  104.      *  }
  105.      *  $phoneNumber = '213 4567';
  106.      *  if ( Validate_IE::phoneNumber($phoneNumber, false) ) {
  107.      *      print 'Valid';
  108.      *  } else {
  109.      *      print 'Not valid!';
  110.      *  }
  111.      *
  112.      *  ?>
  113.      *  </code>
  114.      *
  115.      * @access public
  116.      * @return bool   true if number is valid, false if not.
  117.      * @static
  118.      */
  119.     function phoneNumber($number$requiredAreaCode = true)
  120.     {
  121.         /*
  122.          * categorize prefixes into landline, mobile and 'other'
  123.          * each prefix has an associated regular expression.
  124.          * use defaultRegExp if associated entry is empty.
  125.          * @note Irish phone numbers are not the same as UK Phone numbers;
  126.          *       Irish phone numbers are much less complex as UK now has per
  127.          *       district phone numbers etc.
  128.          */
  129.         static $defaultRegExp '/^\d{7,10}$/';
  130.         static $irishLandLine = array(
  131.                 '1'=>'/^01\d{7}$/',
  132.                 '21'=>'''22'=>'''23'=>'''24'=>'''25'=>'''242'=>'',
  133.                 '225'=>'''26'=>'''27'=>'''28'=>'''29'=>'''402'=>'',
  134.                 '404'=>'''405'=>'''41'=>'''42'=>'''43'=>'''44'=>'',
  135.                 '45'=>'''46'=>'''47'=>'',
  136.                 '48'=>'/^048[0-9]{8}$/'//direct dial to Northern Ireland
  137.                 '49'=>'''51'=>'''52'=>'''53'=>'''54'=>'''55'=>'',
  138.                 '56'=>'''57'=>'',
  139.                 '58'=>'/^058[0-9]{5}$/',
  140.                 '59'=>'/^059[0-9]{7}$/',
  141.                 '502'=>'''504'=>'',
  142.                 '505'=>'/^0505[0-9]{5}$/',
  143.                 '506'=>'''509'=>'''61'=>'''62'=>'''63'=>'''64'=>'',
  144.                 '65'=>'''66'=>'''67'=>'''68'=>'''69'=>'''71'=>'',
  145.                 '74'=>'',
  146.                 '818'=>'/^0818[0-9]{6}$/',
  147.                 '90'=>'''91'=>'''92'=>'''93'=>'''94'=>'''95'=>'',
  148.                 '96'=>'''97'=>'''98'=>'''99'=>'');
  149.         static $irishMobileAreas = array('83'=>'/^083[0-9]{7}$/',
  150.                                            '85'=>'/^085[0-9]{7}$/',
  151.                                            '86'=>'/^086[0-9]{7}$/',
  152.                                            '87'=>'/^087[0-9]{7}$/',
  153.                                            '88'=>'/^088[0-9]{7}$/',
  154.                                            '89'=>'/^089[0-9]{7}$/');
  155.         static $irishMobileAreasVoiceMail = array('83'=>'/^0835[0-9]{7}$/',
  156.                                            '85'=>'/^0855[0-9]{7}$/',
  157.                                            '86'=>'/^0865[0-9]{7}$/',
  158.                                            '87'=>'/^0875[0-9]{7}$/',
  159.                                            '88'=>'/^0885[0-9]{7}$/',
  160.                                            '89'=>'/^0895[0-9]{7}$/');
  161.         static $irishOtherRates = array('1800'=>'/^1800[0-9]{6}$/',
  162.                                           '1850'=>'/^1850[0-9]{6}$/',
  163.                                           '1890'=>'/^1890[0-9]{6}$/');
  164.  
  165.         if (preg_match('/^00.*$/'$number)) {
  166.             $number '+' . substr($number2);
  167.         }
  168.         $number = str_replace(array('('')''-''+''.'' ')''$number);
  169.         //remove country code for Ireland and insert leading zero of area code.
  170.         //presence of area code is implied if country code is present.
  171.         if (strpos($number'353'=== 0{
  172.             $number "0" . substr($number3);
  173.         }
  174.  
  175.         if (strlen(trim($number)) <= 0{
  176.             return false;
  177.         }
  178.         if (!ctype_digit($number)) {
  179.             return false;
  180.         }
  181.         //area code must start with the standard 0 or a 1 for 'other rates'.
  182.         if (($requiredAreaCode&& !(preg_match("(^[01][0-9]*$)"$number))) {
  183.             return false;
  184.         }
  185.         //check special rate numbers
  186.         if (($requiredAreaCode&& (substr($number01== '1')) {
  187.             $prefix substr($number04);
  188.             if (isset($irishOtherRates[$prefix])) {
  189.                 $reg $irishOtherRates[$prefix];
  190.                 return (preg_match($reg$number));
  191.             else {
  192.                 return false;
  193.             }
  194.         }
  195.  
  196.         $len strlen($number);
  197.  
  198.         //if number has ten digits and a prefix it's likely a mobile phone
  199.         if (($requiredAreaCode&& ($len == 10)) {
  200.             $prefix substr($number12);
  201.             if (isset($irishMobileAreas[$prefix])) {
  202.                 $regexp $irishMobileAreas[$prefix];
  203.                 if (preg_match($regexp$number)) {
  204.                     return true;
  205.                 }
  206.             }
  207.         }
  208.         //see if it's a mobile phone with a 'direct to voicemail' prefix.
  209.         if (($requiredAreaCode&& ($len == 11)) {
  210.             $prefix substr($number12);
  211.             if (isset($irishMobileAreasVoiceMail[$prefix])) {
  212.                 $regexp $irishMobileAreasVoiceMail[$prefix];
  213.                 if (preg_match($regexp$number)) {
  214.                     return true;
  215.                 }
  216.             }
  217.         }
  218.  
  219.         if (!$requiredAreaCode{
  220.             //regular numbers, without an area code, don't start with a zero.
  221.             //they may be 5-8 digits long (depending on area code which can
  222.             //be 2-4 digits long...)
  223.             $preg "/^[1-9]\d{4,7}$/";
  224.             if (preg_match($preg$number)) {
  225.                 return true;
  226.             }
  227.         else {
  228.             $ret = false;
  229.             for ($i = 3; $i > 0; $i--{
  230.                 $prefix substr($number1$i);
  231.                 $preg   "";
  232.                 if (isset($irishLandLine[$prefix])) {
  233.                     $preg $irishLandLine[$prefix];
  234.                     if ($preg == ''{
  235.                         $preg $defaultRegExp;
  236.                     }
  237.                     if (preg_match($preg$number)) {
  238.                         $ret = true;
  239.                     }
  240.                     break;
  241.                 }
  242.             }
  243.             if ($ret{
  244.                 return true;
  245.             }
  246.         }
  247.  
  248.         return false;
  249.     }
  250.     // }}}
  251.     // {{{ public function postalcode
  252.     /**
  253.      * Validate postal code
  254.      *
  255.      * This function validates postal district codes in Dublin.
  256.      * It will be revised when national postal codes are rolled out.
  257.      *
  258.      * @param string $postalCode The postal code to validate
  259.      * @param string $dir        optional; /path/to/data/dir
  260.      *
  261.      * @access public
  262.      * @link   http://en.wikipedia.org/wiki/List_of_Dublin_postal_districts
  263.      * @return bool    true if postcode is ok, false otherwise
  264.      */
  265.     function postalCode($postalCode$dir = null)
  266.     {
  267.         $postalCode strtoupper(str_replace(' '''trim($postalCode)));
  268.         $postalCode str_replace('DUBLIN''D'$postalCode);
  269.  
  270.         if ($dir != null && (is_file($dir '/IE_postcodes.txt'))) {
  271.             $file $dir '/IE_postcodes.txt';
  272.         else {
  273.             $file '@DATADIR@/Validate_IE/data/IE_postcodes.txt';
  274.         }
  275.         $postcodes array_map('trim'file($file));
  276.         return in_array($postalCode$postcodes);
  277.     }
  278.     // }}}
  279.     // {{{ public function passport
  280.     /**
  281.      * Validate passport
  282.      *
  283.      * Validate an irish passport number.
  284.      *
  285.      * @param string $pp The passport number to validate.
  286.      *
  287.      * @access public
  288.      * @return bool   If the passport number is valid or not.
  289.      */
  290.     function passport($pp)
  291.     {
  292.         $pp   strtolower($pp);
  293.         $preg "/^[a-z]{2}[0-9]{7}$/";
  294.  
  295.         if (preg_match($preg$pp)) {
  296.             return true;
  297.         }
  298.  
  299.         return false;
  300.     }
  301.     // }}}
  302.     // {{{ public function drive
  303.     /**
  304.      * Validates an Irish driving licence
  305.      *
  306.      * This function will validate the number on an Irish driving licence.
  307.      *
  308.      * @param string $dl The drivers licence to validate
  309.      *
  310.      * @access    public
  311.      * @return    bool   true if it validates false if it doesn't.
  312.      */
  313.     function drive($dl)
  314.     {
  315.         $dl    str_replace(array(' ''-')''$dl);
  316.         $preg  "/^[0-9]{3}[0-9]{3}[0-9]{3}$/";
  317.         $match preg_match($preg$dl? true : false;
  318.  
  319.         return $match;
  320.     }
  321.     // }}}
  322.     // {{{ public function
  323.     /**
  324.      * Validate an Irish vehicle's license plate/registration number.
  325.      *
  326.      * @param string $number value to validate.
  327.      *
  328.      * @access public
  329.      * @return bool   true on success; else false.
  330.      */
  331.     function licensePlate($number)
  332.     {
  333.         //in_array is case sensitive, so use strtoupper...
  334.         $plate strtoupper($number);
  335.         $regex "/^\d{2}[\ -]([A-Z][A-Z]?)[\ -]\d{1,6}$/";
  336.  
  337.         if (preg_match($regex$plate$matches)) {
  338.             $mark strtoupper($matches[1]);
  339.             //check valid index mark
  340.             $marks = array('C','CE','CN','CW','D','DL','G','KE','KK','KY','L',
  341.                            'LD','LH','LK','LM','LS','MH','MN','MO','OY','RN',
  342.                            'SO','TN','TS','W','WD','WH','WX','WW');
  343.             return in_array($mark$marks);
  344.         else {
  345.             //two pre-1987 codes are still in use. ZZ and ZV.
  346.             //format is ZZ nnnnn - 5 digits for ZZ code and as few as 4 for ZV
  347.             $regex "/^ZZ[\ -]\d{5}$/";
  348.             if (preg_match($regex$plate)) {
  349.                 return true;
  350.             }
  351.             $regex "/^ZV[\ -]\d{4,5}$/";
  352.             if (preg_match($regex$plate)) {
  353.                 return true;
  354.             }
  355.             return false;
  356.         }
  357.     }
  358.     // }}}
  359.     // {{{ public function bankAC
  360.     /**
  361.      * Validate a bank account number
  362.      *
  363.      * This function will validate a bank account
  364.      * number for irish banks.
  365.      *
  366.      * @param string $ac     The account number
  367.      * @param string $noSort Don't validate the sort codes, optional (default: false)
  368.      *
  369.      * @access public
  370.      * @return bool                    true if the account validates
  371.      */
  372.     function bankAC($ac$noSort = false)
  373.     {
  374.         $ac   str_replace(array('-'' ')''$ac);
  375.         $preg "/^\d{14}$/";
  376.  
  377.         if ($noSort{
  378.             $preg "/^\d{8}$/";
  379.         }
  380.  
  381.         $returnValue preg_match($preg$ac? true : false;
  382.  
  383.         return $returnValue;
  384.     }
  385.     // }}}
  386.     // {{{ public function ssn
  387.     /**
  388.      * Validate SSN
  389.      *
  390.      * Ireland does not have a social security number system,
  391.      * the closest equivalent is a Personal Public Service Number.
  392.      *
  393.      * @param string $ssn ssn number to validate
  394.      *
  395.      * @link http://en.wikipedia.org/wiki/Personal_Public_Service_Number
  396.      * @access  public
  397.      * @see     Validate_IE::ppsn()
  398.      * @return  bool    Returns true on success, false otherwise
  399.      */
  400.     function ssn($ssn)
  401.     {
  402.         return Validate_IE::ppsn($ssn);
  403.     }
  404.     // }}}
  405.     // {{{ public function checkMOD23
  406.     /**
  407.      * Return true if the checksum in the specified PPSN or vat number, without
  408.      * the 'IE' prefix, is valid.
  409.      *
  410.      * @param string $value Value to perform modulus 23 checksum on.
  411.      *
  412.      * @access public
  413.      * @return boolean 
  414.      */
  415.     function checkMOD23($value)
  416.     {
  417.         $total = 0;
  418.         for ($i = 0;$i<7;++$i{
  419.             $total += (int)$value[$i]*(8-$i);
  420.         }
  421.         $mod ($total%23);
  422.         if ($mod === 0{
  423.             $mod = 23;
  424.         }
  425.         return (int) (chr(64+$mod== strtoupper($value[7]));
  426.  
  427.     }
  428.     // }}}
  429.     // {{{ public function ppsn
  430.     /**
  431.      * Personal Public Service Number
  432.      *
  433.      * Ireland does not have a social security number system,
  434.      * the closest equivalent is a Personal Public Service Number.
  435.      *
  436.      * @param string $ppsn Personal Public Service Number
  437.      *
  438.      * @access  public
  439.      * @return  bool    Returns true on success, false otherwise
  440.      * @link    http://en.wikipedia.org/wiki/Personal_Public_Service_Number
  441.      */
  442.     function ppsn($ppsn)
  443.     {
  444.         $preg "/^[0-9]{7}[A-Z]$/";
  445.  
  446.         if (preg_match($preg$ppsn)) {
  447.             //return (true);
  448.             return Validate_IE::checkMOD23($ppsn);
  449.         }
  450.  
  451.         $preg "/^[0-9]{7}[A-Z][\ WTX]?$/";
  452.         if (preg_match($preg$ppsn)) {
  453.             return Validate_IE::checkMOD23($ppsn);
  454.         else {
  455.             return false;
  456.         }
  457.     }
  458.     // }}}
  459.  
  460.     // {{{ public function vatNumber
  461.     /**
  462.      * Validate Irish VAT registration number.
  463.      *
  464.      * @param string $vat vat number to validate.
  465.      *
  466.      * @access  public
  467.      *
  468.      *  <code>
  469.      *  <?php
  470.      *  // Include the package
  471.      *  require_once('Validate/IE.php');
  472.      *
  473.      *  $vat = 'IE6335315A';
  474.      *  if ( Validate_IE::vatNumber($vat) ) {
  475.      *      print 'Valid';
  476.      *  } else {
  477.      *      print 'Not valid!';
  478.      *  }
  479.      *
  480.      *  ?>
  481.      *  </code>
  482.      *
  483.      * @return  bool            Returns true on success, false otherwise
  484.      * @link    http://www.iecomputersystems.com/ordering/eu_vat_numbers.htm
  485.      * @link    http://www.braemoor.co.uk/software/vat.shtml
  486.      */
  487.     function vatNumber($vat)
  488.     {
  489.         // IE1234567X or IE1X34567X are valid (includes one or two letters
  490.         // either the last or second + last).
  491.         if (preg_match('/^IE\d{7}[a-z]$/i'$vat)) {
  492.             return Validate_IE::checkMOD23(substr($vat2));
  493.         elseif (preg_match('/^IE\d[a-z]\d{5}[a-z]$/i'$vat)) {
  494.             $d   substr($vat2);
  495.             $new "0" substr($d25substr($d01substr($d71);
  496.             return Validate_IE::checkMOD23($new);
  497.         else {
  498.             return false;
  499.         }
  500.     }
  501.     // }}}
  502. }
  503. // }}}
  504. ?>

Documentation generated on Thu, 20 Nov 2008 16:30:04 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.