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

Source for file ZA.php

Documentation is available at ZA.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in South Africa
  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_ZA
  18.  * @author    Jacques Marneweck <jacques@php.net>
  19.  * @copyright 1997-2005 Jacques Marneweck
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  21.  * @version   CVS: $Id: ZA.php,v 1.6 2007/09/29 23:46:35 kguest Exp $
  22.  * @link      http://pear.php.net/package/Validate_ZA
  23.  */
  24.  
  25. /**
  26. * Requires base class Validate
  27. */
  28. require_once 'Validate.php';
  29.  
  30. /**
  31.  * Data validation class for South Africa
  32.  *
  33.  * This class provides methods to validate:
  34.  *  - Social insurance number (aka SSN)
  35.  *  - Province code
  36.  *  - Postal code
  37.  *
  38.  * @category  Validate
  39.  * @package   Validate_ZA
  40.  * @author    Jacques Marneweck <jacques@php.net>
  41.  * @copyright 1997-2005 Jacques Marneweck
  42.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  43.  * @version   Release: @package_version@
  44.  * @link      http://pear.php.net/package/Validate_ZA
  45.  */
  46. {
  47.     /**
  48.      * Validate a South African Postal Code
  49.      *
  50.      * I've downloaded a list of postal codes from the SAPO website and
  51.      * reduced the list down to unique postal codes.
  52.      *
  53.      * @param string $postcode the postal code to validate
  54.      * @param bool   $strong   optional; strong checks (e.g. against a list
  55.      *                          of postcodes)
  56.      *
  57.      * @return  bool    true if postal code is ok else false
  58.      * @static
  59.      * @access  public
  60.      * @link    http://www.sapo.co.za/cms/download/postcodes.zip
  61.      */
  62.     function postalCode($postcode$strong = false)
  63.     {
  64.         if (!is_numeric($postcode)) {
  65.             return false;
  66.         }
  67.  
  68.         if ($strong{
  69.             static $postcodes;
  70.  
  71.             if (!isset($postcodes)) {
  72.                 $file      '@DATADIR@/Validate_ZA/ZA_postcodes.txt';
  73.                 $postcodes array_map('trim'file($file));
  74.             }
  75.             return in_array((int)$postcode$postcodes);
  76.         }
  77.         return (bool)ereg('^[0-9]{4}$'$postcode);
  78.     }
  79.  
  80.     /**
  81.      * Validates a "region" (i.e. province) code
  82.      *
  83.      * @param string $region 2-letter province code
  84.      *
  85.      * @return  bool    true if valid else false
  86.      * @access  public
  87.      * @static
  88.      */
  89.     function region($region)
  90.     {
  91.         switch (strtoupper($region)) {
  92.         case 'EC'/* Eastern Cape */
  93.         case 'FS'/* Free State */
  94.         case 'GP'/* Gauteng */
  95.         case 'KN'/* Kwa-Zulu Natal */
  96.         case 'MP'/* Mpumalanga */
  97.         case 'NC'/* Northern Cape */
  98.         case 'NP'/* Limpopo (former Northern Province) */
  99.         case 'NW'/* North West */
  100.         case 'WC'/* Western Cape */
  101.             return true;
  102.             break;
  103.         default:
  104.             return false;
  105.         }
  106.         return (false);
  107.     }
  108.  
  109.     /**
  110.      * Validate a South African ID Number
  111.      *
  112.      * @param string $id 11 digit South African Identity Number
  113.      *
  114.      * @return  bool    true if valid else false
  115.      * @access  public
  116.      */
  117.     function ssn($id)
  118.     {
  119.         $match preg_match("!^(\d{2})(\d{2})(\d{2})[0|5]\d{6}$!"$id$matches);
  120.         if (!$match{
  121.             return false;
  122.         }
  123.  
  124.         list ($year$month$day$matches;
  125.  
  126.         /**
  127.          * Check that the date is valid
  128.          */
  129.         if (!Validate::date("$year-$month-$day"array('format' => '%y-%m-%d'))) {
  130.             return false;
  131.         }
  132.  
  133.         include_once 'Validate/Finance/CreditCard.php';
  134.  
  135.         if (Validate_Finance_CreditCard::Luhn($id)) {
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140. }
  141. ?>

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