|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ReportingCloud PHP Wrapper |
|
6
|
|
|
* |
|
7
|
|
|
* PHP wrapper 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
|
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
|
12
|
|
|
* @copyright © 2019 Text Control GmbH |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Stdlib; |
|
16
|
|
|
|
|
17
|
|
|
use Riimu\Kit\PHPEncoder\PHPEncoder; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ArrayUtils |
|
21
|
|
|
* |
|
22
|
|
|
* @package TxTextControl\ReportingCloud |
|
23
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
|
24
|
|
|
*/ |
|
25
|
|
|
class ArrayUtils extends AbstractStdlib |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Write the array of passed data to a file as a PHP structure |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $filename |
|
31
|
|
|
* @param array $values |
|
32
|
|
|
* @param string|null $generator |
|
33
|
|
|
* |
|
34
|
|
|
* @return int |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public static function varExportToFile(string $filename, array $values, ?string $generator = null): int |
|
37
|
|
|
{ |
|
38
|
|
|
$options = [ |
|
39
|
3 |
|
'array.indent' => 4, |
|
40
|
|
|
'array.align' => true, |
|
41
|
|
|
'array.omit' => true, |
|
42
|
|
|
'array.short' => true, |
|
43
|
|
|
'object.format' => 'export', |
|
44
|
|
|
'string.utf8' => true, |
|
45
|
|
|
'whitespace' => true, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
3 |
|
$encoder = new PHPEncoder($options); |
|
49
|
|
|
|
|
50
|
3 |
|
$format = '<?php'; |
|
51
|
3 |
|
$format .= PHP_EOL; |
|
52
|
3 |
|
$format .= 'declare(strict_types=1);'; |
|
53
|
3 |
|
$format .= PHP_EOL . PHP_EOL; |
|
54
|
3 |
|
$format .= '%s'; |
|
55
|
3 |
|
$format .= 'return '; |
|
56
|
3 |
|
$format .= '%s'; |
|
57
|
3 |
|
$format .= ';'; |
|
58
|
3 |
|
$format .= PHP_EOL; |
|
59
|
|
|
|
|
60
|
3 |
|
if (empty($generator)) { |
|
61
|
3 |
|
$prefix = ''; |
|
62
|
|
|
} else { |
|
63
|
|
|
$prefix = sprintf('// File generated by %s.', $generator); |
|
64
|
|
|
$prefix .= PHP_EOL; |
|
65
|
|
|
$prefix .= '// Do not edit.'; |
|
66
|
|
|
$prefix .= PHP_EOL . PHP_EOL; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
$buffer = sprintf($format, $prefix, $encoder->encode($values)); |
|
70
|
|
|
|
|
71
|
3 |
|
return (int) file_put_contents($filename, $buffer); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|