cvs: pear /Validate/Validate NZ.php /Validate/tests Validate_NZ.phpt
| From: | Ken Guest | Date: | Mon, 20 Aug 2007 23:05:05 +0000 |
| Subject: | cvs: pear /Validate/Validate NZ.php /Validate/tests Validate_NZ.phpt | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48477@lists.php.net to get a copy of this message | ||
kguest Mon Aug 20 23:05:05 2007 UTC
Modified files:
/pear/Validate/Validate NZ.php
/pear/Validate/tests Validate_NZ.phpt
Log:
fix for bug #11879. IRD numbers must be mod 11 checked - else invalid IRD values are marked valid.
http://cvs.php.net/viewvc.cgi/pear/Validate/Validate/NZ.php?r1=1.20&r2=1.21&diff_format=u
Index: pear/Validate/Validate/NZ.php
diff -u pear/Validate/Validate/NZ.php:1.20 pear/Validate/Validate/NZ.php:1.21
--- pear/Validate/Validate/NZ.php:1.20 Sun Aug 19 23:37:18 2007
+++ pear/Validate/Validate/NZ.php Mon Aug 20 23:05:05 2007
@@ -25,7 +25,7 @@
* @author Byron Adams <Byron.adams54@gmail.com>
* @copyright 2006 Byron Adams
* @license http://www.opensource.org/licenses/bsd-license.php
New BSD License
- * @version CVS: $Id: NZ.php,v 1.20 2007/08/19 23:37:18 kguest Exp $
+ * @version CVS: $Id: NZ.php,v 1.21 2007/08/20 23:05:05 kguest Exp $
* @link http://pear.php.net/package/Validate_NZ
*/
@@ -113,6 +113,8 @@
*
* @access public
* @return bool The valid or invalid ird number
+ *
+ * @link www.ird.govt.nz/resources/file/eb70020dbbadb13/rwt-nrwt-spec-2006.pdf
*/
function ssn($ssn)
{
@@ -122,11 +124,11 @@
}
switch (strlen($ssn)) {
case 8:
- return true;
+ return Validate_NZ::checkIRD($ssn);
break;
case 9:
if ($ssn{0} == "0") {
- return true;
+ return Validate_NZ::checkIRD($ssn);
}
break;
}
@@ -247,4 +249,48 @@
!ctype_digit($reg) &&
in_array(strlen($reg), array("6", "5")));
}
+ /**
+ * Return true if the checksum[s] in the specified value is valid as
+ * regards the value being a valid IRD number.
+ *
+ * @param string $ssn Value to perform the validation on
+ *
+ * @access public
+ * @return boolean
+ */
+ function checkIRD($ssn)
+ {
+ $ird = (int) str_replace("-", "", $ssn);
+ if (strlen("$ird") == 8 ) {
+ //should be 8 characters in length: converting to int drops leading zero.
+ if ($ird < 10000000) {
+ return false;
+ }
+ $weights = array(2,7,6,5,4,3,2);
+ $sird = "$ird";
+ $sum = 0;
+ for ($i = 0; $i < 7; ++$i) {
+ $sum += $sird[$i] * $weights[$i];
+ }
+ $remainder = ($sum%11);
+ $checkdigit = 11 - $remainder;
+ if ($sird[7] == $checkdigit) {
+ return true;
+ }
+ if ($checkdigit == 10) {
+ $weights = array(4,3,2,5,2,7,6);
+ $sum = 0;
+ for ($i = 0; $i < 7; ++$i) {
+ $sum += $sird[$i] * $weights[$i];
+ }
+ $remainder = ($sum%11);
+ $checkdigit = 11 - $remainder;
+ if ($checkdigit == 10) {
+ return false;
+ }
+ return ($sird[7] == $checkdigit);
+ }
+ }
+ return false;
+ }
}
http://cvs.php.net/viewvc.cgi/pear/Validate/tests/Validate_NZ.phpt?r1=1.8&r2=1.9&diff_format=u
Index: pear/Validate/tests/Validate_NZ.phpt
diff -u pear/Validate/tests/Validate_NZ.phpt:1.8 pear/Validate/tests/Validate_NZ.phpt:1.9
--- pear/Validate/tests/Validate_NZ.phpt:1.8 Mon Aug 20 20:45:03 2007
+++ pear/Validate/tests/Validate_NZ.phpt Mon Aug 20 23:05:05 2007
@@ -5,7 +5,11 @@
// Validate test script
$noYes = array('NO', 'YES');
-require 'Validate/NZ.php';
+if (is_file(dirname(__FILE__) . '/../Validate/NZ.php')) {
+ require_once dirname(__FILE__) . '/../Validate/NZ.php';
+} else {
+ require_once 'Validate/NZ.php';
+}
echo " Test Validate_NZ\n";
echo "****************\n";
@@ -107,16 +111,6 @@
"SC", //NOk
"CAB", //NOk
"CAN"); //OK
- /*
- "South Canterbury",//OK
- "West Auckland", //OK
- "Central-Otago", //OK
- "west coast", //OK
-
- /*this ones causing trouble... its on the todo list */
- /*"west-coast"); //NOk
- */
-
$bankAccounts = array(
"06-0889-0262506-00", //OK
@@ -127,11 +121,12 @@
$IrdNumbers = array(
- "087 784 215",
- "071-321-321",
- "97 654 456",
- "83-366-3215",
- "987 784 215",);
+ "087 784 215", //VALID
+ "036-410-232", //VALID
+ "071-321-321",//INVALID
+ "97 654 456", //INVALID
+ "83-366-3215", //INVALID
+ "987 784 215",); //INVALID
$Carreg = array("AE12Y3", //Ok
"000000", //NOk
@@ -249,8 +244,9 @@
06-088902625060: NO
----Test IRD Numbers (SSN)----
087 784 215: YES
-071-321-321: YES
-97 654 456: YES
+036-410-232: YES
+071-321-321: NO
+97 654 456: NO
83-366-3215: NO
987 784 215: NO
----Test Vehicle License plates ----