BIFFwriter   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 12
Bugs 1 Features 3
Metric Value
wmc 16
c 12
b 1
f 3
lcom 1
cbo 0
dl 0
loc 155
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addContinueIfNeeded() 0 8 2
A append() 0 5 1
A appendRaw() 0 9 2
A appendRecord() 0 4 1
A addContinue() 0 4 1
A getDataSize() 0 4 1
A getRecord() 0 6 1
A createRecord() 0 5 1
A isBufferedWriteOn() 0 4 1
A startBufferedWrite() 0 5 1
A endBufferedWrite() 0 4 1
A getBuffer() 0 4 1
A getBufferSize() 0 4 1
A getDataAndFlush() 0 8 1
1
<?php
2
3
namespace Xls;
4
5
use Xls\Record\AbstractRecord;
6
7
class BIFFwriter
8
{
9
    /**
10
     * This flag indicates write to temporary buffer mode
11
     * instead of $data
12
     * @var bool
13
     */
14
    protected $bufferedWrite = false;
15
16
    /**
17
     * Temporary buffer
18
     * @var string
19
     */
20
    protected $buffer = '';
21
22
    /**
23
     * The string containing the data of the BIFF stream
24
     * @var string
25
     */
26
    protected $data = '';
27
28
    /**
29
     * The size of the data in bytes. Should be the same as strlen($this->data)
30
     * But this is not true for Worksheet, cause it writes directly to file
31
     * @var integer
32
     */
33
    protected $datasize = 0;
34
35
    /**
36
     * @param $data
37
     *
38
     * @return string
39
     */
40
    protected function addContinueIfNeeded($data)
41
    {
42
        if (strlen($data) > Biff8::LIMIT) {
43
            $data = $this->addContinue($data);
44
        }
45
46
        return $data;
47
    }
48
49
    /**
50
     * @param string $data binary data to append
51
     */
52
    protected function append($data)
53
    {
54
        $data = $this->addContinueIfNeeded($data);
55
        $this->appendRaw($data);
56
    }
57
58
    /**
59
     * @param string $data binary data to append
60
     */
61
    protected function appendRaw($data)
62
    {
63
        if ($this->isBufferedWriteOn()) {
64
            $this->buffer .= $data;
65
        } else {
66
            $this->data .= $data;
67
            $this->datasize += strlen($data);
68
        }
69
    }
70
71
    /**
72
     * @param string $type
73
     * @param array $params
74
     */
75
    protected function appendRecord($type, array $params = array())
76
    {
77
        $this->append($this->getRecord($type, $params));
78
    }
79
80
    /**
81
     * This function takes a long BIFF record and inserts CONTINUE records as
82
     * necessary.
83
     *
84
     * @param  string $data The original binary data to be written
85
     * @return string Сonvenient string of continue blocks
86
     */
87
    protected function addContinue($data)
88
    {
89
        return $this->getRecord('ContinueRecord', array($data));
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getDataSize()
96
    {
97
        return $this->datasize;
98
    }
99
100
    /**
101
     * @param string $type
102
     * @param array $params
103
     *
104
     * @return mixed
105
     */
106
    protected function getRecord($type, array $params = array())
107
    {
108
        $record = $this->createRecord($type);
109
110
        return call_user_func_array(array($record, 'getData'), $params);
111
    }
112
113
    /**
114
     * @param $type
115
     *
116
     * @return AbstractRecord
117
     */
118
    protected function createRecord($type)
119
    {
120
        $className = "\\Xls\\Record\\$type";
121
        return new $className();
122
    }
123
124
    protected function isBufferedWriteOn()
125
    {
126
        return $this->bufferedWrite;
127
    }
128
129
    protected function startBufferedWrite()
130
    {
131
        $this->bufferedWrite = true;
132
        $this->buffer = '';
133
    }
134
135
    protected function endBufferedWrite()
136
    {
137
        $this->bufferedWrite = false;
138
    }
139
140
    protected function getBuffer()
141
    {
142
        return $this->buffer;
143
    }
144
145
    protected function getBufferSize()
146
    {
147
        return strlen($this->buffer);
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    protected function getDataAndFlush()
154
    {
155
        $data = $this->data;
156
        $this->data = '';
157
        $this->datasize = 0;
158
159
        return $data;
160
    }
161
}
162