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$
  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,3})[\ -]([A-Z][A-Z]?)[\ -]\d{1,6}$/";
  336.  
  337.         if (preg_match($regex$plate$matches)) {
  338.             $mark strtoupper($matches[2]);
  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.             if (in_array($mark$marks)) {
  344.                 // The first component, if 3 digits in length can only end
  345.                 // with a '1' or a '2'.
  346.                 if (strlen($matches[1]== 3{
  347.                     $end  = (int) substr($matches[1]21);
  348.                     return ($end == 1|| ($end == 2);
  349.                 }
  350.                 return true;
  351.             else {
  352.                 return false;
  353.             }
  354.         else {
  355.             //two pre-1987 codes are still in use. ZZ and ZV.
  356.             //format is ZZ nnnnn - 5 digits for ZZ code and as few as 4 for ZV
  357.             $regex "/^ZZ[\ -]\d{5}$/";
  358.             if (preg_match($regex$plate)) {
  359.                 return true;
  360.             }
  361.             $regex "/^ZV[\ -]\d{4,5}$/";
  362.             if (preg_match($regex$plate)) {
  363.                 return true;
  364.             }
  365.             return false;
  366.         }
  367.     }
  368.     // }}}
  369.  
  370.     // {{{ public function sortCode
  371.  
  372.     /**
  373.      * Validate a sort code, no dashes or whitespace - just digits.
  374.      *
  375.      * @param string $sc The sort code.
  376.      *
  377.      * @access public
  378.      * @return bool 
  379.      */
  380.     function sortCode($sc)
  381.     {
  382.         // 6 digits expected - starting with a '9'.
  383.         return (preg_match('/^9[0-9]{5}$/'$sc)) ? true : false;
  384.     }
  385.     // }}}
  386.     // {{{ public function bankAC
  387.     /**
  388.      * Validate a bank account number
  389.      *
  390.      * This function will validate a bank account
  391.      * number for irish banks.
  392.      *
  393.      * @param string $ac     The account number
  394.      * @param string $noSort Don't validate the sort codes, optional (default: false)
  395.      *
  396.      * @access public
  397.      * @return bool                    true if the account validates
  398.      */
  399.     function bankAC($ac$noSort = false)
  400.     {
  401.         $ac   str_replace(array('-'' ')''$ac);
  402.         $preg "/^\d{14}$/";
  403.  
  404.         if ($noSort{
  405.             $preg "/^\d{8}$/";
  406.         }
  407.  
  408.         $returnValue preg_match($preg$ac? true : false;
  409.  
  410.         return $returnValue;
  411.     }
  412.     // }}}
  413.     // {{{ public function ssn
  414.     /**
  415.      * Validate SSN
  416.      *
  417.      * Ireland does not have a social security number system,
  418.      * the closest equivalent is a Personal Public Service Number.
  419.      *
  420.      * @param string $ssn ssn number to validate
  421.      *
  422.      * @link http://en.wikipedia.org/wiki/Personal_Public_Service_Number
  423.      * @access  public
  424.      * @see     Validate_IE::ppsn()
  425.      * @return  bool    Returns true on success, false otherwise
  426.      */
  427.     function ssn($ssn)
  428.     {
  429.         return Validate_IE::ppsn($ssn);
  430.     }
  431.     // }}}
  432.     // {{{ public function checkMOD23
  433.     /**
  434.      * Return true if the checksum in the specified PPSN or vat number, without
  435.      * the 'IE' prefix, is valid.
  436.      *
  437.      * @param string $value Value to perform modulus 23 checksum on.
  438.      *
  439.      * @access public
  440.      * @return boolean 
  441.      */
  442.     function checkMOD23($value)
  443.     {
  444.         $total = 0;
  445.         for ($i = 0;$i<7;++$i{
  446.             $total += (int)$value[$i]*(8-$i);
  447.         }
  448.         $mod ($total%23);
  449.         if ($mod === 0{
  450.             $mod = 23;
  451.         }
  452.         return (int) (chr(64+$mod== strtoupper($value[7]));
  453.  
  454.     }
  455.     // }}}
  456.     // {{{ public function ppsn
  457.     /**
  458.      * Personal Public Service Number
  459.      *
  460.      * Ireland does not have a social security number system,
  461.      * the closest equivalent is a Personal Public Service Number.
  462.      *
  463.      * @param string $ppsn Personal Public Service Number
  464.      *
  465.      * @access  public
  466.      * @return  bool    Returns true on success, false otherwise
  467.      * @link    http://en.wikipedia.org/wiki/Personal_Public_Service_Number
  468.      */
  469.     function ppsn($ppsn)
  470.     {
  471.         $preg "/^[0-9]{7}[A-Z]$/";
  472.  
  473.         if (preg_match($preg$ppsn)) {
  474.             //return (true);
  475.             return Validate_IE::checkMOD23($ppsn);
  476.         }
  477.  
  478.         $preg "/^[0-9]{7}[A-Z][\ WTX]?$/";
  479.         if (preg_match($preg$ppsn)) {
  480.             return Validate_IE::checkMOD23($ppsn);
  481.         else {
  482.             return false;
  483.         }
  484.     }
  485.     // }}}
  486.  
  487.     // {{{ public function vatNumber
  488.     /**
  489.      * Validate Irish VAT registration number.
  490.      *
  491.      * @param string $vat vat number to validate.
  492.      *
  493.      * @access  public
  494.      *
  495.      *  <code>
  496.      *  <?php
  497.      *  // Include the package
  498.      *  require_once('Validate/IE.php');
  499.      *
  500.      *  $vat = 'IE6335315A';
  501.      *  if ( Validate_IE::vatNumber($vat) ) {
  502.      *      print 'Valid';
  503.      *  } else {
  504.      *      print 'Not valid!';
  505.      *  }
  506.      *
  507.      *  ?>
  508.      *  </code>
  509.      *
  510.      * @return  bool            Returns true on success, false otherwise
  511.      * @link    http://www.iecomputersystems.com/ordering/eu_vat_numbers.htm
  512.      * @link    http://www.braemoor.co.uk/software/vat.shtml
  513.      */
  514.     function vatNumber($vat)
  515.     {
  516.         // IE1234567X or IE1X34567X are valid (includes one or two letters
  517.         // either the last or second + last).
  518.         if (preg_match('/^IE\d{7}[a-z]$/i'$vat)) {
  519.             return Validate_IE::checkMOD23(substr($vat2));
  520.         elseif (preg_match('/^IE\d[a-z]\d{5}[a-z]$/i'$vat)) {
  521.             $d   substr($vat2);
  522.             $new "0" substr($d25substr($d01substr($d71);
  523.             return Validate_IE::checkMOD23($new);
  524.         }
  525.         return false;
  526.     }
  527.     // }}}
  528. }
  529. // }}}
  530. ?>

Documentation generated on Tue, 22 Jan 2013 14:30:02 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.