1: <?php
2: /**
3: * InputItem.php
4: *
5: * @since 2015-02-21
6: * @category Library
7: * @package Barcode
8: * @author Nicola Asuni <info@tecnick.com>
9: * @copyright 2010-2016 Nicola Asuni - Tecnick.com LTD
10: * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
11: * @link https://github.com/tecnickcom/tc-lib-barcode
12: *
13: * This file is part of tc-lib-barcode software library.
14: */
15:
16: namespace Com\Tecnick\Barcode\Type\Square\QrCode;
17:
18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
19: use \Com\Tecnick\Barcode\Type\Square\QrCode\Data;
20:
21: /**
22: * Com\Tecnick\Barcode\Type\Square\QrCode\InputItem
23: *
24: * @since 2015-02-21
25: * @category Library
26: * @package Barcode
27: * @author Nicola Asuni <info@tecnick.com>
28: * @copyright 2010-2016 Nicola Asuni - Tecnick.com LTD
29: * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
30: * @link https://github.com/tecnickcom/tc-lib-barcode
31: */
32: abstract class InputItem extends \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
33: {
34: /**
35: * Append data to an input object.
36: * The data is copied and appended to the input object.
37: *
38: * @param array $items Input items
39: * @param int $mode Encoding mode.
40: * @param int $size Size of data (byte).
41: * @param array $data Array of input data.
42: *
43: * @return array items
44: */
45: public function appendNewInputItem($items, $mode, $size, $data)
46: {
47: $newitem = $this->newInputItem($mode, $size, $data);
48: if (!empty($newitem)) {
49: $items[] = $newitem;
50: }
51: return $items;
52: }
53:
54: /**
55: * newInputItem
56: *
57: * @param int $mode Encoding mode.
58: * @param int $size Size of data (byte).
59: * @param array $data Array of input data.
60: * @param array $bstream Binary stream
61: *
62: * @return array input item
63: */
64: protected function newInputItem($mode, $size, $data, $bstream = null)
65: {
66: $setData = array_slice($data, 0, $size);
67: if (count($setData) < $size) {
68: $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
69: }
70: if (!$this->check($mode, $size, $setData)) {
71: throw new BarcodeException('Invalid input item');
72: }
73: return array(
74: 'mode' => $mode,
75: 'size' => $size,
76: 'data' => $setData,
77: 'bstream' => $bstream,
78: );
79: }
80:
81: /**
82: * Validate the input data.
83: *
84: * @param int $mode Encoding mode.
85: * @param int $size Size of data (byte).
86: * @param array $data Data to validate
87: *
88: * @return boolean true in case of valid data, false otherwise
89: */
90: protected function check($mode, $size, $data)
91: {
92: if ($size <= 0) {
93: return false;
94: }
95: switch ($mode) {
96: case Data::$encodingModes['NM']:
97: return $this->checkModeNum($size, $data);
98: case Data::$encodingModes['AN']:
99: return $this->checkModeAn($size, $data);
100: case Data::$encodingModes['KJ']:
101: return $this->checkModeKanji($size, $data);
102: case Data::$encodingModes['8B']:
103: return true;
104: case Data::$encodingModes['ST']:
105: return true;
106: }
107: return false;
108: }
109:
110: /**
111: * checkModeNum
112: *
113: * @param int $size
114: * @param int $data
115: *
116: * @return boolean true or false
117: */
118: protected function checkModeNum($size, $data)
119: {
120: for ($idx = 0; $idx < $size; ++$idx) {
121: if ((ord($data[$idx]) < ord('0')) || (ord($data[$idx]) > ord('9'))) {
122: return false;
123: }
124: }
125: return true;
126: }
127:
128: /**
129: * checkModeAn
130: *
131: * @param int $size
132: * @param int $data
133: *
134: * @return boolean true or false
135: */
136: protected function checkModeAn($size, $data)
137: {
138: for ($idx = 0; $idx < $size; ++$idx) {
139: if ($this->lookAnTable(ord($data[$idx])) == -1) {
140: return false;
141: }
142: }
143: return true;
144: }
145:
146: /**
147: * checkModeKanji
148: *
149: * @param int $size
150: * @param int $data
151: *
152: * @return boolean true or false
153: */
154: protected function checkModeKanji($size, $data)
155: {
156: if ($size & 1) {
157: return false;
158: }
159: for ($idx = 0; $idx < $size; $idx += 2) {
160: $val = (ord($data[$idx]) << 8) | ord($data[($idx + 1)]);
161: if (($val < 0x8140) || (($val > 0x9ffc) && ($val < 0xe040)) || ($val > 0xebbf)) {
162: return false;
163: }
164: }
165: return true;
166: }
167: }
168: