cvs: pear /Validate/Validate ISPN.php

From: Date: Fri, 21 Jan 2005 02:52:57 +0000
Subject: cvs: pear /Validate/Validate ISPN.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-28716@lists.php.net to get a copy of this message
dufuz Thu Jan 20 21:52:57 2005 EDT Modified files: /pear/Validate/Validate ISPN.php Log: unified ean, ucc and sscc funcs like Daniel C suggested, still some issues with those static things left. add @access doc block http://cvs.php.net/diff.php/pear/Validate/Validate/ISPN.php?r1=1.1&r2=1.2&ty=u Index: pear/Validate/Validate/ISPN.php diff -u pear/Validate/Validate/ISPN.php:1.1 pear/Validate/Validate/ISPN.php:1.2 --- pear/Validate/Validate/ISPN.php:1.1 Thu Jan 20 10:18:32 2005 +++ pear/Validate/Validate/ISPN.php Thu Jan 20 21:52:56 2005 @@ -17,7 +17,7 @@ // | Damien Seguy <dams@nexen.net> | // +----------------------------------------------------------------------+ // -// $Id: ISPN.php,v 1.1 2005/01/20 15:18:32 dufuz Exp $ +// $Id: ISPN.php,v 1.2 2005/01/21 02:52:56 dufuz Exp $ /** * ISPN is also known as International Standard Product Numbers @@ -34,6 +34,7 @@ * * This function checks given number according * + * @access public * @param string $isbn number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Damien Seguy <dams@nexen.net> @@ -82,6 +83,7 @@ * ISSN identifies periodical publications: * http://www.issn.org * + * @access public * @param string $issn number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -112,6 +114,7 @@ * or an element in a multi-media kit: * http://www.ismn-international.org/ * + * @access public * @param string $ismn ISMN number * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -141,6 +144,7 @@ * http://www.ean-ucc.org/ * http://www.uc-council.org/checkdig.htm * + * @access public * @param string $ean number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -148,15 +152,7 @@ function ean8($ean) { static $weights_ean8 = array(3,1,3,1,3,1,3); - - $ean = str_replace(array('-', '/', ' ', "\t", "\n"), '', $ean); - - // check if this is a 8-digit number - if (!is_numeric($ean) || strlen($ean) != 8) { - return false; - } - - return Validate::_checkControlNumber($ean, $weights_ean8, 10, 10); + return Validate_ISPN::_process($ean, 8, $weights_ean8); } /** @@ -167,6 +163,7 @@ * http://www.ean-ucc.org/ * http://www.uc-council.org/checkdig.htm * + * @access public * @param string $ean number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -174,15 +171,7 @@ function ean13($ean) { static $weights_ean13 = array(1,3,1,3,1,3,1,3,1,3,1,3); - - $ean = str_replace(array('-', '/', ' ', "\t", "\n"), '', $ean); - - // check if this is a 13-digit number - if (!is_numeric($ean) || strlen($ean) != 13) { - return false; - } - - return Validate::_checkControlNumber($ean, $weights_ean13, 10, 10); + return Validate_ISPN::_process($ean, 13, $weights_ean13); } /** @@ -193,6 +182,7 @@ * http://www.ean-ucc.org/ * http://www.uc-council.org/checkdig.htm * + * @access public * @param string $ean number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -200,15 +190,7 @@ function ean14($ean) { static $weights_ean14 = array(3,1,3,1,3,1,3,1,3,1,3,1,3); - - $ean = str_replace(array('-', '/', ' ', "\t", "\n"), '', $ean); - - // check if this is a 14-digit number - if (!is_numeric($ean) || strlen($ean) != 14) { - return false; - } - - return Validate::_checkControlNumber($ean, $weights_ean14, 10, 10); + return Validate_ISPN::_process($ean, 14, $weights_ean14); } /** @@ -219,6 +201,7 @@ * http://www.ean-ucc.org/ * http://www.uc-council.org/checkdig.htm * + * @access public * @param string $ucc number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -226,15 +209,7 @@ function ucc12($ucc) { static $weights_ucc12 = array(3,1,3,1,3,1,3,1,3,1,3); - - $ucc = str_replace(array('-', '/', ' ', "\t", "\n"), '', $ucc); - - // check if this is a 12-digit number - if (!is_numeric($ucc) || strlen($ucc) != 12) { - return false; - } - - return Validate::_checkControlNumber($ucc, $weights_ucc12, 10, 10); + return Validate_ISPN::_process($ucc, 12, $weights_ucc12); } /** @@ -245,6 +220,7 @@ * http://www.ean-ucc.org/ * http://www.uc-council.org/checkdig.htm * + * @access public * @param string $sscc number (only numeric chars will be considered) * @return bool true if number is valid, otherwise false * @author Piotr Klaban <makler@man.torun.pl> @@ -252,15 +228,31 @@ function sscc($sscc) { static $weights_sscc = array(3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3); + return Validate_ISPN::_process($sscc, 18, $weights_sscc); + } + + /** + * Does all the work for EAN8, EAN13, EAN14, UCC12 and SSCC + * + * @access private + * @param int $data number (only numeric chars will be considered) + * @param int $lenght required length of number string + * @param array $weights holds the weight that will be used in calculations for the validation + * @return bool true if number is valid, otherwise false + */ + function _process($data, $length, &$weights) + { + //$weights = array(3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3); + //$weights = array_slice($weights, 0, $length); - $sscc = str_replace(array('-', '/', ' ', "\t", "\n"), '', $sscc); + $data = str_replace(array('-', '/', ' ', "\t", "\n"), '', $data); // check if this is a 18-digit number - if (!is_numeric($sscc) || strlen($sscc) != 18) { + if (!is_numeric($data) || strlen($data) != $length) { return false; } - return Validate::_checkControlNumber($sscc, $weights_sscc, 10, 10); + return Validate::_checkControlNumber($data, $weights, 10, 10); } } ?> \ No newline at end of file

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