update v1.0.7.9 R.C.

This is a Release Candidate. We are still testing.
This commit is contained in:
Sujit Prasad
2016-08-03 20:04:36 +05:30
parent 8b6b924d09
commit ffa56a43cb
3830 changed files with 181529 additions and 495353 deletions

View File

@@ -0,0 +1,310 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
abstract class AbstractAdapter implements AdapterInterface
{
/**
* Allowed options for this adapter
* @var array
*/
protected $options = [
'length' => null, // Allowed barcode lengths, integer, array, string
'characters' => null, // Allowed barcode characters
'checksum' => null, // Callback to checksum function
'useChecksum' => true, // Is a checksum value included?, boolean
];
/**
* Checks the length of a barcode
*
* @param string $value The barcode to check for proper length
* @return bool
*/
public function hasValidLength($value)
{
if (!is_string($value)) {
return false;
}
$fixum = strlen($value);
$found = false;
$length = $this->getLength();
if (is_array($length)) {
foreach ($length as $value) {
if ($fixum == $value) {
$found = true;
}
if ($value == -1) {
$found = true;
}
}
} elseif ($fixum == $length) {
$found = true;
} elseif ($length == -1) {
$found = true;
} elseif ($length == 'even') {
$count = $fixum % 2;
$found = (0 == $count);
} elseif ($length == 'odd') {
$count = $fixum % 2;
$found = (1 == $count);
}
return $found;
}
/**
* Checks for allowed characters within the barcode
*
* @param string $value The barcode to check for allowed characters
* @return bool
*/
public function hasValidCharacters($value)
{
if (!is_string($value)) {
return false;
}
$characters = $this->getCharacters();
if ($characters == 128) {
for ($x = 0; $x < 128; ++$x) {
$value = str_replace(chr($x), '', $value);
}
} else {
$chars = str_split($characters);
foreach ($chars as $char) {
$value = str_replace($char, '', $value);
}
}
if (strlen($value) > 0) {
return false;
}
return true;
}
/**
* Validates the checksum
*
* @param string $value The barcode to check the checksum for
* @return bool
*/
public function hasValidChecksum($value)
{
$checksum = $this->getChecksum();
if (!empty($checksum)) {
if (method_exists($this, $checksum)) {
return $this->$checksum($value);
}
}
return false;
}
/**
* Returns the allowed barcode length
*
* @return int|array
*/
public function getLength()
{
return $this->options['length'];
}
/**
* Returns the allowed characters
*
* @return int|string|array
*/
public function getCharacters()
{
return $this->options['characters'];
}
/**
* Returns the checksum function name
*
*/
public function getChecksum()
{
return $this->options['checksum'];
}
/**
* Sets the checksum validation method
*
* @param callable $checksum Checksum method to call
* @return AbstractAdapter
*/
protected function setChecksum($checksum)
{
$this->options['checksum'] = $checksum;
return $this;
}
/**
* Sets the checksum validation, if no value is given, the actual setting is returned
*
* @param bool $check
* @return AbstractAdapter|bool
*/
public function useChecksum($check = null)
{
if ($check === null) {
return $this->options['useChecksum'];
}
$this->options['useChecksum'] = (bool) $check;
return $this;
}
/**
* Sets the length of this barcode
*
* @param int|array $length
* @return AbstractAdapter
*/
protected function setLength($length)
{
$this->options['length'] = $length;
return $this;
}
/**
* Sets the allowed characters of this barcode
*
* @param int $characters
* @return AbstractAdapter
*/
protected function setCharacters($characters)
{
$this->options['characters'] = $characters;
return $this;
}
/**
* Validates the checksum (Modulo 10)
* GTIN implementation factor 3
*
* @param string $value The barcode to validate
* @return bool
*/
protected function gtin($value)
{
$barcode = substr($value, 0, -1);
$sum = 0;
$length = strlen($barcode) - 1;
for ($i = 0; $i <= $length; $i++) {
if (($i % 2) === 0) {
$sum += $barcode[$length - $i] * 3;
} else {
$sum += $barcode[$length - $i];
}
}
$calc = $sum % 10;
$checksum = ($calc === 0) ? 0 : (10 - $calc);
if ($value[$length + 1] != $checksum) {
return false;
}
return true;
}
/**
* Validates the checksum (Modulo 10)
* IDENTCODE implementation factors 9 and 4
*
* @param string $value The barcode to validate
* @return bool
*/
protected function identcode($value)
{
$barcode = substr($value, 0, -1);
$sum = 0;
$length = strlen($value) - 2;
for ($i = 0; $i <= $length; $i++) {
if (($i % 2) === 0) {
$sum += $barcode[$length - $i] * 4;
} else {
$sum += $barcode[$length - $i] * 9;
}
}
$calc = $sum % 10;
$checksum = ($calc === 0) ? 0 : (10 - $calc);
if ($value[$length + 1] != $checksum) {
return false;
}
return true;
}
/**
* Validates the checksum (Modulo 10)
* CODE25 implementation factor 3
*
* @param string $value The barcode to validate
* @return bool
*/
protected function code25($value)
{
$barcode = substr($value, 0, -1);
$sum = 0;
$length = strlen($barcode) - 1;
for ($i = 0; $i <= $length; $i++) {
if (($i % 2) === 0) {
$sum += $barcode[$i] * 3;
} else {
$sum += $barcode[$i];
}
}
$calc = $sum % 10;
$checksum = ($calc === 0) ? 0 : (10 - $calc);
if ($value[$length + 1] != $checksum) {
return false;
}
return true;
}
/**
* Validates the checksum ()
* POSTNET implementation
*
* @param string $value The barcode to validate
* @return bool
*/
protected function postnet($value)
{
$checksum = substr($value, -1, 1);
$values = str_split(substr($value, 0, -1));
$check = 0;
foreach ($values as $row) {
$check += $row;
}
$check %= 10;
$check = 10 - $check;
if ($check == $checksum) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
interface AdapterInterface
{
/**
* Checks the length of a barcode
*
* @param string $value The barcode to check for proper length
* @return bool
*/
public function hasValidLength($value);
/**
* Checks for allowed characters within the barcode
*
* @param string $value The barcode to check for allowed characters
* @return bool
*/
public function hasValidCharacters($value);
/**
* Validates the checksum
*
* @param string $value The barcode to check the checksum for
* @return bool
*/
public function hasValidChecksum($value);
/**
* Returns the allowed barcode length
*
* @return int|array
*/
public function getLength();
/**
* Returns the allowed characters
*
* @return int|string|array
*/
public function getCharacters();
/**
* Returns if barcode uses a checksum
*
* @return bool
*/
public function getChecksum();
/**
* Sets the checksum validation, if no value is given, the actual setting is returned
*
* @param bool $check
* @return AbstractAdapter|bool
*/
public function useChecksum($check = null);
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Codabar extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters('0123456789-$:/.+ABCDTN*E');
$this->useChecksum(false);
}
/**
* Checks for allowed characters
* @see Zend\Validator\Barcode.AbstractAdapter::checkChars()
*/
public function hasValidCharacters($value)
{
if (strpbrk($value, 'ABCD')) {
$first = $value[0];
if (!strpbrk($first, 'ABCD')) {
// Missing start char
return false;
}
$last = substr($value, -1, 1);
if (!strpbrk($last, 'ABCD')) {
// Missing stop char
return false;
}
$value = substr($value, 1, -1);
} elseif (strpbrk($value, 'TN*E')) {
$first = $value[0];
if (!strpbrk($first, 'TN*E')) {
// Missing start char
return false;
}
$last = substr($value, -1, 1);
if (!strpbrk($last, 'TN*E')) {
// Missing stop char
return false;
}
$value = substr($value, 1, -1);
}
$chars = $this->getCharacters();
$this->setCharacters('0123456789-$:/.+');
$result = parent::hasValidCharacters($value);
$this->setCharacters($chars);
return $result;
}
}

View File

@@ -0,0 +1,448 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
use Zend\Stdlib\StringUtils;
use Zend\Stdlib\StringWrapper\StringWrapperInterface;
use Zend\Validator\Exception;
class Code128 extends AbstractAdapter
{
/**
* The used string wrapper used for basic UTF-8 string functions
*
* @var StringWrapperInterface
*/
protected $utf8StringWrapper;
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters([
'A' => [
0 => ' ', 1 => '!', 2 => '"', 3 => '#', 4 => '$', 5 => '%', 6 => '&', 7 => "'",
8 => '(', 9 => ')', 10 => '*', 11 => '+', 12 => ',', 13 => '-', 14 => '.', 15 => '/',
16 => '0', 17 => '1', 18 => '2', 19 => '3', 20 => '4', 21 => '5', 22 => '6', 23 => '7',
24 => '8', 25 => '9', 26 => ':', 27 => ';', 28 => '<', 29 => '=', 30 => '>', 31 => '?',
32 => '@', 33 => 'A', 34 => 'B', 35 => 'C', 36 => 'D', 37 => 'E', 38 => 'F', 39 => 'G',
40 => 'H', 41 => 'I', 42 => 'J', 43 => 'K', 44 => 'L', 45 => 'M', 46 => 'N', 47 => 'O',
48 => 'P', 49 => 'Q', 50 => 'R', 51 => 'S', 52 => 'T', 53 => 'U', 54 => 'V', 55 => 'W',
56 => 'X', 57 => 'Y', 58 => 'Z', 59 => '[', 60 => '\\', 61 => ']', 62 => '^', 63 => '_',
64 =>0x00, 65 =>0x01, 66 =>0x02, 67 =>0x03, 68 =>0x04, 69 =>0x05, 70 =>0x06, 71 =>0x07,
72 =>0x08, 73 =>0x09, 74 =>0x0A, 75 =>0x0B, 76 =>0x0C, 77 =>0x0D, 78 =>0x0E, 79 =>0x0F,
80 =>0x10, 81 =>0x11, 82 =>0x12, 83 =>0x13, 84 =>0x14, 85 =>0x15, 86 =>0x16, 87 =>0x17,
88 =>0x18, 89 =>0x19, 90 =>0x1A, 91 =>0x1B, 92 =>0x1C, 93 =>0x1D, 94 =>0x1E, 95 =>0x1F,
96 => 'Ç', 97 => 'ü', 98 => 'é', 99 => 'â', 100 => 'ä', 101 => 'à', 102 => 'å', 103 => '‡',
104 => 'ˆ', 105 => '‰', 106 => 'Š'],
'B' => [
0 => ' ', 1 => '!', 2 => '"', 3 => '#', 4 => '$', 5 => '%', 6 => '&', 7 => "'",
8 => '(', 9 => ')', 10 => '*', 11 => '+', 12 => ',', 13 => '-', 14 => '.', 15 => '/',
16 => '0', 17 => '1', 18 => '2', 19 => '3', 20 => '4', 21 => '5', 22 => '6', 23 => '7',
24 => '8', 25 => '9', 26 => ':', 27 => ';', 28 => '<', 29 => '=', 30 => '>', 31 => '?',
32 => '@', 33 => 'A', 34 => 'B', 35 => 'C', 36 => 'D', 37 => 'E', 38 => 'F', 39 => 'G',
40 => 'H', 41 => 'I', 42 => 'J', 43 => 'K', 44 => 'L', 45 => 'M', 46 => 'N', 47 => 'O',
48 => 'P', 49 => 'Q', 50 => 'R', 51 => 'S', 52 => 'T', 53 => 'U', 54 => 'V', 55 => 'W',
56 => 'X', 57 => 'Y', 58 => 'Z', 59 => '[', 60 => '\\', 61 => ']', 62 => '^', 63 => '_',
64 => '`', 65 => 'a', 66 => 'b', 67 => 'c', 68 => 'd', 69 => 'e', 70 => 'f', 71 => 'g',
72 => 'h', 73 => 'i', 74 => 'j', 75 => 'k', 76 => 'l', 77 => 'm', 78 => 'n', 79 => 'o',
80 => 'p', 81 => 'q', 82 => 'r', 83 => 's', 84 => 't', 85 => 'u', 86 => 'v', 87 => 'w',
88 => 'x', 89 => 'y', 90 => 'z', 91 => '{', 92 => '|', 93 => '}', 94 => '~', 95 =>0x7F,
96 => 'Ç', 97 => 'ü', 98 => 'é', 99 => 'â', 100 => 'ä', 101 => 'à', 102 => 'å', 103 => '‡',
104 => 'ˆ', 105 => '‰', 106 => 'Š'],
'C' => [
0 => '00', 1 => '01', 2 => '02', 3 => '03', 4 => '04', 5 => '05', 6 => '06', 7 => '07',
8 => '08', 9 => '09', 10 => '10', 11 => '11', 12 => '12', 13 => '13', 14 => '14', 15 => '15',
16 => '16', 17 => '17', 18 => '18', 19 => '19', 20 => '20', 21 => '21', 22 => '22', 23 => '23',
24 => '24', 25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29', 30 => '30', 31 => '31',
32 => '32', 33 => '33', 34 => '34', 35 => '35', 36 => '36', 37 => '37', 38 => '38', 39 => '39',
40 => '40', 41 => '41', 42 => '42', 43 => '43', 44 => '44', 45 => '45', 46 => '46', 47 => '47',
48 => '48', 49 => '49', 50 => '50', 51 => '51', 52 => '52', 53 => '53', 54 => '54', 55 => '55',
56 => '56', 57 => '57', 58 => '58', 59 => '59', 60 => '60', 61 => '61', 62 => '62', 63 => '63',
64 => '64', 65 => '65', 66 => '66', 67 => '67', 68 => '68', 69 => '69', 70 => '70', 71 => '71',
72 => '72', 73 => '73', 74 => '74', 75 => '75', 76 => '76', 77 => '77', 78 => '78', 79 => '79',
80 => '80', 81 => '81', 82 => '82', 83 => '83', 84 => '84', 85 => '85', 86 => '86', 87 => '87',
88 => '88', 89 => '89', 90 => '90', 91 => '91', 92 => '92', 93 => '93', 94 => '94', 95 => '95',
96 => '96', 97 => '97', 98 => '98', 99 => '99', 100 => 'ä', 101 => 'à', 102 => 'å', 103 => '‡',
104 => 'ˆ', 105 => '‰', 106 => 'Š']
]);
$this->setChecksum('code128');
}
public function setUtf8StringWrapper(StringWrapperInterface $utf8StringWrapper)
{
if (!$utf8StringWrapper->isSupported('UTF-8')) {
throw new Exception\InvalidArgumentException(
"The string wrapper needs to support UTF-8 character encoding"
);
}
$this->utf8StringWrapper = $utf8StringWrapper;
}
/**
* Get the string wrapper supporting UTF-8 character encoding
*
* @return StringWrapperInterface
*/
public function getUtf8StringWrapper()
{
if (!$this->utf8StringWrapper) {
$this->utf8StringWrapper = StringUtils::getWrapper('UTF-8');
}
return $this->utf8StringWrapper;
}
/**
* Checks for allowed characters within the barcode
*
* @param string $value The barcode to check for allowed characters
* @return bool
*/
public function hasValidCharacters($value)
{
if (!is_string($value)) {
return false;
}
// get used string wrapper for UTF-8 character encoding
$strWrapper = $this->getUtf8StringWrapper();
// detect starting charset
$set = $this->getCodingSet($value);
$read = $set;
if ($set != '') {
$value = $strWrapper->substr($value, 1, null);
}
// process barcode
while ($value != '') {
$char = $strWrapper->substr($value, 0, 1);
switch ($char) {
// Function definition
case 'Ç':
case 'ü':
case 'å':
break;
// Switch 1 char between A and B
case 'é':
if ($set == 'A') {
$read = 'B';
} elseif ($set == 'B') {
$read = 'A';
}
break;
// Switch to C
case 'â':
$set = 'C';
$read = 'C';
break;
// Switch to B
case 'ä':
$set = 'B';
$read = 'B';
break;
// Switch to A
case 'à':
$set = 'A';
$read = 'A';
break;
// Doubled start character
case '‡':
case 'ˆ':
case '‰':
return false;
// Chars after the stop character
case 'Š':
break 2;
default:
// Does the char exist within the charset to read?
if ($this->ord128($char, $read) == -1) {
return false;
}
break;
}
$value = $strWrapper->substr($value, 1, null);
$read = $set;
}
if (($value != '') && ($strWrapper->strlen($value) != 1)) {
return false;
}
return true;
}
/**
* Validates the checksum ()
*
* @param string $value The barcode to validate
* @return bool
*/
protected function code128($value)
{
$sum = 0;
$pos = 1;
$set = $this->getCodingSet($value);
$read = $set;
$usecheck = $this->useChecksum(null);
$strWrapper = $this->getUtf8StringWrapper();
$char = $strWrapper->substr($value, 0, 1);
if ($char == '‡') {
$sum = 103;
} elseif ($char == 'ˆ') {
$sum = 104;
} elseif ($char == '‰') {
$sum = 105;
} elseif ($usecheck == true) {
// no start value, unable to detect a proper checksum
return false;
}
$value = $strWrapper->substr($value, 1, null);
while ($strWrapper->strpos($value, 'Š') || ($value != '')) {
$char = $strWrapper->substr($value, 0, 1);
if ($read == 'C') {
$char = $strWrapper->substr($value, 0, 2);
}
switch ($char) {
// Function definition
case 'Ç':
case 'ü':
case 'å':
$sum += ($pos * $this->ord128($char, $set));
break;
case 'é':
$sum += ($pos * $this->ord128($char, $set));
if ($set == 'A') {
$read = 'B';
} elseif ($set == 'B') {
$read = 'A';
}
break;
// Switch to C
case 'â':
$sum += ($pos * $this->ord128($char, $set));
$set = 'C';
$read = 'C';
break;
// Switch to B
case 'ä':
$sum += ($pos * $this->ord128($char, $set));
$set = 'B';
$read = 'B';
break;
// Switch to A
case 'à':
$sum += ($pos * $this->ord128($char, $set));
$set = 'A';
$read = 'A';
break;
case '‡':
case 'ˆ':
case '‰':
return false;
break;
default:
// Does the char exist within the charset to read?
if ($this->ord128($char, $read) == -1) {
return false;
}
$sum += ($pos * $this->ord128($char, $set));
break;
}
$value = $strWrapper->substr($value, 1);
++$pos;
if (($strWrapper->strpos($value, 'Š') == 1) && ($strWrapper->strlen($value) == 2)) {
// break by stop and checksum char
break;
}
$read = $set;
}
if (($strWrapper->strpos($value, 'Š') != 1) || ($strWrapper->strlen($value) != 2)) {
// return false if checksum is not readable and true if no startvalue is detected
return (!$usecheck);
}
$mod = $sum % 103;
if ($strWrapper->substr($value, 0, 1) == $this->chr128($mod, $set)) {
return true;
}
return false;
}
/**
* Returns the coding set for a barcode
*
* @param string $value Barcode
* @return string
*/
protected function getCodingSet($value)
{
$value = $this->getUtf8StringWrapper()->substr($value, 0, 1);
switch ($value) {
case '‡':
return 'A';
break;
case 'ˆ':
return 'B';
break;
case '‰':
return 'C';
break;
}
return '';
}
/**
* Internal method to return the code128 integer from an ascii value
*
* Table A
* ASCII CODE128
* 32 to 95 == 0 to 63
* 0 to 31 == 64 to 95
* 128 to 138 == 96 to 106
*
* Table B
* ASCII CODE128
* 32 to 138 == 0 to 106
*
* Table C
* ASCII CODE128
* "00" to "99" == 0 to 99
* 132 to 138 == 100 to 106
*
* @param string $value
* @param string $set
* @return int
*/
protected function ord128($value, $set)
{
$ord = ord($value);
if ($set == 'A') {
if ($ord < 32) {
return ($ord + 64);
} elseif ($ord < 96) {
return ($ord - 32);
} elseif ($ord > 138) {
return -1;
} else {
return ($ord - 32);
}
} elseif ($set == 'B') {
if ($ord < 32) {
return -1;
} elseif ($ord <= 138) {
return ($ord - 32);
} else {
return -1;
}
} elseif ($set == 'C') {
$val = (int) $value;
if (($val >= 0) && ($val <= 99)) {
return $val;
} elseif (($ord >= 132) && ($ord <= 138)) {
return ($ord - 32);
} else {
return -1;
}
} else {
if ($ord < 32) {
return ($ord +64);
} elseif ($ord <= 138) {
return ($ord - 32);
} else {
return -1;
}
}
}
/**
* Internal Method to return the ascii value from a code128 integer
*
* Table A
* ASCII CODE128
* 32 to 95 == 0 to 63
* 0 to 31 == 64 to 95
* 128 to 138 == 96 to 106
*
* Table B
* ASCII CODE128
* 32 to 138 == 0 to 106
*
* Table C
* ASCII CODE128
* "00" to "99" == 0 to 99
* 132 to 138 == 100 to 106
*
* @param int $value
* @param string $set
* @return string
*/
protected function chr128($value, $set)
{
if ($set == 'A') {
if ($value < 64) {
return chr($value + 32);
} elseif ($value < 96) {
return chr($value - 64);
} elseif ($value > 106) {
return -1;
} else {
return chr($value + 32);
}
} elseif ($set == 'B') {
if ($value > 106) {
return -1;
} else {
return chr($value + 32);
}
} elseif ($set == 'C') {
if (($value >= 0) && ($value <= 9)) {
return "0" . (string) $value;
} elseif ($value <= 99) {
return (string) $value;
} elseif ($value <= 106) {
return chr($value + 32);
} else {
return -1;
}
} else {
if ($value <= 106) {
return ($value + 32);
} else {
return -1;
}
}
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code25 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters('0123456789');
$this->setChecksum('code25');
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code25interleaved extends AbstractAdapter
{
/**
* Constructor
*
* Sets check flag to false.
*/
public function __construct()
{
$this->setLength('even');
$this->setCharacters('0123456789');
$this->setChecksum('code25');
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code39 extends AbstractAdapter
{
/**
* @var array
*/
protected $check = [
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6,
'7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
'%' => 42,
];
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%');
$this->setChecksum('code39');
$this->useChecksum(false);
}
/**
* Validates the checksum (Modulo 43)
*
* @param string $value The barcode to validate
* @return bool
*/
protected function code39($value)
{
$checksum = substr($value, -1, 1);
$value = str_split(substr($value, 0, -1));
$count = 0;
foreach ($value as $char) {
$count += $this->check[$char];
}
$mod = $count % 43;
if ($mod == $this->check[$checksum]) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code39ext extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters(128);
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code93 extends AbstractAdapter
{
/**
* Note that the characters !"§& are only synonyms
* @var array
*/
protected $check = [
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6,
'7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
'%' => 42, '!' => 43, '"' => 44, '§' => 45, '&' => 46,
];
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%');
$this->setChecksum('code93');
$this->useChecksum(false);
}
/**
* Validates the checksum (Modulo CK)
*
* @param string $value The barcode to validate
* @return bool
*/
protected function code93($value)
{
$checksum = substr($value, -2, 2);
$value = str_split(substr($value, 0, -2));
$count = 0;
$length = count($value) % 20;
foreach ($value as $char) {
if ($length == 0) {
$length = 20;
}
$count += $this->check[$char] * $length;
--$length;
}
$check = array_search(($count % 47), $this->check);
$value[] = $check;
$count = 0;
$length = count($value) % 15;
foreach ($value as $char) {
if ($length == 0) {
$length = 15;
}
$count += $this->check[$char] * $length;
--$length;
}
$check .= array_search(($count % 47), $this->check);
if ($check == $checksum) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Code93ext extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters(128);
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean12 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(12);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean13 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(13);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean14 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(14);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean18 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(18);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean2 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(2);
$this->setCharacters('0123456789');
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean5 extends AbstractAdapter
{
/**
* Constructor
*
* Sets check flag to false.
*/
public function __construct()
{
$this->setLength(5);
$this->setCharacters('0123456789');
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Ean8 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength([7, 8]);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
/**
* Overrides parent checkLength
*
* @param string $value Value
* @return bool
*/
public function hasValidLength($value)
{
if (strlen($value) == 7) {
$this->useChecksum(false);
} else {
$this->useChecksum(true);
}
return parent::hasValidLength($value);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Gtin12 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(12);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Gtin13 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(13);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Gtin14 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(14);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Identcode extends AbstractAdapter
{
/**
* Allowed barcode lengths
* @var int
*/
protected $length = 12;
/**
* Allowed barcode characters
* @var string
*/
protected $characters = '0123456789';
/**
* Checksum function
* @var string
*/
protected $checksum = 'identcode';
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(12);
$this->setCharacters('0123456789');
$this->setChecksum('identcode');
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Intelligentmail extends AbstractAdapter
{
/**
* Constructor
*
* Sets check flag to false.
*/
public function __construct()
{
$this->setLength([20, 25, 29, 31]);
$this->setCharacters('0123456789');
$this->useChecksum(false);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Issn extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength([8, 13]);
$this->setCharacters('0123456789X');
$this->setChecksum('gtin');
}
/**
* Allows X on length of 8 chars
*
* @param string $value The barcode to check for allowed characters
* @return bool
*/
public function hasValidCharacters($value)
{
if (strlen($value) != 8) {
if (strpos($value, 'X') !== false) {
return false;
}
}
return parent::hasValidCharacters($value);
}
/**
* Validates the checksum
*
* @param string $value The barcode to check the checksum for
* @return bool
*/
public function hasValidChecksum($value)
{
if (strlen($value) == 8) {
$this->setChecksum('issn');
} else {
$this->setChecksum('gtin');
}
return parent::hasValidChecksum($value);
}
/**
* Validates the checksum ()
* ISSN implementation (reversed mod11)
*
* @param string $value The barcode to validate
* @return bool
*/
protected function issn($value)
{
$checksum = substr($value, -1, 1);
$values = str_split(substr($value, 0, -1));
$check = 0;
$multi = 8;
foreach ($values as $token) {
if ($token == 'X') {
$token = 10;
}
$check += ($token * $multi);
--$multi;
}
$check %= 11;
$check = ($check === 0 ? 0 : (11 - $check));
if ($check == $checksum) {
return true;
} elseif (($check == 10) && ($checksum == 'X')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Itf14 extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(14);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Leitcode extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(14);
$this->setCharacters('0123456789');
$this->setChecksum('identcode');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Planet extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength([12, 14]);
$this->setCharacters('0123456789');
$this->setChecksum('postnet');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Postnet extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength([6, 7, 10, 12]);
$this->setCharacters('0123456789');
$this->setChecksum('postnet');
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Royalmail extends AbstractAdapter
{
protected $rows = [
'0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
'6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
];
protected $columns = [
'0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
'6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
];
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(-1);
$this->setCharacters('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$this->setChecksum('royalmail');
}
/**
* Validates the checksum ()
*
* @param string $value The barcode to validate
* @return bool
*/
protected function royalmail($value)
{
$checksum = substr($value, -1, 1);
$values = str_split(substr($value, 0, -1));
$rowvalue = 0;
$colvalue = 0;
foreach ($values as $row) {
$rowvalue += $this->rows[$row];
$colvalue += $this->columns[$row];
}
$rowvalue %= 6;
$colvalue %= 6;
$rowchkvalue = array_keys($this->rows, $rowvalue);
$colchkvalue = array_keys($this->columns, $colvalue);
$intersect = array_intersect($rowchkvalue, $colchkvalue);
$chkvalue = current($intersect);
if ($chkvalue == $checksum) {
return true;
}
return false;
}
/**
* Allows start and stop tag within checked chars
*
* @param string $value The barcode to check for allowed characters
* @return bool
*/
public function hasValidCharacters($value)
{
if ($value[0] == '(') {
$value = substr($value, 1);
if ($value[strlen($value) - 1] == ')') {
$value = substr($value, 0, -1);
} else {
return false;
}
}
return parent::hasValidCharacters($value);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Sscc extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(18);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Upca extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength(12);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\Barcode;
class Upce extends AbstractAdapter
{
/**
* Constructor for this barcode adapter
*/
public function __construct()
{
$this->setLength([6, 7, 8]);
$this->setCharacters('0123456789');
$this->setChecksum('gtin');
}
/**
* Overrides parent checkLength
*
* @param string $value Value
* @return bool
*/
public function hasValidLength($value)
{
if (strlen($value) != 8) {
$this->useChecksum(false);
} else {
$this->useChecksum(true);
}
return parent::hasValidLength($value);
}
}