| 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 ob_end_clean() |
| 257 |
* @see ob_end_clean() |
| 258 |
* |
| 259 |
* @return void |
| 260 |
*/ |
| 261 |
public function ob_end_clean() |
| 262 |
{ |
| 263 |
if (ob_get_length() > 0) { |
| 264 |
ob_end_clean(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Wrapper around global function iconv() |
| 270 |
* @see iconv() |
| 271 |
* |
| 272 |
* @param string $string The string to be converted |
| 273 |
* @param string $sourceEncoding The encoding of the source string |
| 274 |
* @param string $targetEncoding The encoding the source string should be converted to |
| 275 |
* @return string|bool the converted string or FALSE on failure. |
| 276 |
*/ |
| 277 |
public function iconv($string, $sourceEncoding, $targetEncoding) |
| 278 |
{ |
| 279 |
return iconv($sourceEncoding, $targetEncoding, $string); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Wrapper around global function mb_convert_encoding() |
| 284 |
* @see mb_convert_encoding() |
| 285 |
* |
| 286 |
* @param string $string The string to be converted |
| 287 |
* @param string $sourceEncoding The encoding of the source string |
| 288 |
* @param string $targetEncoding The encoding the source string should be converted to |
| 289 |
* @return string|bool the converted string or FALSE on failure. |
| 290 |
*/ |
| 291 |
public function mb_convert_encoding($string, $sourceEncoding, $targetEncoding) |
| 292 |
{ |
| 293 |
return mb_convert_encoding($string, $targetEncoding, $sourceEncoding); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Wrapper around global function stream_get_wrappers() |
| 298 |
* @see stream_get_wrappers() |
| 299 |
* |
| 300 |
* @return array |
| 301 |
*/ |
| 302 |
public function stream_get_wrappers() |
| 303 |
{ |
| 304 |
return stream_get_wrappers(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Wrapper around global function function_exists() |
| 309 |
* @see function_exists() |
| 310 |
* |
| 311 |
* @param string $functionName |
| 312 |
* @return bool |
| 313 |
*/ |
| 314 |
public function function_exists($functionName) |
| 315 |
{ |
| 316 |
return function_exists($functionName); |
| 317 |
} |
| 318 |
} |
| 319 |
|