Validate_ISPN
[ class tree: Validate_ISPN ] [ index: Validate_ISPN ] [ all elements ]

Source for file ISPN.php

Documentation is available at ISPN.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 1997-2005 Piotr Klaban, Damien Seguy, Helgi Þormar     |
  5. // |                        Þorbjörnsson, Pierre-Alain Joye               |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to the New BSD license, That is bundled  |
  8. // | with this package in the file LICENSE, and is available through      |
  9. // | the world-wide-web at                                                |
  10. // | http://www.opensource.org/licenses/bsd-license.php                   |
  11. // | If you did not receive a copy of the new BSDlicense and are unable   |
  12. // | to obtain it through the world-wide-web, please send a note to       |
  13. // | pajoye@php.net so we can mail you a copy immediately.                |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Tomas V.V.Cox  <cox@idecnet.com>                             |
  16. // |         Pierre-Alain Joye <pajoye@php.net>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. /**
  20.  * Specific validation methods for International Standard Product Numbers (ISPN)
  21.  *
  22.  * @category   Validate
  23.  * @package    Validate_ISPN
  24.  * @author     Piotr Klaban <makler@man.torun.pl>
  25.  * @author     Damien Seguy <dams@nexen.net>
  26.  * @author     Helgi Þormar Þorbjörnsson <dufuz@php.net>
  27.  * @copyright   1997-2005 Piotr Klaban, Damien Seguy, Helgi Þormar Þorbjörnsson,
  28.  *                         Pierre-Alain Joye
  29.  * @license    http://www.opensource.org/licenses/bsd-license.php  new BSD
  30.  * @version    CVS: $Id: ISPN.php,v 1.13 2006/08/17 19:20:51 makler Exp $
  31.  * @link       http://pear.php.net/package/Validate_ISPN
  32.  */
  33.  
  34. /**
  35.  * Data validation class for International Standard Product Numbers (ISPN)
  36.  *
  37.  * This class provides methods to validate:
  38.  *  - ISBN (International Standard Book Number)
  39.  *  - ISSN (International Standard Serial Number)
  40.  *  - ISMN (International Standard Music Number)
  41.  *  - ISRC (International Standard Recording Code)
  42.  *  - EAN/UCC-8 number
  43.  *  - EAN/UCC-13 number
  44.  *  - EAN/UCC-14 number
  45.  *  - UCC-12 (U.P.C.) ID number
  46.  *  - SSCC (Serial Shipping Container Code)
  47.  *
  48.  * @category   Validate
  49.  * @package    Validate_ISPN
  50.  * @author     Piotr Klaban <makler@man.torun.pl>
  51.  * @author     Damien Seguy <dams@nexen.net>
  52.  * @author     Helgi Þormar Þorbjörnsson <dufuz@php.net>
  53.  * @copyright   1997-2005 Piotr Klaban, Damien Seguy, Helgi Þormar Þorbjörnsson,
  54.  *                         Pierre-Alain Joye
  55.  * @license    http://www.opensource.org/licenses/bsd-license.php  new BSD
  56.  * @version    Release: @package_version@
  57.  * @link       http://pear.php.net/package/Validate_ISPN
  58.  */
  59. {
  60.     function isbn($isbn)
  61.     {
  62.         if (preg_match("/[^0-9 IXSBN-]/"$isbn)) {
  63.             return false;
  64.         }
  65.  
  66.         $isbn strtoupper($isbn);
  67.         $isbn str_replace(array('ISBN''-'' '"\t""\n")''$isbn);
  68.  
  69.         if (strlen($isbn== 13{
  70.             return Validate_ISPN::isbn13($isbn);
  71.         elseif (strlen($isbn== 10{
  72.             return Validate_ISPN::isbn10($isbn);
  73.         else {
  74.             return false;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Validate a ISBN 13 number
  80.      * The ISBN is a unique machine-readable identification number,
  81.      * which marks any book unmistakably.
  82.      *
  83.      * This function checks given number according
  84.      *
  85.      * Manual can be found at http://www.isbn-international.org
  86.      *
  87.      * @param  string  $isbn number (only numeric chars will be considered)
  88.      * @return bool    true if number is valid, otherwise false
  89.      * @access public
  90.      * @author Helgi Þormar <dufuz@php.net>
  91.      * @author Piotr Klaban <makler@man.torun.pl>
  92.      */
  93.     function isbn13($isbn)
  94.     {
  95.         if (preg_match("/[^0-9 ISBN-]/"$isbn)) {
  96.             return false;
  97.         }
  98.  
  99.         $isbn strtoupper($isbn);
  100.         $isbn str_replace(array('ISBN''-'' '"\t""\n")''$isbn);
  101.         if (!preg_match('/^[0-9]{13}$/'$isbn)) {
  102.             return false;
  103.         }
  104.  
  105.         return Validate_ISPN::ean13($isbn);
  106.     }
  107.  
  108.     /**
  109.      * Validate a ISBN 10 number
  110.      * The ISBN is a unique machine-readable identification number,
  111.      * which marks any book unmistakably.
  112.      *
  113.      * This function checks given number according
  114.      *
  115.      * Manual can be found at http://www.isbn-international.org
  116.      *
  117.      * @param  string  $isbn number (only numeric chars will be considered)
  118.      * @return bool    true if number is valid, otherwise false
  119.      * @access public
  120.      * @author Damien Seguy <dams@nexen.net>
  121.      * @author Helgi Þormar <dufuz@php.net>
  122.      */
  123.     function isbn10($isbn)
  124.     {
  125.         static  $weights_isbn = array(10,9,8,7,6,5,4,3,2);
  126.  
  127.         if (preg_match("/[^0-9 IXSBN-]/"$isbn)) {
  128.             return false;
  129.         }
  130.  
  131.         $isbn = strtoupper($isbn);
  132.         $isbn = str_replace(array('ISBN''-'' '"\t""\n")''$isbn);
  133.         if (strlen($isbn!= 10{
  134.             return false;
  135.         }
  136.  
  137.         if (!preg_match('/^[0-9]{9}[0-9X]$/'$isbn)) {
  138.             return false;
  139.         }
  140.  
  141.         // Requires base class Validate
  142.                 require_once 'Validate.php';
  143.         return Validate::_checkControlNumber($isbn$weights_isbn1111);
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Validate an ISSN (International Standard Serial Number)
  149.      *
  150.      * This function checks given ISSN number
  151.      * ISSN identifies periodical publications:
  152.      * http://www.issn.org
  153.      *
  154.      * @param  string  $issn number (only numeric chars will be considered)
  155.      * @return bool    true if number is valid, otherwise false
  156.      * @access public
  157.      * @author Piotr Klaban <makler@man.torun.pl>
  158.      */
  159.     function issn($issn)
  160.     {
  161.         static $weights_issn = array(8,7,6,5,4,3,2);
  162.  
  163.         $issn = strtoupper($issn);
  164.         $issn = str_replace(array('ISSN''-''/'' '"\t""\n")''$issn);
  165.         $issn_num str_replace('X''0'$issn);
  166.  
  167.         // check if this is an 8-digit number
  168.         if (!is_numeric($issn_num|| strlen($issn!= 8{
  169.             return false;
  170.         }
  171.  
  172.         // Requires base class Validate
  173.         require_once 'Validate.php';
  174.         return Validate::_checkControlNumber($issn$weights_issn1111);
  175.     }
  176.  
  177.     /**
  178.      * Validate a ISMN (International Standard Music Number)
  179.      *
  180.      * This function checks given ISMN number (ISO Standard 10957)
  181.      * ISMN identifies all printed music publications from all over the world
  182.      * whether available for sale, hire or gratis--whether a part, a score,
  183.      * or an element in a multi-media kit.
  184.      *
  185.      * Manual can be found at:
  186.      * http://www.ismn-international.org/
  187.      *
  188.      * @param  string  $ismn ISMN number
  189.      * @return bool    true if number is valid, otherwise false
  190.      * @access public
  191.      * @author Piotr Klaban <makler@man.torun.pl>
  192.      */
  193.     function ismn($ismn)
  194.     {
  195.         static $weights_ismn = array(3,1,3,1,3,1,3,1,3);
  196.  
  197.         $ismn = strtoupper($ismn);
  198.         $ismn = str_replace(array('ISMN''-''/'' '"\t""\n")''$ismn);
  199.         // First char has to be M (after ISMN has been stripped if present)
  200.         if ($ismn{0!= 'M'{
  201.             return false;
  202.         }
  203.  
  204.         // change M to 3
  205.         $ismn{0= 3;
  206.  
  207.         // check if this is a 10-digit number
  208.         if (!is_numeric($ismn|| strlen($ismn!= 10{
  209.             return false;
  210.         }
  211.  
  212.         // Requires base class Validate
  213.         require_once 'Validate.php';
  214.         return Validate::_checkControlNumber($ismn$weights_ismn1010);
  215.     }
  216.  
  217.     /**
  218.      * Validate a ISRC (International Standard Recording Code)
  219.      *
  220.      * This function validates an International Standard Recording Code
  221.      * which is the international identification system for sound recordings
  222.      * and music videorecordings.
  223.      *
  224.      * @param  string  $isrc ISRC number
  225.      * @return bool    true if number is valid, otherwise false
  226.      * @see    http://www.ifpi.org/isrc/isrc_handbook.html
  227.      * @access public
  228.      * @author David Grant <david@grant.org.uk>
  229.      */
  230.     function isrc($isrc)
  231.     {
  232.         $isrc str_replace(array('ISRC''-'' ')''strtoupper($isrc));
  233.         if (!preg_match("/[A-Z]{2}[A-Z0-9]{3}[0-9]{7}/"$isrc)) {
  234.             return false;
  235.         }
  236.  
  237.         return true;
  238.     }
  239.  
  240.     /**
  241.      * Validate a EAN/UCC-8 number
  242.      *
  243.      * This function checks given EAN8 number
  244.      * used to identify trade items and special applications.
  245.      * http://www.ean-ucc.org/
  246.      * http://www.uc-council.org/checkdig.htm
  247.      *
  248.      * @param  string  $ean number (only numeric chars will be considered)
  249.      * @return bool    true if number is valid, otherwise false
  250.      * @access public
  251.      * @see Validate_ISPN::process()
  252.      * @author Piotr Klaban <makler@man.torun.pl>
  253.      */
  254.     function ean8($ean)
  255.     {
  256.         static $weights_ean8 = array(3,1,3,1,3,1,3);
  257.         return Validate_ISPN::process($ean8$weights_ean81010);
  258.     }
  259.  
  260.     /**
  261.      * Validate a EAN/UCC-13 number
  262.      *
  263.      * This function checks given EAN/UCC-13 number used to identify
  264.      * trade items, locations, and special applications (e.g., coupons)
  265.      * http://www.ean-ucc.org/
  266.      * http://www.uc-council.org/checkdig.htm
  267.      *
  268.      * @param  string  $ean number (only numeric chars will be considered)
  269.      * @return bool    true if number is valid, otherwise false
  270.      * @access public
  271.      * @see Validate_ISPN::process()
  272.      * @author Piotr Klaban <makler@man.torun.pl>
  273.      */
  274.     function ean13($ean)
  275.     {
  276.         static $weights_ean13 = array(1,3,1,3,1,3,1,3,1,3,1,3);
  277.         return Validate_ISPN::process($ean13$weights_ean131010);
  278.     }
  279.  
  280.     /**
  281.      * Validate a EAN/UCC-14 number
  282.      *
  283.      * This function checks given EAN/UCC-14 number
  284.      * used to identify trade items.
  285.      * http://www.ean-ucc.org/
  286.      * http://www.uc-council.org/checkdig.htm
  287.      *
  288.      * @param  string  $ean number (only numeric chars will be considered)
  289.      * @return bool    true if number is valid, otherwise false
  290.      * @access public
  291.      * @see Validate_ISPN::process()
  292.      * @author Piotr Klaban <makler@man.torun.pl>
  293.      */
  294.     function ean14($ean)
  295.     {
  296.         static $weights_ean14 = array(3,1,3,1,3,1,3,1,3,1,3,1,3);
  297.         return Validate_ISPN::process($ean14$weights_ean141010);
  298.     }
  299.  
  300.     /**
  301.      * Validate a UCC-12 (U.P.C.) ID number
  302.      *
  303.      * This function checks given UCC-12 number used to identify
  304.      * trade items, locations, and special applications (e.g., * coupons)
  305.      * http://www.ean-ucc.org/
  306.      * http://www.uc-council.org/checkdig.htm
  307.      *
  308.      * @param  string  $ucc number (only numeric chars will be considered)
  309.      * @return bool    true if number is valid, otherwise false
  310.      * @access public
  311.      * @see Validate_ISPN::process()
  312.      * @author Piotr Klaban <makler@man.torun.pl>
  313.      */
  314.     function ucc12($ucc)
  315.     {
  316.         static $weights_ucc12 = array(3,1,3,1,3,1,3,1,3,1,3);
  317.         return Validate_ISPN::process($ucc12$weights_ucc121010);
  318.     }
  319.  
  320.     /**
  321.      * Validate a SSCC (Serial Shipping Container Code)
  322.      *
  323.      * This function checks given SSCC number
  324.      * used to identify logistic units.
  325.      * http://www.ean-ucc.org/
  326.      * http://www.uc-council.org/checkdig.htm
  327.      *
  328.      * @param  string  $sscc number (only numeric chars will be considered)
  329.      * @return bool    true if number is valid, otherwise false
  330.      * @access public
  331.      * @see Validate_ISPN::process()
  332.      * @author Piotr Klaban <makler@man.torun.pl>
  333.      */
  334.     function sscc($sscc)
  335.     {
  336.         static $weights_sscc = array(3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3);
  337.         return Validate_ISPN::process($sscc18$weights_sscc1010);
  338.     }
  339.  
  340.     /**
  341.      * Does all the work for EAN8, EAN13, EAN14, UCC12 and SSCC
  342.      * and can be used for as base for similar kind of calculations
  343.      *
  344.      * @param int $data number (only numeric chars will be considered)
  345.      * @param int $lenght required length of number string
  346.      * @param int $modulo (optionsl) number
  347.      * @param int $subtract (optional) numbier
  348.      * @param array $weights holds the weight that will be used in calculations for the validation
  349.      * @return bool    true if number is valid, otherwise false
  350.      * @access public
  351.      * @see Validate::_checkControlNumber()
  352.      */
  353.     function process($data$length&$weights$modulo = 10$subtract = 0)
  354.     {
  355.         //$weights = array(3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3);
  356.         //$weights = array_slice($weights, 0, $length);
  357.  
  358.         $data str_replace(array('-''/'' '"\t""\n")''$data);
  359.  
  360.         // check if this is a digit number and is the right length
  361.         if (!is_numeric($data|| strlen($data!= $length{
  362.             return false;
  363.         }
  364.  
  365.         // Requires base class Validate
  366.         require_once 'Validate.php';
  367.         return Validate::_checkControlNumber($data$weights$modulo$subtract);
  368.     }
  369. }
  370. ?>

Documentation generated on Tue, 27 Mar 2007 19:00:04 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.