Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Split Validate/Validate FR

  • Loading branch information...
commit d008b871be00f2fb8bf4b7ea3ad513f3a1070be7 1 parent 7d6b0a0
@CloCkWeRX CloCkWeRX authored
Showing with 0 additions and 652 deletions.
  1. +0 −408 Validate/FR.php
  2. +0 −31 tests/bug7389.phpt
  3. +0 −213 tests/validate_FR.phpt
View
408 Validate/FR.php
@@ -1,408 +0,0 @@
-<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-/**
- * Specific validation methods for data used in France
- *
- * PHP Versions 4 and 5
- *
- * 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.
- *
- * @category Validate
- * @package Validate_FR
- * @author Pierre-Alain Joye <pajoye@php.net>
- * @author Bertrand Gugger <bertrand@toggg.com>
- * @copyright 1997-2005 Pierre-Alain Joye, Bertrand Gugger
- * @license http://www.opensource.org/licenses/bsd-license.php new BSD
- * @version CVS: $Id$
- * @link http://pear.php.net/package/Validate_FR
- */
-
-/**
-* Requires base class Validate
-*/
-require_once 'Validate.php';
-
-define('VALIDATE_FR_SSN_MODULUS', 97);
-
-/**
- * Data validation class for France
- *
- * This class provides methods to validate:
- * - Social insurance number (aka SSN)
- * - French RIB
- * - French SIREN number
- * - French SIRET number
- * - Postal code
- * - French "departement"
- *
- * @category PHP
- * @package Validate
- * @subpackage Validate_FR
- * @author Pierre-Alain Joye <pajoye@php.net>
- * @author Bertrand Gugger <bertrand@toggg.com>
- * @copyright 1997-2005 Pierre-Alain Joye, Bertrand Gugger
- * @license http://www.opensource.org/licenses/bsd-license.php new BSD
- * @version Release: @package_version@
- * @link http://pear.php.net/package/Validate_FR
- */
-class Validate_FR
-{
- /**
- * Validate a french SSN
- *
- * TO DO :
- * strong validation against the INSEE cities databases (rpc or local DB)
- *
- * This function checks given number according the specs
- * available here:
- * http://www.dads.cnav.fr/tds/Stru0103.htm
- * http://xml.insee.fr/schema/nir.html
- *
- * @param string $ssn number or an array containing the 'number'=>1234
- *
- * @return bool true if number is valid
- */
- function ssn($ssn)
- {
- $str = strtolower(preg_replace('/[^0-9a-zA-Z]/', '', $ssn));
-
- $regexp = "/^([12])(\d\d)(\d\d)(\d\d|2a|2b)(\d\d\d)(\d\d\d)(\d\d)$/";
-
- if (!preg_match($regexp, $str, $parts)) {
- return false;
- }
-
- // special case for Corsica and DOM not 100% sure, but cannot test
- // from a db :)
- if ($parts[4] == '2a' || $parts[4] == '2b' || $parts[4] == '9a' ||
- $parts[4] == '9b' || $parts[4] == '9c' || $parts[4] == '9d') {
- if (strlen($str) == 15) {
- return true;
- } else {
- return false;
- }
- }
-
- if ($parts[2] < 1 || $parts[2] > 99) {
- return false;
- }
- if ($parts[3] < 1 || $parts[3] > 12) {
- return false;
- }
- if ($parts[4] < 1 || $parts[4] > 99) {
- return false;
- }
-
- // Person born outside France (region code==99), have to check insee
- // country code
- // To do
- /*
- if($parts[4]==99){
- include 'FR_insee_country_codes.php';
- if ( !isset( $insee_country[$parts[5]] ) ){
- return false;
- }
- }
- */
- $num = $parts[1].$parts[2].$parts[3].$parts[4].$parts[5].$parts[6];
- $key = VALIDATE_FR_SSN_MODULUS -
- Validate::_modf($num, VALIDATE_FR_SSN_MODULUS);
-
- return ($key == $parts[7]) ? true : false;
- }
-
- /**
- * Validate a french RIB
- * see http://www.ecbs.org/Download/Tr201v3.9.pdf
- *
- * @param array $rib number or an associative array:
- * "aCodeBanque" => 1234
- * "aCodeGuichet" => 1234
- * "aNoCompte" => 1234
- *
- * @return bool true if number is valid, otherwise false
- */
- function rib($rib)
- {
- if (is_array($rib)) {
- $codebanque = $codeguichet = $nocompte = $key = '';
- extract($rib);
- } else {
- return false;
- }
- $chars = array('/[AJ]/','/[BKS]/','/[CLT]/','/[DMU]/','/[ENV]/',
- '/[FOW]/','/[GPX]/','/[HQY]/','/[IRZ]/');
- $values = array('1','2','3','4','5','6','7','8','9');
-
- $codebank = preg_replace('/[^0-9]/', '', $codebanque);
- $officecode = preg_replace('/[^0-9]/', '', $codeguichet);
- $account = preg_replace($chars, $values, $nocompte);
-
- if (strlen($codebank) != 5) {
- return false;
- }
-
- if (strlen($officecode) != 5) {
- return false;
- }
-
- if (strlen($account) > 11) {
- return false;
- }
-
- if (strlen($key) != 2) {
- return false;
- }
-
- $padded = str_pad($account, 11, '0', STR_PAD_LEFT);
- $l = $codebank . $officecode . $padded . $key . '0';
- $keyChk = 0;
- for ($i = 0; $i < 24; $i += 4) {
- $keyChk = ($keyChk*9 + substr($l, $i, 4)) % 97;
- }
- return !$keyChk;
- }
-
-
- /**
- * Validate a french SIREN number
- * see http://xml.insee.fr/schema/siret.html
- *
- * @param string $siren number or an array containaing the 'number'=>1234
- *
- * @return bool true if number is valid, otherwise false
- */
- function siren($siren)
- {
- $siren = str_replace(' ', '', $siren);
- $reg = "/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/";
- if (!preg_match($reg, $siren, $match)) {
- return false;
- }
- $match[2] *= 2;
- $match[4] *= 2;
- $match[6] *= 2;
- $match[8] *= 2;
- $sum = 0;
-
- for ($i = 1; $i < count($match); $i++) {
- if ($match[$i] > 9) {
- $a = (int) substr($match[$i], 0, 1);
- $b = (int) substr($match[$i], 1, 1);
- $match[$i] = $a + $b;
- }
- $sum += $match[$i];
- }
- return (($sum % 10) == 0);
- }
-
- /**
- * Validate a french SIRET number
- * see http://xml.insee.fr/schema/siret.html
- *
- * @param string $siret number or an array containaing the 'number'=>1234
- *
- * @return bool true if number is valid, otherwise false
- */
- function siret($siret)
- {
- $siret = str_replace(' ', '', $siret);
- $reg = "/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/";
- if (!preg_match($reg, $siret, $match)) {
- return false;
- } else {
- if (!Validate_FR::siren(implode('', array_slice($match, 1, 9)))) {
- return false;
- }
- }
- $match[1] *= 2;
- $match[3] *= 2;
- $match[5] *= 2;
- $match[7] *= 2;
- $match[9] *= 2;
- $match[11] *= 2;
- $match[13] *= 2;
- $sum = 0;
-
- for ($i = 1; $i < count($match); $i++) {
- if ($match[$i] > 9) {
- $a = (int) substr($match[$i], 0, 1);
- $b = (int) substr($match[$i], 1, 1);
- $match[$i] = $a + $b;
- }
- $sum += $match[$i];
- }
- return (($sum % 10) == 0);
- }
-
- /**
- * Validates a French Postal Code format
- *
- * @param string $postalCode the code to validate
- * @param bool $strong optional; strong checks (e.g. against a list
- * of postcodes) (not implanted)
- *
- * @return bool true if code is valid, false otherwise
- * @access public
- * @static
- * @todo Validate against department
- */
- function postalCode($postalCode, $strong = false)
- {
- return (bool) preg_match('/^(0[1-9]|[1-9][0-9])[0-9][0-9][0-9]$/',
- $postalCode);
- }
-
- /**
- * Name of a French "departement"
- *
- * @param string $departement 2 (2A or 2B included) or 3 -digit department number
- * or none to get the list
- *
- * @return mixed The department's name, special chars in numeric entities or ''
- * No argument, returns the array of known code => departements
- * @static
- */
- function nameDepartement($departement = null)
- {
- static $return = array(
- '01' => 'Ain',
- '02' => 'Aisne',
- '03' => 'Allier',
- '04' => 'Alpes-de-Haute-Provence',
- '05' => 'Hautes-Alpes',
- '06' => 'Alpes-Maritimes',
- '07' => 'Ard&#232;che',
- '08' => 'Ardennes',
- '09' => 'Ari&#232;ge',
- '10' => 'Aube',
- '11' => 'Aude',
- '12' => 'Aveyron',
- '13' => 'Bouches-du-Rh&#244;ne',
- '14' => 'Calvados',
- '15' => 'Cantal',
- '16' => 'Charente',
- '17' => 'Charente-Maritime',
- '18' => 'Cher',
- '19' => 'Corr&#232;ze',
- //'20' => 'Corse', // deprecated
- '21' => 'C&#244;te-d\'Or',
- '22' => 'C&#244;tes d\'Armor',
- '23' => 'Creuse',
- '24' => 'Dordogne',
- '25' => 'Doubs',
- '26' => 'Dr&#244;me',
- '27' => 'Eure',
- '28' => 'Eure-et-Loir',
- '29' => 'Finist&#232;re',
- '2A' => 'Corse-du-Sud',
- '2B' => 'Haute-Corse',
- '30' => 'Gard',
- '31' => 'Haute-Garonne',
- '32' => 'Gers',
- '33' => 'Gironde',
- '34' => 'H&#233;rault',
- '35' => 'Ille-et-Vilaine',
- '36' => 'Indre',
- '37' => 'Indre-et-Loire',
- '38' => 'Is&#232;re',
- '39' => 'Jura',
- '40' => 'Landes',
- '41' => 'Loir-et-Cher',
- '42' => 'Loire',
- '43' => 'Haute-Loire',
- '44' => 'Loire-Atlantique',
- '45' => 'Loiret',
- '46' => 'Lot',
- '47' => 'Lot-et-Garonne',
- '48' => 'Loz&#232;re',
- '49' => 'Maine-et-Loire',
- '50' => 'Manche',
- '51' => 'Marne',
- '52' => 'Haute-Marne',
- '53' => 'Mayenne',
- '54' => 'Meurthe-et-Moselle',
- '55' => 'Meuse',
- '56' => 'Morbihan',
- '57' => 'Moselle',
- '58' => 'Ni&#232;vre',
- '59' => 'Nord',
- '60' => 'Oise',
- '61' => 'Orne',
- '62' => 'Pas-de-Calais',
- '63' => 'Puy-de-D&#244;me',
- '64' => 'Pyr&#233;n&#233;es-Atlantiques',
- '65' => 'Hautes-Pyr&#233;n&#233;es',
- '66' => 'Pyr&#233;n&#233;es-Orientales',
- '67' => 'Bas-Rhin',
- '68' => 'Haut-Rhin',
- '69' => 'Rh&#244;ne',
- '70' => 'Haute-Sa&#244;ne',
- '71' => 'Sa&#244;ne-et-Loire',
- '72' => 'Sarthe',
- '73' => 'Savoie',
- '74' => 'Haute-Savoie',
- '75' => 'Paris',
- '76' => 'Seine-Maritime',
- '77' => 'Seine-et-Marne',
- '78' => 'Yvelines',
- '79' => 'Deux-S&#232;vres',
- '80' => 'Somme',
- '81' => 'Tarn',
- '82' => 'Tarn-et-Garonne',
- '83' => 'Var',
- '84' => 'Vaucluse',
- '85' => 'Vend&#233;e',
- '86' => 'Vienne',
- '87' => 'Haute-Vienne',
- '88' => 'Vosges',
- '89' => 'Yonne',
- '90' => 'Territoire de Belfort',
- '91' => 'Essonne',
- '92' => 'Hauts-de-Seine',
- '93' => 'Seine-St-Denis',
- '94' => 'Val-de-Marne',
- '95' => 'Val-D\'Oise',
- '971' => 'Guadeloupe',
- '972' => 'Martinique',
- '973' => 'Guyane',
- '974' => 'R&#233;union',
- '975' => 'Saint-Pierre-et-Miquelon',
- '976' => 'Mayotte',
- '984' => 'Terres Australes et Antarctiques',
- '985' => 'Mayotte', // deprecated
- '986' => 'Wallis-et-Futuna',
- '987' => 'Polyn&#233;sie fran&#231;aise',
- '988' => 'Nouvelle-Cal&#233;donie'
- );
-
- return isset($departement) ?
- (isset($return[$departement]) ? $return[$departement] : '') :
- $return;
- }
-
- /**
- * Validates a French "departement"
- *
- * @param string $region 2 (2A or 2B included) or 3 -digit department number
- *
- * @return mixed The department's name, special chars numeric entities
- * (true) or false
- * @static
- */
- function region($region)
- {
- //return ($return = Validate_FR::nameDepartement($region)) ? $return : false;
- if (Validate_FR::nameDepartement($region) !== '') {
- return true;
- } else {
- return false;
- }
- }
-}
View
31 tests/bug7389.phpt
@@ -1,31 +0,0 @@
---TEST--
-#7389, Codes postaux non pris en compte
---FILE--
-<?php
-require_once 'Validate/FR.php';
-if (validate_FR::postalCode("98001")) {
- echo "#1 Ok\n";
-}
-if (validate_FR::postalCode("98701")) {
- echo "#2 Ok\n";
-}
-if (validate_FR::postalCode("98801")) {
- echo "#3 Ok\n";
-}
-if (validate_FR::region("986")) {
- echo "#4 Ok\n";
-}
-if (validate_FR::region("987")) {
- echo "#5 Ok\n";
-}
-if (validate_FR::region("971")) {
- echo "#6 Ok\n";
-}
-?>
---EXPECT--
-#1 Ok
-#2 Ok
-#3 Ok
-#4 Ok
-#5 Ok
-#6 Ok
View
213 tests/validate_FR.phpt
@@ -1,213 +0,0 @@
---TEST--
-validate_FR.phpt: Unit tests for 'Validate/FR.php'
---FILE--
-<?php
-// $Id$
-
-include (dirname(__FILE__).'/validate_functions.inc');
-if (is_file(dirname(__FILE__) . '/../Validate/FR.php')) {
- require_once dirname(__FILE__) . '/../Validate/FR.php';
-} else {
- require_once 'Validate/FR.php';
-}
-
-echo "Test Validate_FR\n";
-echo "****************\n";
-
-$noYes = array('KO', 'OK');
-$symbol = array('!X!', ' V ');
-
-$ssns = array( '156077851718185' => 'OK',
-'2781120050003' => 'KO', // too short
-'278102305000331' => 'OK',
-'278112005000332' => 'KO', // bad checksum
-'278112B0500033112' => 'KO', // too long
-'278112005000331' => 'OK',
-'278112005000339' => 'KO', // bad checksum
-'578112005000375' => 'KO', // bad sex
-'278132005000363' => 'KO', // bad month
-'278130005000321' => 'KO', // bad dept
-'2x8112005000331' => 'KO', // NOK bad alpha
-);
-
-$ribs = array( '20041 01003 0175293T024 33' => 'OK',
-'45499 91289 01697111 65' => 'OK',
-'20041 01003 0175293T024 34' => 'KO',
-'45499 91289 01697111 5' => 'KO',
-'12345 12345 12345678901 46' => 'OK',
-'1234 5123451 2345678901 46' => 'KO',
-'12345 12345 12345678901 47' => 'KO',
-'12345 12345 1234E67H901 46' => 'OK',
-'12345 12345 1234E67B901 46' => 'KO',
-'12345 12345 VALIDATEURS 01' => 'OK',
-'12345 12345 1234E67H901 45' => 'KO',
-'12345 12345 VALIDATEURS 00' => 'KO',
-'12345 12345 51394135492 01' => 'OK',
-'11111 22222 33333333333 91' => 'OK',
-'11111 22222 33333333298 02' => 'OK',
-'11111 22222 33333333298 99' => 'OK',
-);
-
-$sirens = array('423068147' => 'OK',
-'123456789' => 'KO',
-'123456782' => 'OK',
-'422260208' => 'OK',
-'12345678' => 'KO',
-'1234567840' => 'KO',
-);
-
-$sirets = array('42306814700010' => 'OK',
-'12345678912345' => 'KO', //
-'12345678200010' => 'OK',
-'42226020800026' => 'OK',
-'1234567891234x25' => 'KO', //
-'12345678' => 'KO', //
-'1234567840' => 'KO',
-); // NOK
-
-$postalCodes = array('01234' => 'OK',
-'012345' => 'KO',
-'0123' => 'KO',
-'00234' => 'KO',
-'99234' => 'OK',
-'2A234' => 'KO',
-'20234' => 'OK',
-'0123X' => 'KO',
-);
-
-$regions = array('12' => 'OK',
-'00' => 'KO',
-'1' => 'KO',
-'100' => 'KO',
-'20' => 'KO',
-'2A' => 'OK',
-'2B' => 'OK',
-'2C' => 'KO',
-'972' => 'OK',
-'979' => 'KO',
-);
-
-
-$errorFound = false;
-$errorFound = $errorFound || test_func(array('Validate_FR','ssn' ), $ssns );
-$errorFound = $errorFound || test_func(array('Validate_FR','siren' ), $sirens );
-$errorFound = $errorFound || test_func(array('Validate_FR','siret' ), $sirets );
-$errorFound = $errorFound || test_func(array('Validate_FR','postalCode' ), $postalCodes );
-$errorFound = $errorFound || test_func(array('Validate_FR','region' ), $regions );
-
-// rib need an array as parameters. test_func is not ready for that.
-
-echo "---------\nTest Validate_FR::rib\n";
-echo ' _ Value State Return' . "\n";
-echo ' V = validation result is right' . "\n";
-echo ' X = validation result is wrong' . "\n";
-
-foreach ($ribs as $rib => $state) {
- $arib = explode(' ', $rib);
- $resultWaited = $noYes[Validate_FR::rib(
- array( 'codebanque' => $arib[0],
- 'codeguichet' => $arib[1],
- 'nocompte' => $arib[2],
- 'key' => $arib[3])
- )];
-
- echo ($resultWaited == $state ? ' V ' : '!X!')
- . str_pad($rib, 30) . " : " . $state . ' ' . $resultWaited . "\n";
-
-}
-
-echo ($errorFound) ? '... FAILED' : '... SUCCESS';
-
-?>
---EXPECT--
-Test Validate_FR
-****************
----------
-Test Validate_FR::ssn
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 156077851718185 : OK OK
- V 2781120050003 : KO KO
- V 278102305000331 : OK OK
- V 278112005000332 : KO KO
- V 278112B0500033112 : KO KO
- V 278112005000331 : OK OK
- V 278112005000339 : KO KO
- V 578112005000375 : KO KO
- V 278132005000363 : KO KO
- V 278130005000321 : KO KO
- V 2x8112005000331 : KO KO
----------
-Test Validate_FR::siren
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 423068147 : OK OK
- V 123456789 : KO KO
- V 123456782 : OK OK
- V 422260208 : OK OK
- V 12345678 : KO KO
- V 1234567840 : KO KO
----------
-Test Validate_FR::siret
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 42306814700010 : OK OK
- V 12345678912345 : KO KO
- V 12345678200010 : OK OK
- V 42226020800026 : OK OK
- V 1234567891234x25 : KO KO
- V 12345678 : KO KO
- V 1234567840 : KO KO
----------
-Test Validate_FR::postalCode
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 01234 : OK OK
- V 012345 : KO KO
- V 0123 : KO KO
- V 00234 : KO KO
- V 99234 : OK OK
- V 2A234 : KO KO
- V 20234 : OK OK
- V 0123X : KO KO
----------
-Test Validate_FR::region
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 12 : OK OK
- V 00 : KO KO
- V 1 : KO KO
- V 100 : KO KO
- V 20 : KO KO
- V 2A : OK OK
- V 2B : OK OK
- V 2C : KO KO
- V 972 : OK OK
- V 979 : KO KO
----------
-Test Validate_FR::rib
- _ Value State Return
- V = validation result is right
- X = validation result is wrong
- V 20041 01003 0175293T024 33 : OK OK
- V 45499 91289 01697111 65 : OK OK
- V 20041 01003 0175293T024 34 : KO KO
- V 45499 91289 01697111 5 : KO KO
- V 12345 12345 12345678901 46 : OK OK
- V 1234 5123451 2345678901 46 : KO KO
- V 12345 12345 12345678901 47 : KO KO
- V 12345 12345 1234E67H901 46 : OK OK
- V 12345 12345 1234E67B901 46 : KO KO
- V 12345 12345 VALIDATEURS 01 : OK OK
- V 12345 12345 1234E67H901 45 : KO KO
- V 12345 12345 VALIDATEURS 00 : KO KO
- V 12345 12345 51394135492 01 : OK OK
- V 11111 22222 33333333333 91 : OK OK
- V 11111 22222 33333333298 02 : OK OK
- V 11111 22222 33333333298 99 : OK OK
-... SUCCESS
Please sign in to comment.
Something went wrong with that request. Please try again.