1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP SDK |
6
|
|
|
* |
7
|
|
|
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://git.io/Jejj2 for the canonical source repository |
11
|
|
|
* @license https://git.io/Jejjr |
12
|
|
|
* @copyright © 2022 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Stdlib; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FileUtils |
19
|
|
|
* |
20
|
|
|
* @package TxTextControl\ReportingCloud |
21
|
|
|
*/ |
22
|
|
|
class FileUtils extends AbstractStdlib |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Read a filename from filesystem and return its binary data. |
26
|
|
|
* Optionally, base64 encode the returned binary data. |
27
|
|
|
* |
28
|
|
|
* @param string $filename |
29
|
|
|
* @param bool $base64Encode |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
58 |
|
public static function read(string $filename, bool $base64Encode = false): string |
34
|
|
|
{ |
35
|
58 |
|
$binaryData = file_get_contents($filename); |
36
|
58 |
|
assert(is_string($binaryData)); |
37
|
|
|
|
38
|
58 |
|
if ($base64Encode) { |
39
|
54 |
|
$binaryData = base64_encode($binaryData); |
40
|
|
|
} |
41
|
|
|
|
42
|
58 |
|
return $binaryData; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Write binary data to a filename on filesystem. |
47
|
|
|
* Optionally, based decode the binary data before writing. |
48
|
|
|
* |
49
|
|
|
* @param string $filename |
50
|
|
|
* @param string $binaryData |
51
|
|
|
* @param bool $base64Encoded |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
4 |
|
public static function write(string $filename, string $binaryData, bool $base64Encoded = false): bool |
56
|
|
|
{ |
57
|
4 |
|
if ($base64Encoded) { |
58
|
2 |
|
$binaryData = base64_decode($binaryData, true); |
59
|
2 |
|
assert(is_string($binaryData)); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$result = file_put_contents($filename, $binaryData); |
63
|
|
|
|
64
|
4 |
|
return is_int($result) && $result > 0; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|