Issues (19)

src/Stdlib/ArrayUtils.php (1 issue)

Severity
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
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 $generator
33
     *
34
     * @return int
35
     */
36 4
    public static function varExportToFile(string $filename, array $values, string $generator = ''): int
37
    {
38 4
        $options = [
39 4
            'array.indent'  => 4,
40 4
            'array.align'   => true,
41 4
            'array.omit'    => true,
42 4
            'array.short'   => true,
43 4
            'object.format' => 'export',
44 4
            'string.utf8'   => true,
45 4
            'whitespace'    => true,
46 4
        ];
47
48 4
        $encoder = new PHPEncoder($options);
49
50 4
        $format = '<?php';
51 4
        $format .= PHP_EOL;
52 4
        $format .= 'declare(strict_types=1);';
53 4
        $format .= PHP_EOL . PHP_EOL;
54 4
        $format .= '%s';
55 4
        $format .= 'return ';
56 4
        $format .= '%s';
57 4
        $format .= ';';
58 4
        $format .= PHP_EOL;
59
60 4
        if (0 === strlen($generator)) {
61 2
            $prefix = '';
62
        } else {
63 2
            $prefix = sprintf('// File generated by %s.', $generator);
64 2
            $prefix .= PHP_EOL;
65 2
            $prefix .= '// Do not edit.';
66 2
            $prefix .= PHP_EOL . PHP_EOL;
67
        }
68
69 4
        $buffer = sprintf($format, $prefix, $encoder->encode($values));
70
71 4
        $ret = file_put_contents($filename, $buffer);
72
73 4
        if (!is_int($ret)) {
0 ignored issues
show
The condition is_int($ret) is always true.
Loading history...
74
            $ret = 0;
75
        }
76
77 4
        return $ret;
78
    }
79
}
80