Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c4545586aa
Daniel O'Connor June 05, 2009
file 241 lines (228 sloc) 8.829 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 2007-2007 Bertrand Gugger |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSDlicense and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | pajoye@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Bertrand Gugger <toggg@php.net> |
// +----------------------------------------------------------------------+
//
/**
* A wrapper to call common local Validate methods
*
* @category Validate
* @package Validate_World
* @author Bertrand Gugger <bertrand@toggg.com>
* @copyright 2007-2007 Bertrand Gugger
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version CVS: $Id$
* @link http://pear.php.net/package/Validate_World
*/

/**
* The value returned when nation class or method does not exists
*/
if (!defined('VALIDATE_WORLD_DEFAULT')) {
    define('VALIDATE_WORLD_DEFAULT', 1);
}

/**
* Worldwide data validation class for installed locals
*
* This class provides methods to validate, if implemented by the local package:
* - Identity number
* - Social insurance number (ssn)
* - Postal code
* - Region
* - Phone number
*
* Any of these functions takes as first argument the nation code
* then the data to check and method/nation depending extra arguments
* (following each nation implementation)
*
* If nation Validate_XX package is installed and method is available
* methods return an actual boolean, else VALIDATE_WORLD_DEFAULT default to 1
* so, the check is weak by default, but 1 indicates it's this default
* false or true indicate that the check was actually run
*
* To set the check as bad if nation method is unavailable, define it to 0 as
* define('VALIDATE_WORLD_DEFAULT', 0); // please dont use false here
*
*
* @category PHP
* @package Validate
* @subpackage Validate_World
* @author Bertrand Gugger <bertrand@toggg.com>
* @copyright 2007-2007 Bertrand Gugger
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_World
*/
class Validate_World
{
    /**
* Validate a personal identity number
* Formerly mixed with ssn, social security number,
* but some nations make the distinction
*
* @param string $nationCode nation code
* @param string $pin national identity number to check
* @param mixed optionnal extra parameters depending on the implementation
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function pin($nationCode, $pin)
    {
        // some packages have national signifiant function names
        static $compat = array( 'BE' => 'nationalId',
                                'esMX' => 'dni',
                                'ES' => 'dni',
                                'FI' => 'pin',
                                'IE' => 'passport',
                                'LV' => 'personId',
                                'PL' => 'pesel');

        // we keep the behaviour to default to ssn
        $fun = isset($compat[$nationCode]) ? $compat[$nationCode] : 'ssn';
        return Validate_World::check($fun, func_get_args());
    }

    /**
* Validate a social insurance (or security) number
*
* @param string $nationCode nation code
* @param string $ssn national identity number to check
* @param mixed optionnal extra parameters depending on the implementation
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function ssn($nationCode, $ssn)
    {
        // some packages have national signifiant function names
        static $compat = array('esMX' => 'dni',
                                'ES' => 'dni',
                                'FI' => 'pin',
                                'IE' => 'ppsn',
                                'LV' => 'personId',
                                'PL' => 'pesel');

        $fun = isset($compat[$nationCode]) ? $compat[$nationCode] : 'ssn';
        return Validate_World::check($fun, func_get_args());
    }

    /**
* Validates a postal code format
*
* @param string $nationCode nation code
* @param string $postalCode postal code to check
* @param mixed optionnal extra parameters depending on the implementation
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function postalCode($nationCode, $postalCode)
    {
        return Validate_World::check('postalCode', func_get_args());
    }

    /**
* Validate a region code as US states, french departements, etc.
*
* @param string $nationCode nation code
* @param string $region region code to check
* @param mixed optionnal extra parameters depending on the implementation
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function region($nationCode, $region)
    {
        return Validate_World::check('region', func_get_args());
    }

    /**
* Validates a phone number
*
* @param string $nationCode nation code
* @param string $phoneNumber phone number to check
* @param mixed optionnal extra parameters depending on the implementation
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function phoneNumber($nationCode, $phoneNumber)
    {
        return Validate_World::check('phoneNumber', func_get_args());
    }

    /**
* Abstract method to call the national Validate methods
*
* @param string $what method to call
* @param array $args nation code followed by method's own arguments
* @return mixed bool if method available: data is valid
* number if method unavailable
* @access public
* @static
*/
    function check($what, $args)
    {
        static $nations = array();
        $nationCode = array_shift($args);
        if (!isset($nations[$nationCode])) {
            $nations[$nationCode] = is_readable(dirname(__FILE__) .
                                    DIRECTORY_SEPARATOR . $nationCode . '.php')
                                    && require_once 'Validate' . DIRECTORY_SEPARATOR . $nationCode . '.php';
        }
        
        if (!$nations[$nationCode] || !method_exists(
                $class = 'Validate' . '_' . $nationCode, $what))
        {
            return VALIDATE_WORLD_DEFAULT;
        }
        
        return (bool) call_user_func_array(array($class, $what), $args);
    }

    /**
* Lists available locales for Validate
*
* @param mixed string/array of strings $locale
* Optional searched locale code(s).
* If not present: all available locales.
*
* @return array The available locales (optionaly only the requested ones)
*
* @access public
* @static
*/
    function getLocales($locale = null)
    {
        static $last = '';
        static $ret = null;
        if ($locale === $last and isset($ret)) {
            return $ret;
        }
        $last = $locale;
        $ret = array();
        if (!empty($locale) && is_string($locale)) {
            $locale = array($locale);
        }
        $dh = opendir(dirname(__FILE__));
        if ($dh) {
            while ($fname = readdir($dh)) {
                if (preg_match('#^([A-Z]{2}[a-z_]*)\.php$#i', $fname, $matches)) {
                    if (is_file($dname . $fname) && is_readable($dname . $fname) &&
                        (empty($locale) || in_array($matches[1], $locale))) {
                        $ret[] = $matches[1];
                    }
                }
            }
            closedir($dh);
            sort($ret);
        }
        return $ret;
    }
}
Something went wrong with that request. Please try again.