Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
StaticArrayWriter
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 writeClass
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 encodeArray
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 encodeValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * @license GPL-2.0-or-later
4 */
5
6namespace Wikimedia;
7
8/**
9 * Format a static PHP array to be written to a file
10 *
11 * @newable
12 * @since 1.32
13 */
14class StaticArrayWriter {
15    /**
16     * @param array $data Array with keys/values to export
17     * @param string $header
18     * @return string PHP code
19     */
20    public function create( array $data, $header = 'Automatically generated' ) {
21        return self::write( $data, $header );
22    }
23
24    /**
25     * Create a PHP file that returns the array.
26     *
27     * @since 1.35
28     * @param array $data Array with keys/values to export
29     * @param string $header
30     * @return string PHP code
31     */
32    public static function write( array $data, $header ) {
33        $code = "<?php\n"
34            . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
35            . "return " . self::encodeArray( $data ) . ";\n";
36        return $code;
37    }
38
39    /**
40     * Create an PHP class file with the array as a class constant.
41     *
42     * PHP classes can be autoloaded by name, which allows usage to be decoupled
43     * from the file path.
44     *
45     * @since 1.37
46     * @param array $data
47     * @param array{header:string,namespace:string,class:string,const:string} $layout
48     * @return string PHP code
49     */
50    public static function writeClass( array $data, array $layout ) {
51        $code = "<?php\n"
52            . "// " . implode( "\n// ", explode( "\n", $layout['header'] ) ) . "\n"
53            . "\n"
54            . "namespace {$layout['namespace']};\n"
55            . "\n"
56            . "class {$layout['class']} {\n"
57            . "\tpublic const {$layout['const']} = " . self::encodeArray( $data, "\t\t" ) . ";\n}\n";
58        return $code;
59    }
60
61    /**
62     * Recursively turn an array into properly-indented PHP
63     *
64     * @param array $array
65     * @param string $tabs Indentation level
66     * @return string PHP code
67     */
68    private static function encodeArray( array $array, string $tabs = "\t" ): string {
69        $code = "[\n";
70        if ( array_is_list( $array ) ) {
71            foreach ( $array as $value ) {
72                $code .= $tabs . self::encodeValue( $value, $tabs ) . ",\n";
73            }
74        } else {
75            foreach ( $array as $key => $value ) {
76                $code .= $tabs . var_export( $key, true ) . ' => ' .
77                    self::encodeValue( $value, $tabs ) . ",\n";
78            }
79        }
80        return $code . substr( $tabs, 0, -1 ) . ']';
81    }
82
83    /**
84     * Recursively turn one value into properly-indented PHP
85     *
86     * @param mixed $value
87     * @param string $tabs Indentation level
88     * @return string PHP code
89     */
90    private static function encodeValue( $value, string $tabs ): string {
91        if ( is_array( $value ) ) {
92            return self::encodeArray( $value, $tabs . "\t" );
93        }
94
95        // var_export() exports nulls as uppercase NULL which
96        // violates our own coding standards.
97        return $value === null ? 'null' : var_export( $value, true );
98    }
99}