1 | <?php |
||
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 | * Create new print connector |
||
62 | * and set $buffer property as empty array |
||
63 | */ |
||
64 | 28 | public function __construct() |
|
65 | { |
||
66 | 28 | $this->buffer = array(); |
|
67 | 28 | } |
|
68 | |||
69 | /** |
||
70 | * Destruct print connection |
||
71 | */ |
||
72 | 27 | public function __destruct() |
|
73 | { |
||
74 | 27 | $this->close(); |
|
75 | 27 | } |
|
76 | |||
77 | /** |
||
78 | * Send data to buffer porperty |
||
79 | * |
||
80 | * @param string $data |
||
81 | */ |
||
82 | 26 | public function write($data) |
|
83 | { |
||
84 | 26 | $this->buffer[] = $data; |
|
85 | 26 | } |
|
86 | |||
87 | /** |
||
88 | * Read data form buffer |
||
89 | * |
||
90 | * @param int $len |
||
91 | * @return string |
||
92 | */ |
||
93 | 1 | public function read($len = null) |
|
94 | { |
||
95 | 1 | return $this->getDataReadable(false); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Finalize printer connection |
||
100 | * and clear buffer property |
||
101 | */ |
||
102 | 29 | public function close() |
|
103 | { |
||
104 | 29 | $this->buffer = array(); |
|
105 | 29 | } |
|
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 | 21 | public function getDataBinary($retArray = true) |
|
114 | { |
||
115 | 21 | if (! $retArray) { |
|
116 | 20 | return implode($this->buffer); |
|
117 | } |
||
118 | 1 | return $this->buffer; |
|
119 | } |
||
120 | |||
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 | 2 | public function getDataBase64($retArray = true) |
|
129 | { |
||
130 | 2 | $lbuff = $this->zConvArray('B'); |
|
131 | 2 | if (! $retArray) { |
|
132 | 1 | return implode("\n", $lbuff); |
|
133 | } |
||
134 | 1 | return $lbuff; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns the buffer data in JSON format |
||
139 | * for use with the java ajax |
||
140 | * It must be tested because there may be binary data |
||
141 | * that can not travel on GET or POST requests over TCP/IP |
||
142 | * |
||
143 | * @param bool $retArray Enable return as array, otherwise will return a string |
||
144 | * @return string |
||
145 | */ |
||
146 | 1 | public function getDataJson($retArray = true) |
|
150 | |||
151 | /** |
||
152 | * getDataReadable |
||
153 | * Return buffer data converted into a readable string. |
||
154 | * For testing and debbuging only, this format should not be sent to printer |
||
155 | * |
||
156 | * @param bool $retArray Enable return as array, otherwise will return a string |
||
157 | * @return string|array |
||
158 | */ |
||
159 | 2 | public function getDataReadable($retArray = true) |
|
167 | |||
168 | /** |
||
169 | * Convert buffer content |
||
170 | * |
||
171 | * @param string $type |
||
172 | * @return array |
||
173 | */ |
||
174 | protected function zConvArray($type) |
||
187 | |||
188 | /** |
||
189 | * Converts unprintable characters in screen-printing characters |
||
190 | * used for debugging purpose only |
||
191 | * |
||
192 | * @param string $input |
||
193 | * @return string |
||
194 | */ |
||
195 | 2 | protected function friendlyBinary($input) |
|
211 | } |
||
212 |