|
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: AdapterAbstract.php 20785 2010-01-31 09:43:03Z mikaelkael $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Validate_Barcode_AdapterInterface |
|
24 */ |
|
25 require_once 'Zend/Validate/Barcode/AdapterInterface.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 abstract class Zend_Validate_Barcode_AdapterAbstract |
|
34 implements Zend_Validate_Barcode_AdapterInterface |
|
35 { |
|
36 /** |
|
37 * Allowed barcode lengths |
|
38 * @var integer|array|string |
|
39 */ |
|
40 protected $_length; |
|
41 |
|
42 /** |
|
43 * Allowed barcode characters |
|
44 * @var string |
|
45 */ |
|
46 protected $_characters; |
|
47 |
|
48 /** |
|
49 * Callback to checksum function |
|
50 * @var string|array |
|
51 */ |
|
52 protected $_checksum; |
|
53 |
|
54 /** |
|
55 * Is a checksum value included? |
|
56 * @var boolean |
|
57 */ |
|
58 protected $_hasChecksum = true; |
|
59 |
|
60 /** |
|
61 * Checks the length of a barcode |
|
62 * |
|
63 * @param string $value The barcode to check for proper length |
|
64 * @return boolean |
|
65 */ |
|
66 public function checkLength($value) |
|
67 { |
|
68 if (!is_string($value)) { |
|
69 return false; |
|
70 } |
|
71 |
|
72 $fixum = strlen($value); |
|
73 $found = false; |
|
74 $length = $this->getLength(); |
|
75 if (is_array($length)) { |
|
76 foreach ($length as $value) { |
|
77 if ($fixum == $value) { |
|
78 $found = true; |
|
79 } |
|
80 |
|
81 if ($value == -1) { |
|
82 $found = true; |
|
83 } |
|
84 } |
|
85 } elseif ($fixum == $length) { |
|
86 $found = true; |
|
87 } elseif ($length == -1) { |
|
88 $found = true; |
|
89 } elseif ($length == 'even') { |
|
90 $count = $fixum % 2; |
|
91 $found = ($count == 0) ? true : false; |
|
92 } elseif ($length == 'odd') { |
|
93 $count = $fixum % 2; |
|
94 $found = ($count == 1) ? true : false; |
|
95 } |
|
96 |
|
97 return $found; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Checks for allowed characters within the barcode |
|
102 * |
|
103 * @param string $value The barcode to check for allowed characters |
|
104 * @return boolean |
|
105 */ |
|
106 public function checkChars($value) |
|
107 { |
|
108 if (!is_string($value)) { |
|
109 return false; |
|
110 } |
|
111 |
|
112 $characters = $this->getCharacters(); |
|
113 if ($characters == 128) { |
|
114 for ($x = 0; $x < 128; ++$x) { |
|
115 $value = str_replace(chr($x), '', $value); |
|
116 } |
|
117 } else { |
|
118 $chars = str_split($characters); |
|
119 foreach ($chars as $char) { |
|
120 $value = str_replace($char, '', $value); |
|
121 } |
|
122 } |
|
123 |
|
124 if (strlen($value) > 0) { |
|
125 return false; |
|
126 } |
|
127 |
|
128 return true; |
|
129 } |
|
130 |
|
131 /** |
|
132 * Validates the checksum |
|
133 * |
|
134 * @param string $value The barcode to check the checksum for |
|
135 * @return boolean |
|
136 */ |
|
137 public function checksum($value) |
|
138 { |
|
139 $checksum = $this->getChecksum(); |
|
140 if (!empty($checksum)) { |
|
141 if (method_exists($this, $checksum)) { |
|
142 return call_user_func(array($this, $checksum), $value); |
|
143 } |
|
144 } |
|
145 |
|
146 return false; |
|
147 } |
|
148 |
|
149 /** |
|
150 * Returns the allowed barcode length |
|
151 * |
|
152 * @return string |
|
153 */ |
|
154 public function getLength() |
|
155 { |
|
156 return $this->_length; |
|
157 } |
|
158 |
|
159 /** |
|
160 * Returns the allowed characters |
|
161 * |
|
162 * @return integer|string |
|
163 */ |
|
164 public function getCharacters() |
|
165 { |
|
166 return $this->_characters; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Returns the checksum function name |
|
171 * |
|
172 */ |
|
173 public function getChecksum() |
|
174 { |
|
175 return $this->_checksum; |
|
176 } |
|
177 |
|
178 /** |
|
179 * Returns if barcode uses checksum |
|
180 * |
|
181 * @return boolean |
|
182 */ |
|
183 public function getCheck() |
|
184 { |
|
185 return $this->_hasChecksum; |
|
186 } |
|
187 |
|
188 /** |
|
189 * Sets the checksum validation |
|
190 * |
|
191 * @param boolean $check |
|
192 * @return Zend_Validate_Barcode_AdapterAbstract |
|
193 */ |
|
194 public function setCheck($check) |
|
195 { |
|
196 $this->_hasChecksum = (boolean) $check; |
|
197 return $this; |
|
198 } |
|
199 |
|
200 /** |
|
201 * Validates the checksum (Modulo 10) |
|
202 * GTIN implementation factor 3 |
|
203 * |
|
204 * @param string $value The barcode to validate |
|
205 * @return boolean |
|
206 */ |
|
207 protected function _gtin($value) |
|
208 { |
|
209 $barcode = substr($value, 0, -1); |
|
210 $sum = 0; |
|
211 $length = strlen($barcode) - 1; |
|
212 |
|
213 for ($i = 0; $i <= $length; $i++) { |
|
214 if (($i % 2) === 0) { |
|
215 $sum += $barcode[$length - $i] * 3; |
|
216 } else { |
|
217 $sum += $barcode[$length - $i]; |
|
218 } |
|
219 } |
|
220 |
|
221 $calc = $sum % 10; |
|
222 $checksum = ($calc === 0) ? 0 : (10 - $calc); |
|
223 if ($value[$length + 1] != $checksum) { |
|
224 return false; |
|
225 } |
|
226 |
|
227 return true; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Validates the checksum (Modulo 10) |
|
232 * IDENTCODE implementation factors 9 and 4 |
|
233 * |
|
234 * @param string $value The barcode to validate |
|
235 * @return boolean |
|
236 */ |
|
237 protected function _identcode($value) |
|
238 { |
|
239 $barcode = substr($value, 0, -1); |
|
240 $sum = 0; |
|
241 $length = strlen($value) - 2; |
|
242 |
|
243 for ($i = 0; $i <= $length; $i++) { |
|
244 if (($i % 2) === 0) { |
|
245 $sum += $barcode[$length - $i] * 4; |
|
246 } else { |
|
247 $sum += $barcode[$length - $i] * 9; |
|
248 } |
|
249 } |
|
250 |
|
251 $calc = $sum % 10; |
|
252 $checksum = ($calc === 0) ? 0 : (10 - $calc); |
|
253 if ($value[$length + 1] != $checksum) { |
|
254 return false; |
|
255 } |
|
256 |
|
257 return true; |
|
258 } |
|
259 |
|
260 /** |
|
261 * Validates the checksum (Modulo 10) |
|
262 * CODE25 implementation factor 3 |
|
263 * |
|
264 * @param string $value The barcode to validate |
|
265 * @return boolean |
|
266 */ |
|
267 protected function _code25($value) |
|
268 { |
|
269 $barcode = substr($value, 0, -1); |
|
270 $sum = 0; |
|
271 $length = strlen($barcode) - 1; |
|
272 |
|
273 for ($i = 0; $i <= $length; $i++) { |
|
274 if (($i % 2) === 0) { |
|
275 $sum += $barcode[$i] * 3; |
|
276 } else { |
|
277 $sum += $barcode[$i]; |
|
278 } |
|
279 } |
|
280 |
|
281 $calc = $sum % 10; |
|
282 $checksum = ($calc === 0) ? 0 : (10 - $calc); |
|
283 if ($value[$length + 1] != $checksum) { |
|
284 return false; |
|
285 } |
|
286 |
|
287 return true; |
|
288 } |
|
289 |
|
290 /** |
|
291 * Validates the checksum () |
|
292 * POSTNET implementation |
|
293 * |
|
294 * @param string $value The barcode to validate |
|
295 * @return boolean |
|
296 */ |
|
297 protected function _postnet($value) |
|
298 { |
|
299 $checksum = substr($value, -1, 1); |
|
300 $values = str_split(substr($value, 0, -1)); |
|
301 |
|
302 $check = 0; |
|
303 foreach($values as $row) { |
|
304 $check += $row; |
|
305 } |
|
306 |
|
307 $check %= 10; |
|
308 $check = 10 - $check; |
|
309 if ($check == $checksum) { |
|
310 return true; |
|
311 } |
|
312 |
|
313 return false; |
|
314 } |
|
315 } |