1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Validate |
---|
17 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Royalmail.php 20785 2010-01-31 09:43:03Z mikaelkael $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * @see Zend_Validate_Barcode_AdapterAbstract |
---|
24 | */ |
---|
25 | require_once 'Zend/Validate/Barcode/AdapterAbstract.php'; |
---|
26 | |
---|
27 | /** |
---|
28 | * @category Zend |
---|
29 | * @package Zend_Validate |
---|
30 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
31 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
32 | */ |
---|
33 | class Zend_Validate_Barcode_Royalmail extends Zend_Validate_Barcode_AdapterAbstract |
---|
34 | { |
---|
35 | /** |
---|
36 | * Allowed barcode lengths |
---|
37 | * @var integer |
---|
38 | */ |
---|
39 | protected $_length = -1; |
---|
40 | |
---|
41 | /** |
---|
42 | * Allowed barcode characters |
---|
43 | * @var string |
---|
44 | */ |
---|
45 | protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
---|
46 | |
---|
47 | protected $_rows = array( |
---|
48 | '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1, |
---|
49 | '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2, |
---|
50 | 'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3, |
---|
51 | 'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4, |
---|
52 | 'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5, |
---|
53 | 'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0, |
---|
54 | ); |
---|
55 | |
---|
56 | protected $_columns = array( |
---|
57 | '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0, |
---|
58 | '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0, |
---|
59 | 'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0, |
---|
60 | 'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0, |
---|
61 | 'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0, |
---|
62 | 'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0, |
---|
63 | ); |
---|
64 | |
---|
65 | /** |
---|
66 | * Checksum function |
---|
67 | * @var string |
---|
68 | */ |
---|
69 | protected $_checksum = '_royalmail'; |
---|
70 | |
---|
71 | /** |
---|
72 | * Validates the checksum () |
---|
73 | * |
---|
74 | * @param string $value The barcode to validate |
---|
75 | * @return boolean |
---|
76 | */ |
---|
77 | protected function _royalmail($value) |
---|
78 | { |
---|
79 | $checksum = substr($value, -1, 1); |
---|
80 | $values = str_split(substr($value, 0, -1)); |
---|
81 | $rowvalue = 0; |
---|
82 | $colvalue = 0; |
---|
83 | foreach($values as $row) { |
---|
84 | $rowvalue += $this->_rows[$row]; |
---|
85 | $colvalue += $this->_columns[$row]; |
---|
86 | } |
---|
87 | |
---|
88 | $rowvalue %= 6; |
---|
89 | $colvalue %= 6; |
---|
90 | |
---|
91 | $rowchkvalue = array_keys($this->_rows, $rowvalue); |
---|
92 | $colchkvalue = array_keys($this->_columns, $colvalue); |
---|
93 | $chkvalue = current(array_intersect($rowchkvalue, $colchkvalue)); |
---|
94 | if ($chkvalue == $checksum) { |
---|
95 | return true; |
---|
96 | } |
---|
97 | |
---|
98 | return false; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Allows start and stop tag within checked chars |
---|
103 | * |
---|
104 | * @param string $value The barcode to check for allowed characters |
---|
105 | * @return boolean |
---|
106 | */ |
---|
107 | public function checkChars($value) |
---|
108 | { |
---|
109 | if ($value[0] == '(') { |
---|
110 | $value = substr($value, 1); |
---|
111 | |
---|
112 | if ($value[strlen($value) - 1] == ')') { |
---|
113 | $value = substr($value, 0, -1); |
---|
114 | } else { |
---|
115 | return false; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | return parent::checkChars($value); |
---|
120 | } |
---|
121 | } |
---|