Completed
Pull Request — develop_3.0 (#617)
by
unknown
02:32
created

GlobalFunctionsHelper::ftell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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