File indexing completed on 2024-12-22 05:37:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Barcode_AdapterAbstract
0024  */
0025 // require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_Barcode_Royalmail extends Zend_Validate_Barcode_AdapterAbstract
0034 {
0035     /**
0036      * Allowed barcode lengths
0037      * @var integer
0038      */
0039     protected $_length = -1;
0040 
0041     /**
0042      * Allowed barcode characters
0043      * @var string
0044      */
0045     protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
0046 
0047     protected $_rows = array(
0048         '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
0049         '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
0050         'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
0051         'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
0052         'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
0053         'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
0054      );
0055 
0056     protected $_columns = array(
0057         '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
0058         '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
0059         'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
0060         'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
0061         'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
0062         'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
0063     );
0064 
0065     /**
0066      * Checksum function
0067      * @var string
0068      */
0069     protected $_checksum = '_royalmail';
0070 
0071     /**
0072      * Validates the checksum ()
0073      *
0074      * @param  string $value The barcode to validate
0075      * @return boolean
0076      */
0077     protected function _royalmail($value)
0078     {
0079         $checksum = substr($value, -1, 1);
0080         $values   = str_split(substr($value, 0, -1));
0081         $rowvalue = 0;
0082         $colvalue = 0;
0083         foreach($values as $row) {
0084             $rowvalue += $this->_rows[$row];
0085             $colvalue += $this->_columns[$row];
0086         }
0087 
0088         $rowvalue %= 6;
0089         $colvalue %= 6;
0090 
0091         $rowchkvalue = array_keys($this->_rows, $rowvalue);
0092         $colchkvalue = array_keys($this->_columns, $colvalue);
0093         $chkvalue    = current(array_intersect($rowchkvalue, $colchkvalue));
0094         if ($chkvalue == $checksum) {
0095             return true;
0096         }
0097 
0098         return false;
0099     }
0100 
0101     /**
0102      * Allows start and stop tag within checked chars
0103      *
0104      * @param  string $value The barcode to check for allowed characters
0105      * @return boolean
0106      */
0107     public function checkChars($value)
0108     {
0109         if ($value[0] == '(') {
0110             $value = substr($value, 1);
0111 
0112             if ($value[strlen($value) - 1] == ')') {
0113                 $value = substr($value, 0, -1);
0114             } else {
0115                 return false;
0116             }
0117         }
0118 
0119         return parent::checkChars($value);
0120     }
0121 }