Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.30% |
36 / 37 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
StaticArrayWriter | |
97.30% |
36 / 37 |
|
85.71% |
6 / 7 |
12 | |
0.00% |
0 / 1 |
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
write | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
writeClass | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
encodeArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
encodePair | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
encodeItem | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
encodeValue | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
16 | * |
17 | */ |
18 | |
19 | namespace Wikimedia; |
20 | |
21 | /** |
22 | * Format a static PHP array to be written to a file |
23 | * |
24 | * @newable |
25 | * @since 1.32 |
26 | */ |
27 | class StaticArrayWriter { |
28 | /** |
29 | * @param array $data Array with keys/values to export |
30 | * @param string $header |
31 | * @return string PHP code |
32 | */ |
33 | public function create( array $data, $header = 'Automatically generated' ) { |
34 | return self::write( $data, $header ); |
35 | } |
36 | |
37 | /** |
38 | * Create a PHP file that returns the array. |
39 | * |
40 | * @since 1.35 |
41 | * @param array $data Array with keys/values to export |
42 | * @param string $header |
43 | * @return string PHP code |
44 | */ |
45 | public static function write( array $data, $header ) { |
46 | $code = "<?php\n" |
47 | . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n" |
48 | . "return " . self::encodeArray( $data ) . ";\n"; |
49 | return $code; |
50 | } |
51 | |
52 | /** |
53 | * Create an PHP class file with the array as a class constant. |
54 | * |
55 | * PHP classes can be autoloaded by name, which allows usage to be decoupled |
56 | * from the file path. |
57 | * |
58 | * @since 1.37 |
59 | * @param array $data |
60 | * @param array{header:string,namespace:string,class:string,const:string} $layout |
61 | * @return string PHP code |
62 | */ |
63 | public static function writeClass( array $data, array $layout ) { |
64 | $code = "<?php\n" |
65 | . "// " . implode( "\n// ", explode( "\n", $layout['header'] ) ) . "\n" |
66 | . "\n" |
67 | . "namespace {$layout['namespace']};\n" |
68 | . "\n" |
69 | . "class {$layout['class']} {\n" |
70 | . "\tpublic const {$layout['const']} = " . self::encodeArray( $data, 1 ) . ";\n}\n"; |
71 | return $code; |
72 | } |
73 | |
74 | /** |
75 | * Recursively turn an array into properly-indented PHP |
76 | * |
77 | * @param array $array |
78 | * @param int $indent Indentation level |
79 | * @return string PHP code |
80 | */ |
81 | private static function encodeArray( array $array, $indent = 0 ) { |
82 | $code = "[\n"; |
83 | $tabs = str_repeat( "\t", $indent ); |
84 | if ( array_is_list( $array ) ) { |
85 | foreach ( $array as $item ) { |
86 | $code .= self::encodeItem( $item, $indent + 1 ); |
87 | } |
88 | } else { |
89 | foreach ( $array as $key => $value ) { |
90 | $code .= self::encodePair( $key, $value, $indent + 1 ); |
91 | } |
92 | } |
93 | $code .= "$tabs]"; |
94 | return $code; |
95 | } |
96 | |
97 | /** |
98 | * Recursively turn one k/v pair into properly-indented PHP |
99 | * |
100 | * @param string|int $key |
101 | * @param mixed $value |
102 | * @param int $indent Indentation level |
103 | * @return string PHP code |
104 | */ |
105 | private static function encodePair( $key, $value, $indent = 0 ) { |
106 | $tabs = str_repeat( "\t", $indent ); |
107 | $line = $tabs . var_export( $key, true ) . ' => '; |
108 | $line .= self::encodeValue( $value, $indent ); |
109 | |
110 | $line .= ",\n"; |
111 | return $line; |
112 | } |
113 | |
114 | /** |
115 | * Recursively turn one list item into properly-indented PHP |
116 | * |
117 | * @param mixed $value |
118 | * @param int $indent Indentation level |
119 | * @return string PHP code |
120 | */ |
121 | private static function encodeItem( $value, $indent = 0 ) { |
122 | $tabs = str_repeat( "\t", $indent ); |
123 | $line = $tabs . self::encodeValue( $value, $indent ); |
124 | |
125 | $line .= ",\n"; |
126 | return $line; |
127 | } |
128 | |
129 | /** |
130 | * Recursively turn one value into properly-indented PHP |
131 | * |
132 | * @since 1.38 |
133 | * @param mixed $value |
134 | * @param int $indent Indentation level |
135 | * @return string PHP code |
136 | */ |
137 | public static function encodeValue( $value, $indent = 0 ) { |
138 | if ( is_array( $value ) ) { |
139 | return self::encodeArray( $value, $indent ); |
140 | } else { |
141 | $exportedValue = var_export( $value, true ); |
142 | if ( $exportedValue === 'NULL' ) { |
143 | // var_export() exports nulls as uppercase NULL which |
144 | // violates our own coding standards. |
145 | $exportedValue = 'null'; |
146 | } |
147 | return $exportedValue; |
148 | } |
149 | } |
150 | } |