Issues (5)

src/Writer/XlsxWriterBuffer.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Zhaqq\Xlsx\Writer;
4
5
use Zhaqq\Exception\XlsxException;
6
7
/**
8
 * Class XlsxWriterBuffer.
9
 *
10
 * @author  QiuXiaoYong on 2019-07-02 13:21
11
 */
12
class XlsxWriterBuffer implements WriterBufferInterface
13
{
14
    const BUFFER_SIZE = 8191;
15
16
    /**
17
     * 默认打开标识.
18
     */
19
    const F_OPEN_FLAGS_W = 'w';
20
21
    /**
22
     * 当前文件标识位.
23
     *
24
     * @var bool|resource|null
25
     */
26
    protected $fd = null;
27
28
    /**
29
     * 当前数据大小.
30
     *
31
     * @var string
32
     */
33
    protected $buffer = '';
34
35
    /**
36
     * 检验字符集.
37
     *
38
     * @var bool
39
     */
40
    protected $checkUtf8 = false;
41
42
    /**
43
     * XlsxWriterBuffer constructor.
44
     *
45
     * @param        $filename
46
     * @param string $fdFopenFlags
47
     * @param bool   $checkUtf8
48
     */
49
    public function __construct($filename, $fdFopenFlags = self::F_OPEN_FLAGS_W, $checkUtf8 = false)
50
    {
51
        $this->checkUtf8 = $checkUtf8;
52
        $this->fd = fopen($filename, $fdFopenFlags);
53
        if (false === $this->fd) {
54
            throw new XlsxException("Unable to open $filename for writing.");
55
        }
56
    }
57
58
    /**
59
     * @param $string
60
     *
61
     * @author   QiuXiaoYong on 2019-07-02 14:09
62
     */
63
    public function write($string)
64
    {
65
        $this->buffer .= $string;
66
        if (isset($this->buffer[self::BUFFER_SIZE])) {
67
            $this->purge();
68
        }
69
    }
70
71
    /**
72
     * @author   QiuXiaoYong on 2019-07-02 14:09
73
     */
74
    protected function purge()
75
    {
76
        if ($this->fd) {
77
            if ($this->checkUtf8 && !self::isValidUTF8($this->buffer)) {
78
                $this->checkUtf8 = false;
79
80
                throw new XlsxException('Error, invalid UTF8 encoding detected.');
81
            }
82
83
            fwrite(/* @scrutinizer ignore-type */$this->fd, $this->buffer);
84
            $this->buffer = '';
85
        }
86
    }
87
88
    /**
89
     * @author   QiuXiaoYong on 2019-07-02 14:15
90
     */
91
    public function close()
92
    {
93
        $this->purge();
94
        if ($this->fd) {
95
            fclose(/* @scrutinizer ignore-type */$this->fd);
96
            $this->fd = null;
97
        }
98
    }
99
100
    /**
101
     * @return bool|int
102
     *
103
     * @author   QiuXiaoYong on 2019-07-02 14:10
104
     */
105
    public function ftell()
106
    {
107
        if ($this->fd) {
108
            $this->purge();
109
110
            return ftell($this->fd);
0 ignored issues
show
It seems like $this->fd can also be of type true; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            return ftell(/** @scrutinizer ignore-type */ $this->fd);
Loading history...
111
        }
112
113
        return -1;
114
    }
115
116
    /**
117
     * @param $pos
118
     *
119
     * @return int
120
     *
121
     * @author   QiuXiaoYong on 2019-07-02 14:16
122
     */
123
    public function fseek($pos)
124
    {
125
        if ($this->fd) {
126
            $this->purge();
127
128
            return fseek(/* @scrutinizer ignore-type */$this->fd, $pos);
129
        }
130
131
        return -1;
132
    }
133
134
    /**
135
     * 关闭文件.
136
     */
137
    public function __destruct()
138
    {
139
        $this->close();
140
    }
141
142
    /**
143
     * @param $string
144
     *
145
     * @return bool
146
     *
147
     * @author   QiuXiaoYong on 2019-07-02 14:16
148
     */
149
    protected static function isValidUTF8($string)
150
    {
151
        if (function_exists('mb_check_encoding')) {
152
            return mb_check_encoding($string, 'UTF-8') ? true : false;
153
        }
154
155
        return preg_match('//u', $string) ? true : false;
156
    }
157
}
158