TextControl /
textcontrol-reportingcloud
| 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://tinyurl.com/vmbbh6kd for the canonical source repository |
||
| 11 | * @license https://tinyurl.com/3pc9am89 |
||
| 12 | * @copyright © 2023 Text Control GmbH |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace TextControl\ReportingCloud\Stdlib; |
||
| 16 | |||
| 17 | use Riimu\Kit\PHPEncoder\PHPEncoder; |
||
| 18 | |||
| 19 | class ArrayUtils extends AbstractStdlib |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Write the array of passed data to a file as a PHP structure |
||
| 23 | */ |
||
| 24 | 4 | public static function varExportToFile(string $filename, array $values, string $generator = ''): int |
|
| 25 | { |
||
| 26 | 4 | $options = [ |
|
| 27 | 4 | 'array.indent' => 4, |
|
| 28 | 4 | 'array.align' => true, |
|
| 29 | 4 | 'array.omit' => true, |
|
| 30 | 4 | 'array.short' => true, |
|
| 31 | 4 | 'object.format' => 'export', |
|
| 32 | 4 | 'string.utf8' => true, |
|
| 33 | 4 | 'whitespace' => true, |
|
| 34 | 4 | ]; |
|
| 35 | |||
| 36 | 4 | $encoder = new PHPEncoder($options); |
|
| 37 | |||
| 38 | 4 | $format = '<?php'; |
|
| 39 | 4 | $format .= PHP_EOL; |
|
| 40 | 4 | $format .= 'declare(strict_types=1);'; |
|
| 41 | 4 | $format .= PHP_EOL . PHP_EOL; |
|
| 42 | 4 | $format .= '%s'; |
|
| 43 | 4 | $format .= 'return '; |
|
| 44 | 4 | $format .= '%s'; |
|
| 45 | 4 | $format .= ';'; |
|
| 46 | 4 | $format .= PHP_EOL; |
|
| 47 | |||
| 48 | 4 | if ('' === $generator) { |
|
| 49 | 2 | $prefix = ''; |
|
| 50 | } else { |
||
| 51 | 2 | $prefix = sprintf('// File generated by %s.', $generator); |
|
| 52 | 2 | $prefix .= PHP_EOL; |
|
| 53 | 2 | $prefix .= '// Do not edit.'; |
|
| 54 | 2 | $prefix .= PHP_EOL . PHP_EOL; |
|
| 55 | } |
||
| 56 | |||
| 57 | 4 | $buffer = sprintf($format, $prefix, $encoder->encode($values)); |
|
| 58 | |||
| 59 | 4 | $ret = file_put_contents($filename, $buffer); |
|
| 60 | |||
| 61 | 4 | if (!is_int($ret)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 62 | $ret = 0; |
||
| 63 | } |
||
| 64 | |||
| 65 | 4 | return $ret; |
|
| 66 | } |
||
| 67 | } |
||
| 68 |