cvs: pear /Validate/Validate IS.php /Validate/tests validate_IS.phpt

From: Date: Sat, 19 Nov 2005 17:01:51 +0000
Subject: cvs: pear /Validate/Validate IS.php /Validate/tests validate_IS.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-36381@lists.php.net to get a copy of this message
bjori Sat Nov 19 12:01:51 2005 EDT Modified files: /pear/Validate/Validate IS.php /pear/Validate/tests validate_IS.phpt Log: Validate_IS::postalCode() - Fixed the comments - Allow user to specify datafile rather then datadir - Dont run unnecessary is_readable() statements - Check if the datafile has changed since last (speed up postcode checks) - Added tests for $strong http://cvs.php.net/diff.php/pear/Validate/Validate/IS.php?r1=1.14&r2=1.15&ty=u Index: pear/Validate/Validate/IS.php diff -u pear/Validate/Validate/IS.php:1.14 pear/Validate/Validate/IS.php:1.15 --- pear/Validate/Validate/IS.php:1.14 Thu Nov 17 10:55:53 2005 +++ pear/Validate/Validate/IS.php Sat Nov 19 12:01:50 2005 @@ -23,7 +23,7 @@ * @author Hannes Magnússon <bjori@php.net> * @copyright 1997-2005 Hannes Magnússon * @license http://www.opensource.org/licenses/bsd-license.php new BSD - * @version CVS: $Id: IS.php,v 1.14 2005/11/17 15:55:53 bjori Exp $ + * @version CVS: $Id: IS.php,v 1.15 2005/11/19 17:01:50 bjori Exp $ */ /** @@ -125,40 +125,47 @@ /** * validates a postcode * - * Validates Icelandic postalcodes. By defaults checks against (prefetched) - * csv list containing all Icelanidc postalcodes. If the list is one month - * old, trys to update it. + * 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. + * * * @access public * @param int the postcode to be validated * @param bool optional; check against the official list (default off) - * @param string optional; /path/to/data/dir/ + * @param string optional; /path/to/data/file.txt * @param string optional; http://domain.tld/path/to/live/data/file.txt * @return bool */ - function postalCode($postcode, $strong = false, $dataDir = '', $url = '') + function postalCode($postCode, $strong = false, + $dataFile = '@DATADIR@/Validate_IS/IS_postcodes.txt', + $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 */ - if ($postcode <= 100 || $postcode > 950) { + $postCode = (int)$postCode; + if ($postCode <= 100 || $postCode > 950) { return false; } - - if (!$dataDir) { - $dataDir = '@DATADIR@/Validate_IS'; + /* Same configuration as last time? No need to go further then */ + if (count($postCodes) && $dataFile == $lastFile && + (($strong && $lastUrl == $url) || !$strong)) { + return in_array($postCode, $postCodes); + } + /* Sanity check the data file */ + if (!is_readable($dataFile)) { + return false; } - $file = is_readable($dataDir.'/IS_postcodes.txt') ? - $dataDir.'/IS_postcodes.txt' : - '@DATADIR@/Validate_IS/IS_postcodes.txt'; - - static $postCodes = array(); - static $lastUrl = ''; - if ($strong && $lastUrl != $url) { - $lastUrl = $url; - if (!$url) { - $url = "http://www.postur.is/gogn/Gotuskra/postnumer.txt"; - } - + /* Live check */ + if ($strong) { $fp = fopen($url, 'r'); if ($fp) { $postCodes = array(); @@ -168,8 +175,8 @@ unset($postCodes[0]); // Fake entry fclose($fp); - if (is_writable($file)) { - $fp = fopen($file, 'w'); + if (is_writable($dataFile)) { + $fp = fopen($dataFile, 'w'); if ($fp) { fwrite($fp, implode("\n", $postCodes)); fclose($fp); @@ -178,10 +185,11 @@ } } - if (!count($postCodes) && file_exists($file)) { - $postCodes = file($file); + if (!count($postCodes) && file_exists($dataFile)) { + $postCodes = file($dataFile); + $lastFile = $dataFile; } - if (is_array($postCodes) && in_array($postcode, $postCodes)) { + if (count($postCodes) && in_array($postCode, $postCodes)) { return true; } http://cvs.php.net/diff.php/pear/Validate/tests/validate_IS.phpt?r1=1.2&r2=1.3&ty=u Index: pear/Validate/tests/validate_IS.phpt diff -u pear/Validate/tests/validate_IS.phpt:1.2 pear/Validate/tests/validate_IS.phpt:1.3 --- pear/Validate/tests/validate_IS.phpt:1.2 Wed Nov 16 04:38:45 2005 +++ pear/Validate/tests/validate_IS.phpt Sat Nov 19 12:01:51 2005 @@ -2,7 +2,7 @@ validate_IS.phpt: Unit tests for 'Validate/IS.php' --FILE-- <?php -// $Id: validate_IS.phpt,v 1.2 2005/11/16 09:38:45 bjori Exp $ +// $Id: validate_IS.phpt,v 1.3 2005/11/19 17:01:51 bjori Exp $ // Validate test script $noYes = array('NO', 'YES'); require 'Validate/IS.php'; @@ -59,6 +59,10 @@ echo "{$postalCode}: ".$noYes[$validate->postalCode($postalCode)]."\n"; } +echo "\nTest postalCode strong\n"; +foreach($postalCodes as $postalCode) { + printf("%s: %s\n", $postalCode, $noYes[$validate->postalCode($postalCode, true)]); +} echo "\nTest telNumbers\n"; foreach ($telNumbers as $tel) { echo "{$tel}: ".$noYes[$validate->tel($tel)]."\n"; @@ -93,6 +97,32 @@ 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 telNumbers 5642240: YES +354 664 22 40: YES

« previous php.pear.cvs (#36381) next »