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

Source for file PL.php

Documentation is available at PL.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in Poland
  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_PL
  18.  * @author    Piotr Klaban <makler@man.torun.pl>
  19.  * @copyright 1997-2005 Piotr Klaban
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  new BSD
  21.  * @version   CVS: $Id: PL.php,v 1.13 2007/09/30 16:31:46 kguest Exp $
  22.  * @link      http://pear.php.net/package/Validate_PL
  23.  */
  24.  
  25. /**
  26. * Requires base class Validate
  27. */
  28. require_once 'Validate.php';
  29.  
  30. /**
  31.  * Data validation class for Poland
  32.  *
  33.  * This class provides methods to validate:
  34.  *  - NIP (Polish tax identification number)
  35.  *  - Bank account number
  36.  *  - PESEL (Polish human identification number)
  37.  *  - REGON (Polish statistical national economy register)
  38.  *
  39.  * @category  Validate
  40.  * @package   Validate_PL
  41.  * @author    Piotr Klaban <makler@man.torun.pl>
  42.  * @copyright 1997-2005 Piotr Klaban
  43.  * @license   http://www.opensource.org/licenses/bsd-license.php  new BSD
  44.  * @version   Release: @package_version@
  45.  * @link      http://pear.php.net/package/Validate_PL
  46.  */
  47. {
  48.     /**
  49.      * Validates NIP (Polish tax identification number)
  50.      *
  51.      * Sprawdza NIP (Numer Identyfikacji Podatkowej)
  52.      * http://chemeng.p.lodz.pl/zylla/ut/nip-rego.html
  53.      *
  54.      * @param string $nip 9-digit number to validate
  55.      *
  56.      * @return bool 
  57.      */
  58.     function nip($nip)
  59.     {
  60.         static $weights_nip = array(6,5,7,2,3,4,5,6,7);
  61.  
  62.         // remove any dashes, spaces, returns, tabs or slashes
  63.         $nip = str_replace(array('-','/',' ',"\t","\n")''$nip);
  64.  
  65.         // check if this is a 10-digit number
  66.         if (!is_numeric($nip|| strlen($nip!= 10{
  67.             return false;
  68.         }
  69.  
  70.         // check control sum
  71.         return Validate::_checkControlNumber($nip$weights_nip11);
  72.     }
  73.  
  74.     /**
  75.      * Validates bank number (Polish banks)
  76.      *
  77.      * @param string $number 8-digit number to validate
  78.      *
  79.      * @return bool 
  80.      */
  81.     function bankBranch($number)
  82.     {
  83.         static $weights_bank_branch = array(7,1,3,9,7,11,3);
  84.  
  85.         // remove any dashes, spaces, returns, tabs or slashes
  86.         $number = str_replace(array('-','/',' ',"\t","\n")''$number);
  87.  
  88.         // check if this is a 8-digit number
  89.         if (!is_numeric($number|| strlen($number!= 8{
  90.             return false;
  91.         }
  92.  
  93.         // check control sum
  94.         return Validate::_checkControlNumber($number$weights_bank_branch10);
  95.     }
  96.  
  97.     /**
  98.      * Validates PESEL (Polish human identification number)
  99.      *
  100.      * Sprawdza PESEL (Powszechny Elektroniczny System Ewidencji Ludnoci)
  101.      * http://www.mswia.gov.pl/crp_pesel.html
  102.      * NOTE: some people can have the same PESEL, and some can have
  103.      * PESEL with wrong numbers in place of birth date.
  104.      *
  105.      * @param string $pesel  11-digit number to validate
  106.      * @param array  &$birth reference to array - returns birth date and sex
  107.      *                (either 'female' or 'male' string) extracted from pesel
  108.      *
  109.      * @return bool 
  110.      */
  111.     function pesel($pesel&$birth)
  112.     {
  113.         static $weights_pesel = array(1,3,7,9,1,3,7,9,1,3);
  114.  
  115.         $birth = array(false,false);
  116.  
  117.         // remove any dashes, spaces, returns, tabs or slashes
  118.         $pesel = str_replace(array('-','/',' ',"\t","\n")''$pesel);
  119.  
  120.         // check if this is a 11-digit number
  121.         if (!is_numeric($pesel|| strlen($pesel!= 11{
  122.             return false;
  123.         }
  124.  
  125.         if (Validate::_checkControlNumber($pesel,
  126.                                           $weights_pesel,
  127.                                           10,
  128.                                           10=== false{
  129.             return false;
  130.         }
  131.  
  132.         // now extract birth date from PESEL number
  133.         $vy substr($pesel02);
  134.         $vm substr($pesel22);
  135.         $vd substr($pesel42);
  136.  
  137.         // decode century
  138.         if ($vm < 20{
  139.             $vy += 1900;
  140.         elseif ($vm < 40{
  141.             $vy += 2000;
  142.         elseif ($vm < 60{
  143.             $vy += 2100;
  144.         elseif ($vm < 80{
  145.             $vy += 2200;
  146.         else {
  147.             $vy += 1800;
  148.         }
  149.         $vm      %= 20;
  150.         $birth[0= "$vy-$vm-$vd";
  151.  
  152.         // decode gender
  153.         $gender   substr($pesel91% 2;
  154.         $birth[1($gender % 2 == 0'female' 'male';
  155.  
  156.         return true;
  157.     }
  158.  
  159.     /**
  160.      * Validates REGON (Polish statistical national economy register)
  161.      *
  162.      * Sprawdza REGON (Rejestr Gospodarki Narodowej)
  163.      * http://chemeng.p.lodz.pl/zylla/ut/nip-rego.html
  164.      *
  165.      * @param string $regon 9- or 14- digit number to validate
  166.      *
  167.      * @return bool 
  168.      */
  169.     function regon($regon)
  170.     {
  171.         return Validate_PL::region($regon);
  172.     }
  173.  
  174.     /**
  175.      * Validates REGON (Polish statistical national economy register)
  176.      *
  177.      * Sprawdza REGON (Rejestr Gospodarki Narodowej)
  178.      * http://chemeng.p.lodz.pl/zylla/ut/nip-rego.html
  179.      *
  180.      * @param string $regon 9- or 14- digit number to validate
  181.      *
  182.      * @return bool 
  183.      */
  184.     function region($regon)
  185.     {
  186.         static $weights_regon = array(8,9,2,3,4,5,6,7);
  187.         static $weights_regon_local = array(2,4,8,5,0,9,7,3,6,1,2,4,8);
  188.  
  189.         // remove any dashes, spaces, returns, tabs or slashes
  190.         $regon = str_replace(array('-','/',' ',"\t","\n")''$regon);
  191.  
  192.         // check if this is a 9- or 14-digit number
  193.         if (!is_numeric($regon|| (strlen($regon!= 9 && strlen($regon!= 14)) {
  194.             return false;
  195.         }
  196.  
  197.         // first check first 9 digits
  198.         if (Validate::_checkControlNumber($regon$weights_regon11=== false{
  199.             return false;
  200.         }
  201.  
  202.         // check wide number if there are 14 digits
  203.         if (strlen($regon== 14{
  204.             // check 14 digits
  205.             return Validate::_checkControlNumber($regon$weights_regon_local11);
  206.         }
  207.  
  208.         return true;
  209.     }
  210. }
  211. ?>

Documentation generated on Mon, 23 Mar 2009 00:30:03 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.