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

Source for file CH.php

Documentation is available at CH.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Data validation class for Switzerland
  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_CH
  18.  * @author    Hans-Peter Oeri <hp@oeri.ch>
  19.  * @copyright 1997-2005 Hans-Peter Oeri
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  21.  * @version   CVS: $Id: CH.php,v 1.11 2008/11/06 00:47:00 clockwerx Exp $
  22.  * @link      http://pear.php.net/package/Validate_CH
  23.  */
  24.  
  25. /**
  26. * Requires base class Validate
  27. */
  28. require_once 'Validate.php';
  29.  
  30. /**
  31.  * Data validation class for Switzerland
  32.  *
  33.  * This class provides methods to validate:
  34.  *  - Social insurance number (aka SSN)
  35.  *  - Swiss university's immatriculation number
  36.  *  - Postal code
  37.  *
  38.  * @category  Validate
  39.  * @package   Validate_CH
  40.  * @author    Hans-Peter Oeri <hp@oeri.ch>
  41.  * @copyright 1997-2005 Hans-Peter Oeri
  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_CH
  45.  */
  46. {
  47.     /**
  48.     * Validate a Swiss social security number
  49.     *
  50.     * The Swiss social security numbers follow a very strict format.
  51.     * A check digit is the last one, computed the standard
  52.     * _get_control_number function.
  53.     *
  54.     * @param string $ssn ssn to validate
  55.     *
  56.     * @static
  57.     * @access   public
  58.     * @return   bool    true on success
  59.     */
  60.     function ssn($ssn)
  61.     {
  62.         $t_regex preg_match('/\d{3}\.\d{2}\.\d{3}\.\d{3}/'$ssn$matches);
  63.  
  64.         if (!$t_regex{
  65.             return false;
  66.         }
  67.  
  68.         // weight 0 to non digits -> ignored
  69.         $weights = array(5430270654032);
  70.  
  71.         // although theoretically, a check "digit" of 10 could result,
  72.         // no such ssn is issued! allow_high is therefore meaningless...
  73.         $t_check = Validate::_checkControlNumber($ssn$weights1111);
  74.  
  75.         return $t_check;
  76.     }
  77.  
  78.     /**
  79.     * Validate a Swiss university's immatriculation number
  80.     *
  81.     * Swiss immatriculation numbers are used by all universities
  82.     * in Switzerland. They are used in two primary formats: Official
  83.     * is a readable one with dashes, but commonly only the eight
  84.     * digits are just concatenated.
  85.     *
  86.     * As for check digit, a somewhat "weird" algorithm is used, in
  87.     * which not a sum of products, but the sum of the digits of products
  88.     * are taken.
  89.     *
  90.     * @param string $umn university's immatriculation number
  91.     *
  92.     * @static
  93.     * @access   public
  94.     * @return   bool    true on success
  95.     */
  96.     function studentid($umn)
  97.     {
  98.         // we accept both formats
  99.         $umn     preg_replace('/(\d{2})-(\d{3})-(\d{3})/''$1$2$3'$umn);
  100.         $t_regex preg_match('/\d{8}/'$umn);
  101.  
  102.         if (!$t_regex{
  103.             return false;
  104.         }
  105.  
  106.         // NOW, we have to go on ourselves, as not the products
  107.         // but the digits of the products are to be added!
  108.  
  109.         $weights = array(2121212);
  110.  
  111.         $sum = 0;
  112.  
  113.         for ($i = 0; $i <= 6; ++$i{
  114.             $tsum =  $umn{$i$weights[$i];
  115.             $sum += ($tsum > 9 ? $tsum - 9 : $tsum);
  116.         }
  117.  
  118.         $sum = 10 - $sum%10;
  119.  
  120.         return ($sum == $umn[7]);
  121.     }
  122.  
  123.     /**
  124.     * Validate a Swiss ZIP
  125.     *
  126.     * @param string $postcode postcode to validate
  127.     * @param bool   $strong   optional; strong checks (e.g. against a
  128.     *                          list of postcodes)
  129.     *
  130.     * @static
  131.     * @access   public
  132.     * @return   bool    true if postcode is ok, false otherwise
  133.     */
  134.     function postalCode($postcode$strong = false)
  135.     {
  136.         if ($strong{
  137.             static $postcodes;
  138.  
  139.             if (!isset($postcodes)) {
  140.                 $datadir dirname(dirname(__FILE__));
  141.                 
  142.                 $paths = array('@DATADIR@/Validate_CH/CH_postcodes.txt'
  143.                                $datadir '/data/CH_postcodes.txt');
  144.  
  145.                 foreach ($paths as $file{
  146.                     if (file_exists($file)) {
  147.                         $postcodes array_map('trim'file($file));
  148.                     }
  149.                 }
  150.             }
  151.  
  152.             return in_array($postcode$postcodes);
  153.         }
  154.         return (bool)ereg('^[0-9]{4}$'$postcode);
  155.     }
  156. }
  157. ?>

Documentation generated on Tue, 13 Jan 2009 12:30:06 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.