1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Posprint\Connectors; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Buffer |
7
|
|
|
* In principle, the entire assembly of RAW commands must be made for this buffer |
8
|
|
|
* that will be used later for sending to the appropriate connector set |
9
|
|
|
* by calling class |
10
|
|
|
* this is necessary to make to enable: |
11
|
|
|
* 1 - debug the assembly of commands |
12
|
|
|
* 2 - allow the use of qz.io for printing by using a browser and a cloud server |
13
|
|
|
* |
14
|
|
|
* @category NFePHP |
15
|
|
|
* @package Posprint |
16
|
|
|
* @author Roberto L. Machado <[email protected]> |
17
|
|
|
* @copyright 2016 Roberto L. Machado |
18
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
19
|
|
|
* @link http://github.com/nfephp-org/posprint |
20
|
|
|
* for the canonical source repository |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use Posprint\Connectors\ConnectorInterface; |
24
|
|
|
|
25
|
|
|
final class Buffer implements ConnectorInterface |
26
|
|
|
{ |
27
|
|
|
private $ctrlCodes = [ |
28
|
|
|
' [NUL] ' => 0, //Nulo |
29
|
|
|
' [EOT] ' => 4, //EOT fim da transmissão |
30
|
|
|
' [ENQ] ' => 5, //ENQ colocar na fila Pedido de status 1 |
31
|
|
|
' [BEL] ' => 7, //BEL sinal sonoro |
32
|
|
|
' [HT] ' => 9, //tabulação horizontal |
33
|
|
|
' [LF] ' => 10, //Inicia a impressão e avança uma linha |
34
|
|
|
' [VT] ' => 11, //tabulação vertical |
35
|
|
|
' [FF] ' => 12, //avança pagina |
36
|
|
|
' [CR] ' => 13, //retorno de carro |
37
|
|
|
' [SO] ' => 14, //SO Inicia modo expandido |
38
|
|
|
' [SI] ' => 15, //Seleciona modo condensado |
39
|
|
|
' [DLE] ' => 16, //Data Link Escape |
40
|
|
|
' [DC1] ' => 17, //DC1 Inicia modo enfatizado |
41
|
|
|
' [DC2] ' => 18, //DC2 Cancela modo condensado |
42
|
|
|
' [DC3] ' => 19, //DC3 Cancela modo enfatizado |
43
|
|
|
' [DC4] ' => 20, //DC4 Controle de dispositivo 4 Inicia modo normal |
44
|
|
|
' [SYN] ' => 22, //Sincronismo |
45
|
|
|
' [CAN] ' => 24, //CAN Cancela linha enviada |
46
|
|
|
' [EM] ' => 25, //Avança 4 linhas |
47
|
|
|
' [ESC] ' => 27, //escape |
48
|
|
|
' [FS] ' => 28, //FS |
49
|
|
|
' [GS] ' => 29, //GS |
50
|
|
|
' [DEL] ' => 127 //Cancela último caracter |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Buffer of accumulated raw data. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $buffer = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
29 |
|
* Create new print connector |
62
|
|
|
* and set $buffer property as empty array |
63
|
29 |
|
*/ |
64
|
29 |
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
$this->buffer = array(); |
67
|
|
|
} |
68
|
|
|
|
69
|
29 |
|
/** |
70
|
|
|
* Destruct print connection |
71
|
29 |
|
*/ |
72
|
29 |
|
public function __destruct() |
73
|
|
|
{ |
74
|
|
|
$this->close(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Send data to buffer porperty |
79
|
24 |
|
* |
80
|
|
|
* @param string $data |
81
|
24 |
|
*/ |
82
|
24 |
|
public function write($data) |
83
|
|
|
{ |
84
|
|
|
$this->buffer[] = $data; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
29 |
|
* Read data form buffer |
89
|
|
|
* |
90
|
29 |
|
* @param int $len |
91
|
29 |
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function read($len = null) |
94
|
|
|
{ |
95
|
|
|
return $this->getDataReadable(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
21 |
|
* Finalize printer connection |
100
|
|
|
* and clear buffer property |
101
|
21 |
|
*/ |
102
|
19 |
|
public function close() |
103
|
|
|
{ |
104
|
2 |
|
$this->buffer = array(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the accumulated raw data that has been sent to this buffer. |
109
|
|
|
* |
110
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
111
|
|
|
* @return string|array |
112
|
|
|
*/ |
113
|
|
|
public function getDataBinary($retArray = true) |
114
|
1 |
|
{ |
115
|
|
|
if (! $retArray) { |
116
|
1 |
|
return implode($this->buffer); |
117
|
1 |
|
} |
118
|
1 |
|
return $this->buffer; |
119
|
1 |
|
} |
120
|
1 |
|
|
121
|
|
|
/** |
122
|
|
|
* Return the data buffer in base64-encoded for transmission over TCP/IP, |
123
|
|
|
* specifically for use qz.io or other similar system. |
124
|
|
|
* |
125
|
|
|
* @param boolean $retArray Enable return as array, otherwise will return a string |
126
|
|
|
* @return array|string |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function getDataBase64($retArray = true) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$lbuff = array(); |
131
|
|
|
foreach ($this->buffer as $linha) { |
132
|
|
|
$lbuff[] = base64_encode($linha); |
133
|
|
|
} |
134
|
1 |
|
if (! $retArray) { |
135
|
|
|
return implode("\n", $lbuff); |
136
|
1 |
|
} |
137
|
|
|
return $lbuff; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the buffer data in JSON format |
142
|
|
|
* for use with the java ajax |
143
|
|
|
* It must be tested because there may be binary data |
144
|
|
|
* that can not travel on GET or POST requests over TCP/IP |
145
|
|
|
* |
146
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
147
|
2 |
|
* @return string |
148
|
|
|
*/ |
149
|
2 |
|
public function getDataJson($retArray = true) |
150
|
2 |
|
{ |
151
|
2 |
|
return json_encode($this->getDataBinary($retArray)); |
152
|
2 |
|
} |
153
|
2 |
|
|
154
|
2 |
|
/** |
155
|
2 |
|
* getDataReadable |
156
|
2 |
|
* Return buffer data converted into a readable string. |
157
|
|
|
* For testing and debbuging only, this format should not be sent to printer |
158
|
|
|
* |
159
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
160
|
|
|
* @return string|array |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function getDataReadable($retArray = true) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$ret = array(); |
165
|
|
|
foreach ($this->buffer as $data) { |
166
|
2 |
|
$ret[] = $this->friendlyBinary($data); |
167
|
|
|
} |
168
|
|
|
if (! $retArray) { |
169
|
|
|
$ret = implode("\n", $ret); |
170
|
2 |
|
} |
171
|
2 |
|
return $ret; |
172
|
2 |
|
} |
173
|
2 |
|
|
174
|
2 |
|
/** |
175
|
2 |
|
* Converts unprintable characters in screen-printing characters |
176
|
2 |
|
* used for debugging purpose only |
177
|
1 |
|
* |
178
|
1 |
|
* @param string $input |
179
|
2 |
|
* @return string |
180
|
2 |
|
*/ |
181
|
|
|
protected function friendlyBinary($input) |
182
|
|
|
{ |
183
|
|
|
// Print out binary data with PHP \x00 escape codes, |
184
|
|
|
// for builting test cases. |
185
|
|
|
$chars = str_split($input); |
186
|
|
|
foreach ($chars as $index => $byte) { |
187
|
|
|
$code = ord($byte); |
188
|
|
|
$key = array_search($code, $this->ctrlCodes, true); |
189
|
|
|
if ($key !== false) { |
190
|
|
|
$chars[$index] = $key; |
191
|
|
|
} elseif ($code < 32 || $code > 126) { |
192
|
|
|
$chars[$index] = "\\x" . bin2hex($byte); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return implode($chars); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.