Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Split Validate/Validate_IS

  • Loading branch information...
commit c9d83a9c5c9d90f4da6b48318c0ef4d6363e64a0 1 parent dfc8ee5
Daniel O'Connor CloCkWeRX authored
359 Validate/IS.php
View
@@ -1,359 +0,0 @@
-<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-/**
- * Specific validation methods for data used in Iceland
- *
- * 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
- * @subpackage Validate_IS
- * @author Hannes Magnusson <bjori@php.net>
- * @copyright 2005-2006 Hannes Magnusson
- * @license http://www.opensource.org/licenses/bsd-license.php new BSD
- * @version CVS: $Id$
- * @link http://pear.php.net/package/Validate_IS
- */
-
-/**
- * Data validation class for Iceland
- *
- * This class provides methods to validate:
- * - SSN (Social Security Number (Icelandic: kennitala))
- * - Postal code (Icelandic: post numer)
- * - Address (Icelandic: heimilisfang)
- * - Telephone number (Icelandic: simanumer)
- *
- * @category Validate
- * @package Validate
- * @subpackage Validate_IS
- * @author Hannes Magnusson <bjori@php.net>
- * @copyright 2005-2006 Hannes Magnusson
- * @license http://www.opensource.org/licenses/bsd-license.php new BSD
- * @version Release: @package_version@
- * @link http://pear.php.net/package/Validate_IS
- */
-class Validate_IS
-{
- /**
- * Validate Icelandic SSN (kennitolu)
- *
- * Validation according to http://www.hagstofa.is/?PageID=1474
- *
- * Note: Icelandic SSN (Social Security Number) is on the form ddmmyy-xxxx.
- * Here we strip the "-" char from the string (if present) and/or
- * spaces in our match.
- * Note: PASS IN STRING, NO EXCEPTIONS! SSN with leading zero
- * (passed as integer) will NOT get validated!
- *
- * @param string $ssn SSN
- *
- * @access public
- * @return bool
- */
- function ssn($ssn)
- {
- /* Sanity checks, Icelandic ssn are 10 numbers */
- if (!ctype_digit($ssn)) {
- $ssn = str_replace(array(' ', '-'), '', $ssn);
- }
- if (strlen($ssn) != 10 || !ctype_digit($ssn)) {
- return false;
- }
-
- $tmp = array();
- preg_match_all('([0-9]{2})', $ssn, $tmp);
- $kt = $tmp[0];
- /*
- * $kt[0] = date
- * $kt[1] = month
- * $kt[2] = year (2 letters!)
- */
- switch($ssn{9}) {
- case 0:
- $kt[2] += 2000;
- break;
- case 1:
- $kt[3] += 2100;
- break;
- case 8:
- $kt[2] += 1800;
- break;
- case 9:
- $kt[2] += 1900;
- break;
- default:
- return false;
- }
-
- if (!checkdate($kt[1], $kt[0], $kt[2])) {
- return false;
- }
-
- /* Calculate the nineth number (Icelandic: vartala) */
- $sum = $ssn{7}*2;
- $sum += $ssn{6}*3;
- $sum += $ssn{5}*4;
- $sum += $ssn{4}*5;
- $sum += $ssn{3}*6;
- $sum += $ssn{2}*7;
- $sum += $ssn{1}*2;
- $sum += $ssn{0}*3;
-
- $varTala = 11 - ($sum % 11);
- switch($varTala) {
- case 10: // "Hagstofan" has nothing to say what should happen here
- // it looks like "vartalan" should be set to 0
- case 11:
- $varTala = 0;
- break;
- }
-
- if ($ssn{8} != $varTala) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Validates Icelandic postal codes (postnumer)
- *
- * Validates Icelandic postalcodes. By default checks against (prefetched)
- * list containing all Icelandic postalcodes.
- * Live check (against, by default, the official list) is available by
- * switching $strong (2nd parameter) to true.
- * $dataFile will be rewritten with the data retrived from $url in $strong mode
- *
- * User can provide his own datafile if he wishes and/or own "official" list.
- *
- * @param int $postCode the postcode to be validated
- * @param bool $strong optional; check against the official list
- * (default off)
- * @param string $dataDir optional; /path/to/data/dir
- * @param string $url optional; http://domain.tld/path/to/live/data/file.txt
- *
- * @access public
- * @return bool
- */
- function postalCode($postCode, $strong = false,
- $dataDir = null,
- $url = 'http://www.postur.is/gogn/Gotuskra/postnumer.txt')
- {
- static $postCodes = array();
- static $lastUrl = '';
- static $lastFile = '';
-
- /* Sanity check, all Icelandic postalcodes are between 101 and 950 */
- $postCode = (int)$postCode;
- if ($postCode <= 100 || $postCode > 950) {
- return false;
- }
-
- if (empty($dataDir)) {
- $paths = array('@DATADIR@/Validate_IS', dirname(dirname(__FILE__)) . '/data');
-
- foreach ($paths as $path) {
- if (file_exists($path)) {
- $dataDir = $path;
- }
- }
- }
-
- $dataFile = $dataDir . "/IS_postcodes.txt";
- /* Same configuration as last time? No need to go further then */
- if (count($postCodes) && $dataFile == $lastFile &&
- (!$strong || $lastUrl == $url)) {
- return in_array($postCode, $postCodes);
- }
-
- /* Live check */
- if ($strong) {
- $fp = fopen($url, 'r');
- if ($fp) {
- $postCodes = array();
- while (false !== ($data = fgetcsv($fp, 128, ';'))) {
- $postCodes[] = $data[0];
- }
- unset($postCodes[0]); // Fake entry
- fclose($fp);
-
- if (is_writable($dataFile)) {
- $fp = fopen($dataFile, 'w');
- if ($fp) {
- fwrite($fp, implode("\n", $postCodes));
- fclose($fp);
- }
- }
- }
- }
-
- if (!count($postCodes) && is_readable($dataFile)) {
- $postCodes = file($dataFile);
- $lastFile = $dataFile;
- }
- if (count($postCodes) && in_array($postCode, $postCodes)) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Checks if given address exists
- * If postcode is provided, check if address exists in that area.
- *
- * @param string $address Address to validate
- * @param int $postcode Optional; check if address exists in that area
- * @param string $dataDir Optional; /path/to/data/dir
- *
- * @return mixed false on failure
- * array on in the form of:
- * array(array("nf" => $nf,
- * "thgf" => $thgf,
- * "pnr" => $postnumer))
- * on success.
- */
- function address($address, $postcode = null, $dataDir = '')
- {
- static $lastFile;
- static $lastPos = -1;
- static $lastData = array();
-
- /* Sanity checks */
- if (!is_string($address)) {
- return false;
- }
- if (!empty($postcode)) {
- $postcode = (string)$postcode;
- }
- if (!empty($dataDir) && !is_readable($dataDir. "/IS_gotuskra.txt")) {
- return false;
- }
-
- if (ctype_digit($postcode)) {
- $rsl = Validate_IS::postalCode($postcode, false, $dataDir);
- if (!$rsl) {
- return false;
- }
- }
-
- if (empty($dataDir)) {
- $paths = array('@DATADIR@/Validate_IS', dirname(dirname(__FILE__)) . '/data');
-
- foreach ($paths as $path) {
- if (file_exists($path)) {
- $dataDir = $path;
- }
- }
- }
-
- $file = $dataDir. '/IS_gotuskra.txt';
-
- if ($file != $lastFile) {
- /* Reset cache */
- $lastData = array();
- $lastPos = -1;
- $lastFile = "";
- }
- $lastFile = $file;
-
- $fp = fopen($file, 'r');
- if (!$fp) {
- return false;
- }
- if ($lastPos>0) {
- fseek($fp, $lastPos);
- }
-
- $lastCount = count($lastData);
- $i = 0;
- $return = array();
- reset($lastData);
- while (false !== ($data = ($lastPos == 0 || $lastCount>$i) ?
- next($lastData) : fgetcsv($fp, 128, ';'))) {
- if ($i >= $lastCount) {
- $lastData[$i] = $data;
- }
- /* $data = array(0=>key, 1=>pnr 2=>nf 3=>thgf */
- if (strcasecmp($address, $data[2]) === 0 ||
- strcasecmp($address, $data[3]) === 0) {
- $return[] = array("nf" => $data[2],
- "thgf" => $data[3],
- "pnr" => $data[1]);
- if ($postcode && $postcode == $data[1]) {
- $lastPos = ftell($fp);
- /* In case we found matching address which didnt match
- * the postcode we'll return the last found
- */
- return array(current($return));
- }
- }
- $i++;
- }
- $lastPos = 0;
- fclose($fp);
-
- if (!$postcode && count($return)) {
- return $return;
- }
- return false;
- }
-
- /**
- * Validates Icelandic telephone numbers (simanumer)
- *
- * Checks that the telephone number is 7digits and legal
- * home/office/gsm number (not information/emergency service etc.)
- *
- * NOTE: Number prefixed with 00354 or +354 are allowed
- *
- * Note: Icelandic telephone numbers are on the form xxx-xxxx.
- * Here we strip the "-" char from the string (if present) and/or
- * spaces in our match.
- *
- * @param string $number the telephone number
- *
- * @access public
- * @return bool
- */
- function phoneNumber($number)
- {
- /* Replace "+" with "00" and delete spaces/hyphens */
- $number = str_replace(array('+', ' ', '-'), array('00', '', ''), $number);
-
- /* Sanity check */
- if (!ctype_digit($number)) {
- return false;
- }
-
- /* Checks if the number is 7digits or 12 (prefixed with 00354) */
- $telLength = strlen($number);
- if ($telLength != 7) {
- if ($telLength == 12) {
- if (substr($number, 0, 5) != '00354') {
- return false;
- }
- } else {
- return false;
- }
- }
-
- /* Get the first real telephone number digit and check if its valid */
- $firstDigit = substr($number, -7, 1);
- if (in_array($firstDigit, array(0, 1, 2, 3))) {
- return false;
- }
-
- return true;
- }
-}
-?>
11,806 data/IS_gotuskra.txt
View
0 additions, 11,806 deletions not shown
148 data/IS_postcodes.txt
View
@@ -1,148 +0,0 @@
-101
-102
-103
-104
-105
-107
-108
-109
-110
-111
-112
-113
-116
-121
-123
-124
-125
-127
-128
-129
-130
-132
-150
-155
-170
-172
-190
-200
-201
-202
-203
-210
-212
-220
-221
-222
-225
-230
-232
-233
-235
-240
-245
-250
-260
-270
-271
-276
-300
-301
-302
-310
-311
-320
-340
-345
-350
-355
-356
-360
-370
-371
-380
-400
-401
-410
-415
-420
-425
-430
-450
-451
-460
-465
-470
-471
-500
-510
-512
-520
-524
-530
-531
-540
-541
-545
-550
-551
-560
-565
-566
-570
-580
-600
-601
-602
-603
-610
-611
-620
-621
-625
-630
-640
-641
-645
-650
-660
-670
-671
-675
-680
-681
-685
-690
-700
-701
-710
-715
-720
-730
-735
-740
-750
-755
-760
-765
-780
-781
-785
-800
-801
-802
-810
-815
-820
-825
-840
-845
-850
-851
-860
-861
-870
-871
-880
-900
-902
141 package_IS.xml
View
@@ -1,141 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<package packagerversion="1.7.2" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
-http://pear.php.net/dtd/tasks-1.0.xsd
-http://pear.php.net/dtd/package-2.0
-http://pear.php.net/dtd/package-2.0.xsd">
- <name>Validate_IS</name>
- <channel>pear.php.net</channel>
- <summary>Validation class for Iceland</summary>
- <description>Package containes locale validation for IS (Iceland) such as:
- * SSN
- * Postal Code
- * Address
- * Telephone
-
- </description>
- <lead>
- <name>Hannes Magnússon</name>
- <user>bjori</user>
- <email>bjori@php.net</email>
- <active>yes</active>
- </lead>
- <date>2009-03-22</date>
- <time>01:13:17</time>
- <version>
- <release>0.3.0</release>
- <api>0.3.0</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <license>New BSD</license>
- <notes>
-QA release
-Bug #11824 phpcs/Coding Standards issues highlighted in IS.php
-Bug #11864 test of postalCodes when run from CVS doesn't work
-Bug #14867 Can't run unit tests from CVS without warnings
- </notes>
- <contents>
- <dir name="/">
- <dir name="data">
- <file name="IS_gotuskra.txt" role="data" />
- <file name="IS_postcodes.txt" role="data" />
- </dir> <!-- /data -->
- <dir name="tests">
- <file name="IS_address.phpt" role="test" />
- <file name="IS_phoneNumber.phpt" role="test" />
- <file name="validate_IS.phpt" role="test" />
- </dir> <!-- /tests -->
- <dir name="Validate">
- <file name="IS.php" role="php">
- <tasks:replace from="@DATADIR@" to="data_dir" type="pear-config" />
- </file>
- </dir> <!-- /Validate -->
- <file name="LICENSE" role="doc" />
- </dir> <!-- / -->
- </contents>
- <dependencies>
- <required>
- <php>
- <min>4.2.0</min>
- </php>
- <pearinstaller>
- <min>1.4.0b1</min>
- </pearinstaller>
- <extension>
- <name>ctype</name>
- </extension>
- </required>
- </dependencies>
- <phprelease>
- <filelist>
- <install as="IS_postcodes.txt" name="data/IS_postcodes.txt" />
- <install as="IS_gotuskra.txt" name="data/IS_gotuskra.txt" />
- </filelist>
- </phprelease>
- <changelog>
- <release>
- <version>
- <release>0.2.0</release>
- <api>0.2.0</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2006-05-16</date>
- <license>New BSD</license>
- <notes>- Validate_IS::ssn()
- - Now uses ctype_digit() to check for all numbers
- - Fixed bug in &quot;var tala&quot; calculation
-- Validate_IS::postalCode()
- - Allow user defined data directory
- - $strong mode is now default false - BC BREAK!
- - Now use retrived data even when data file is read-only (in $strong mode)
- - Writes retrived data no matter when data file was last changed (in $strong mode)
- - Deleted bogus entrys in the data file
- - Fixed live data file (thanks to antab, arnar@8.is)
- - Added extra parameter, $url, which allows user defined live data file
-- Validate_IS::address()
- - Initial commit
-- Bump the dep to PHP 4.2+
-- Ctype is now a required dep.
-BC break:
-- tel renamed to phoneNumber to confirm to the Validate standard
-
- </notes>
- </release>
- <release>
- <version>
- <release>0.1.1</release>
- <api>0.1.1</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2005-11-04</date>
- <license>New BSD</license>
- <notes>- Switch to the new BSD License
- (see http://www.opensource.org/licenses/bsd-license.php)
-
- </notes>
- </release>
- <release>
- <version>
- <release>0.1.0</release>
- <api>0.1.0</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2005-10-01</date>
- <license>New BSD</license>
- <notes>Initial release
-
- </notes>
- </release>
- </changelog>
-</package>
42 tests/IS_address.phpt
View
@@ -1,42 +0,0 @@
---TEST--
-Validate_IS::address()
---FILE--
-<?php
- error_reporting(E_ALL & ~E_STRICT);
- require_once "Validate/IS.php";
-
- $addresses = array(
- "Reglubraut" => 1,
- "Mánagata" => 7,
- "Aðalstræti" => 6,
- "Sæbólsbraut" => 1,
- "Bógus" => 0,
- "Vestmannabraut" => 1
- );
-
- foreach($addresses as $address => $count) {
- $result = Validate_IS::address($address);
- printf("%-20s: %d (%d)\n", $address, is_array($result) ? count($result) : 0, $count);
- }
-
- print "\n";
-
- foreach($addresses as $address => $count) {
- $result = Validate_IS::address($address, 200);
- printf("%-20s: %d\n", $address, is_array($result) ? count($result) : 0);
- }
-?>
---EXPECT--
-Reglubraut : 1 (1)
-Mánagata : 7 (7)
-Aðalstræti : 6 (6)
-Sæbólsbraut : 1 (1)
-Bógus : 0 (0)
-Vestmannabraut : 1 (1)
-
-Reglubraut : 0
-Mánagata : 0
-Aðalstræti : 0
-Sæbólsbraut : 1
-Bógus : 0
-Vestmannabraut : 0
33 tests/IS_phoneNumber.phpt
View
@@ -1,33 +0,0 @@
---TEST--
-Validate_IS::phoneNumber
---FILE--
-<?php
- error_reporting(E_ALL & ~E_STRICT);
- require_once "Validate/IS.php";
-
- $phoneNumbers = array(
- 5642240 => true,
- "+354 664 22 40" => true,
- "00354 464 22 40" => true,
- "00 354 864-22 40" => true,
- "54-234-56" => true,
- "+00 354 564 22 40" => false,
- "+354 1234567" => false,
- 1234567 => false,
- 87654321 => false
- );
-
- foreach($phoneNumbers as $number => $result) {
- printf("%-20s: %s\n", $number, (int)Validate_IS::phoneNumber($number));
- }
-?>
---EXPECT--
-5642240 : 1
-+354 664 22 40 : 1
-00354 464 22 40 : 1
-00 354 864-22 40 : 1
-54-234-56 : 1
-+00 354 564 22 40 : 0
-+354 1234567 : 0
-1234567 : 0
-87654321 : 0
142 tests/validate_IS.phpt
View
@@ -1,142 +0,0 @@
---TEST--
-validate_IS.phpt: Unit tests for 'Validate/IS.php'
---FILE--
-<?php
-// $Id$
-// Validate test script
-$noYes = array('NO', 'YES');
-if (is_file(dirname(__FILE__) . '/../Validate/IS.php')) {
- require_once dirname(__FILE__) . '/../Validate/IS.php';
- $dataDir = dirname(__FILE__) . '/../data';
-} else {
- require_once 'Validate/IS.php';
- $dataDir = null;
-}
-
-$validate = new Validate_IS;
-echo "Test Validate_IS\n";
-echo "****************\n";
-
-$postalCodes = array(
- /* few of the "biggest citys" postal codes */
- 101, // OK
- 170, // OK
- 200, // OK
- 210, // OK
- 220, // OK
- 230, // OK
- 300, // OK
- 400, // OK
- 500, // OK
- 600, // OK
- 700, // OK
- 800, // OK
- 900, // OK
-
- /* random numbers, should all fail */
- 100, // NOK
- 120, // NOK
- 140, // NOK
- 205, // NOK
- 305, // NOK
- 472, // NOK
- 903, // NOK
-
- /* absurd codes */
- 99, // NOK
- 1000, // NOK
- "1OO", // NOK
- "abc", // NOK
-);
-
-$telNumbers = array(
- 5642240, // OK
- "+354 664 22 40", // OK
- "00354 464 22 40", // OK
- "00 354 864-22 40", // OK
- "+00 354 564 22 40", // NOK
- "+354 1234567", // NOK
- 1234567, // NOK
- 87654321, // NOK
- '54-234-56'); // OK
-
-echo "Test postalCode\n";
-foreach ($postalCodes as $postalCode) {
- echo "{$postalCode}: ".$noYes[$validate->postalCode($postalCode, false, $dataDir)]."\n";
-}
-
-echo "\nTest postalCode strong\n";
-foreach($postalCodes as $postalCode) {
- printf("%s: %s\n", $postalCode, $noYes[$validate->postalCode($postalCode, true, $dataDir)]);
-}
-
-echo "\nTest phoneNumbers\n";
-foreach ($telNumbers as $tel) {
- echo "{$tel}: ".$noYes[$validate->phoneNumber($tel)]."\n";
-}
-?>
---EXPECT--
-Test Validate_IS
-****************
-Test postalCode
-101: YES
-170: YES
-200: YES
-210: YES
-220: YES
-230: YES
-300: YES
-400: YES
-500: YES
-600: YES
-700: YES
-800: YES
-900: YES
-100: NO
-120: NO
-140: NO
-205: NO
-305: NO
-472: NO
-903: NO
-99: NO
-1000: NO
-1OO: NO
-abc: NO
-
-Test postalCode strong
-101: YES
-170: YES
-200: YES
-210: YES
-220: YES
-230: YES
-300: YES
-400: YES
-500: YES
-600: YES
-700: YES
-800: YES
-900: YES
-100: NO
-120: NO
-140: NO
-205: NO
-305: NO
-472: NO
-903: NO
-99: NO
-1000: NO
-1OO: NO
-abc: NO
-
-Test phoneNumbers
-5642240: YES
-+354 664 22 40: YES
-00354 464 22 40: YES
-00 354 864-22 40: YES
-+00 354 564 22 40: NO
-+354 1234567: NO
-1234567: NO
-87654321: NO
-54-234-56: YES
Please sign in to comment.
Something went wrong with that request. Please try again.