Re: cvs: pear /Validate/Validate IS.php /Validate/tests validate_IS.phpt
| From: | bertrand Gugger | Date: | Sun, 20 Nov 2005 07:11:12 +0000 |
| Subject: | Re: cvs: pear /Validate/Validate IS.php /Validate/tests validate_IS.phpt | ||
| References: | 1 | Groups: | php.pear.cvs php.pear.cvs |
| Request: | Send a blank email to pear-cvs+get-36384@lists.php.net to get a copy of this message | ||
Bonjour,
Really nice, with the save option, it's something more
Just 2 little things:
Hannes Magnusson wrote:
bjori Sat Nov 19 12:01:51 2005 EDTis more efficientModified 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)) { *** (!$strong || $lastUrl == $url)) {
+ return in_array($postCode, $postCodes); + }******************
+ /* Sanity check the data file */
+ if (!is_readable($dataFile)) {
+ return false;
}
******************
I would simply not do that, you could imagine to run only on $url, no $file.
Check it better as last mean after trying url, see below
You take the file as last mean, if url was not used or did not work- $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)) { *** if (!count($postCodes) && $dataFile && is_readable($dataFile)) {
+ $postCodes = file($dataFile);
+ $lastFile = $dataFile;
}
- if (is_array($postCodes) && in_array($postcode, $postCodes)) {
+ if (count($postCodes) && in_array($postCode, $postCodes)) {
return true;
}
Haha, interesting discussion ... but I like the autosave feature based on the writable or not file:
I mean in normal case, the web server will not have it , so no risk.
But then, if the admin runs it in a cli as root, then he *will* update the file.
a+
--
toggg