Completed
Push — master ( 5c5d67...fe7cae )
by Roberto
31:11 queued 28:45
created

Buffer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 79.63%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 21
c 9
b 0
f 0
lcom 1
cbo 0
dl 0
loc 187
ccs 43
cts 54
cp 0.7963
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataJson() 0 4 1
A __construct() 0 4 1
A __destruct() 0 4 1
A write() 0 4 1
A read() 0 4 1
A close() 0 4 1
A getDataBinary() 0 7 2
A getDataBase64() 0 8 2
A getDataReadable() 0 8 2
A zConvArray() 0 13 4
B friendlyBinary() 0 16 5
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
     * 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)
147
    {
148 1
        return json_encode($this->getDataBinary($retArray));
149
    }
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)
160
    {
161 2
        $ret = $this->zConvArray('R');
162 2
        if (! $retArray) {
163 2
            $ret = implode("\n", $ret);
164 2
        }
165 2
        return $ret;
166
    }
167
    
168
    /**
169
     * Convert buffer content
170
     *
171
     * @param string $type
172
     * @return array
173
     */
174
    protected function zConvArray($type)
175
    {
176
        $ret = array();
177
        foreach ($this->buffer as $data) {
178
            if ($type == 'R') {
179
                $ret[] = $this->friendlyBinary($data);
180
            }
181
            if ($type == 'B') {
182
                $ret[] = base64_encode($data);
183
            }
184
        }
185
        return $ret;
186
    }
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)
196
    {
197
        // Print out binary data with PHP \x00 escape codes,
198
        // for builting test cases.
199 2
        $chars = str_split($input);
200 2
        foreach ($chars as $index => $byte) {
201 2
            $code = ord($byte);
202 2
            $key = array_search($code, $this->ctrlCodes, true);
203 2
            if ($key !== false) {
204 2
                $chars[$index] = $key;
205 2
            } elseif ($code < 32 || $code > 126) {
206 2
                $chars[$index] = "\\x" . bin2hex($byte);
207 2
            }
208 2
        }
209 2
        return implode($chars);
210
    }
211
}
212