Completed
Pull Request — master (#187)
by Marie
03:22
created

GlobalFunctionsHelper::stream_get_line()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Box\Spout\Common\Helper;
4
5
/**
6
 * Class GlobalFunctionsHelper
7
 * This class wraps global functions to facilitate testing
8
 *
9
 * @codeCoverageIgnore
10
 *
11
 * @package Box\Spout\Common\Helper
12
 */
13
class GlobalFunctionsHelper
14
{
15
    /**
16
     * Wrapper around global function fopen()
17
     * @see fopen()
18
     *
19
     * @param string $fileName
20
     * @param string $mode
21
     * @return resource|bool
22
     */
23
    public function fopen($fileName, $mode)
24
    {
25
        return fopen($fileName, $mode);
26
    }
27
28
    /**
29
     * Wrapper around global function fgets()
30
     * @see fgets()
31
     *
32
     * @param resource $handle
33
     * @param int|void $length
34
     * @return string
35
     */
36
    public function fgets($handle, $length = null)
37
    {
38
        return fgets($handle, $length);
39
    }
40
41
    /**
42
     * Wrapper around global function fputs()
43
     * @see fputs()
44
     *
45
     * @param resource $handle
46
     * @param string $string
47
     * @return int
48
     */
49
    public function fputs($handle, $string)
50
    {
51
        return fputs($handle, $string);
52
    }
53
54
    /**
55
     * Wrapper around global function fflush()
56
     * @see fflush()
57
     *
58
     * @param resource $handle
59
     * @return bool
60
     */
61
    public function fflush($handle)
62
    {
63
        return fflush($handle);
64
    }
65
66
    /**
67
     * Wrapper around global function fseek()
68
     * @see fseek()
69
     *
70
     * @param resource $handle
71
     * @param int $offset
72
     * @return int
73
     */
74
    public function fseek($handle, $offset)
75
    {
76
        return fseek($handle, $offset);
77
    }
78
79
    /**
80
     * Wrapper around global function fgetcsv()
81
     * @see fgetcsv()
82
     *
83
     * @param resource $handle
84
     * @param int|void $length
85
     * @param string|void $delimiter
86
     * @param string|void $enclosure
87
     * @return array
88
     */
89
    public function fgetcsv($handle, $length = null, $delimiter = null, $enclosure = null)
90
    {
91
        return fgetcsv($handle, $length, $delimiter, $enclosure);
92
    }
93
94
    /**
95
     * Wrapper around global function fputcsv()
96
     * @see fputcsv()
97
     *
98
     * @param resource $handle
99
     * @param array $fields
100
     * @param string|void $delimiter
101
     * @param string|void $enclosure
102
     * @return int
103
     */
104
    public function fputcsv($handle, array $fields, $delimiter = null, $enclosure = null)
105
    {
106
        return fputcsv($handle, $fields, $delimiter, $enclosure);
107
    }
108
109
    /**
110
     * Wrapper around global function fwrite()
111
     * @see fwrite()
112
     *
113
     * @param resource $handle
114
     * @param string $string
115
     * @return int
116
     */
117
    public function fwrite($handle, $string)
118
    {
119
        return fwrite($handle, $string);
120
    }
121
122
    /**
123
     * Wrapper around global function fclose()
124
     * @see fclose()
125
     *
126
     * @param resource $handle
127
     * @return bool
128
     */
129
    public function fclose($handle)
130
    {
131
        return fclose($handle);
132
    }
133
134
    /**
135
     * Wrapper around global function rewind()
136
     * @see rewind()
137
     *
138
     * @param resource $handle
139
     * @return bool
140
     */
141
    public function rewind($handle)
142
    {
143
        return rewind($handle);
144
    }
145
146
    /**
147
     * Wrapper around global function file_exists()
148
     * @see file_exists()
149
     *
150
     * @param string $fileName
151
     * @return bool
152
     */
153
    public function file_exists($fileName)
154
    {
155
        return file_exists($fileName);
156
    }
157
158
    /**
159
     * Wrapper around global function file_get_contents()
160
     * @see file_get_contents()
161
     *
162
     * @param string $filePath
163
     * @return string
164
     */
165
    public function file_get_contents($filePath)
166
    {
167
        $realFilePath = $this->convertToUseRealPath($filePath);
168
        return file_get_contents($realFilePath);
169
    }
170
171
    /**
172
     * Updates the given file path to use a real path.
173
     * This is to avoid issues on some Windows setup.
174
     *
175
     * @param string $filePath File path
176
     * @return string The file path using a real path
177
     */
178
    protected function convertToUseRealPath($filePath)
179
    {
180
        $realFilePath = $filePath;
181
182
        if ($this->isZipStream($filePath)) {
183
            if (preg_match('/zip:\/\/(.*)#(.*)/', $filePath, $matches)) {
184
                $documentPath = $matches[1];
185
                $documentInsideZipPath = $matches[2];
186
                $realFilePath = 'zip://' . realpath($documentPath) . '#' . $documentInsideZipPath;
187
            }
188
        } else {
189
            $realFilePath = realpath($filePath);
190
        }
191
192
        return $realFilePath;
193
    }
194
195
    /**
196
     * Returns whether the given path is a zip stream.
197
     *
198
     * @param string $path Path pointing to a document
199
     * @return bool TRUE if path is a zip stream, FALSE otherwise
200
     */
201
    protected function isZipStream($path)
202
    {
203
        return (strpos($path, 'zip://') === 0);
204
    }
205
206
    /**
207
     * Wrapper around global function feof()
208
     * @see feof()
209
     *
210
     * @param resource
211
     * @return bool
212
     */
213
    public function feof($handle)
214
    {
215
        return feof($handle);
216
    }
217
218
    /**
219
     * Wrapper around global function is_readable()
220
     * @see is_readable()
221
     *
222
     * @param string $fileName
223
     * @return bool
224
     */
225
    public function is_readable($fileName)
226
    {
227
        return is_readable($fileName);
228
    }
229
230
    /**
231
     * Wrapper around global function basename()
232
     * @see basename()
233
     *
234
     * @param string $path
235
     * @param string|void $suffix
236
     * @return string
237
     */
238
    public function basename($path, $suffix = null)
239
    {
240
        return basename($path, $suffix);
241
    }
242
243
    /**
244
     * Wrapper around global function header()
245
     * @see header()
246
     *
247
     * @param string $string
248
     * @return void
249
     */
250
    public function header($string)
251
    {
252
        header($string);
253
    }
254
255
    /**
256
     * Wrapper around global function iconv()
257
     * @see iconv()
258
     *
259
     * @param string $string The string to be converted
260
     * @param string $sourceEncoding The encoding of the source string
261
     * @param string $targetEncoding The encoding the source string should be converted to
262
     * @return string|bool the converted string or FALSE on failure.
263
     */
264
    public function iconv($string, $sourceEncoding, $targetEncoding)
265
    {
266
        return iconv($sourceEncoding, $targetEncoding, $string);
267
    }
268
269
    /**
270
     * Wrapper around global function mb_convert_encoding()
271
     * @see mb_convert_encoding()
272
     *
273
     * @param string $string The string to be converted
274
     * @param string $sourceEncoding The encoding of the source string
275
     * @param string $targetEncoding The encoding the source string should be converted to
276
     * @return string|bool the converted string or FALSE on failure.
277
     */
278
    public function mb_convert_encoding($string, $sourceEncoding, $targetEncoding)
279
    {
280
        return mb_convert_encoding($string, $targetEncoding, $sourceEncoding);
281
    }
282
283
    /**
284
     * Wrapper around global function stream_get_wrappers()
285
     * @see stream_get_wrappers()
286
     *
287
     * @return array
288
     */
289
    public function stream_get_wrappers()
290
    {
291
        return stream_get_wrappers();
292
    }
293
294
    /**
295
     * Wrapper around global function function_exists()
296
     * @see function_exists()
297
     *
298
     * @param string $functionName
299
     * @return bool
300
     */
301
    public function function_exists($functionName)
302
    {
303
        return function_exists($functionName);
304
    }
305
}
306