Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
57.45% |
27 / 47 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
PhpFileWriter | |
57.45% |
27 / 47 |
|
25.00% |
1 / 4 |
59.29 | |
0.00% |
0 / 1 |
savephp | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
12.05 | |||
makePrettyArrayOuts | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
formatKey | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isAssoc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | /** |
6 | * Simple formatter to produce PHP files from input arrays. |
7 | * |
8 | * @author Niklas Laxström |
9 | * @author Ryan Kaldari |
10 | * @author Santhosh Thottingal |
11 | * @author Sam Reed |
12 | * @copyright Copyright © 2007-2015 |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class PhpFileWriter { |
16 | |
17 | /** |
18 | * savephp will build and return a string containing properly formatted php |
19 | * output of all the vars we've just parsed out of the xml. |
20 | * @param array $data The variable names and values we want defined in the php output |
21 | * @param string $location File location to write |
22 | */ |
23 | public function savephp( $data, $location ) { |
24 | $hasData = false; |
25 | foreach ( $data as $v ) { |
26 | if ( count( $v ) ) { |
27 | $hasData = true; |
28 | break; |
29 | } |
30 | } |
31 | |
32 | if ( !$hasData ) { |
33 | return; |
34 | } |
35 | |
36 | // Yes, I am aware I could have simply used var_export. |
37 | // ...the spacing was ugly. |
38 | $output = "<?php\n// This file is generated by rebuild.php. Do not edit it directly.\n"; |
39 | foreach ( $data as $varname => $values ) { |
40 | if ( !count( $values ) ) { |
41 | // Don't output empty arrays |
42 | continue; |
43 | } |
44 | $output .= "\n\$$varname = [\n"; |
45 | if ( $this->isAssoc( $values ) ) { |
46 | foreach ( $values as $key => $value ) { |
47 | if ( is_array( $value ) ) { |
48 | $output .= $this->makePrettyArrayOuts( $key, $value, 1 ); |
49 | } else { |
50 | $key = addcslashes( $key, "'" ); |
51 | $value = addcslashes( $value, "'" ); |
52 | if ( !is_numeric( $key ) ) { |
53 | $key = "'$key'"; |
54 | } |
55 | $output .= "\t$key => '$value',\n"; |
56 | } |
57 | } |
58 | } else { |
59 | foreach ( $values as $value ) { |
60 | if ( is_array( $value ) ) { |
61 | $output .= $this->makePrettyArrayOuts( null, $value, 1 ); |
62 | } else { |
63 | $value = addcslashes( $value, "'" ); |
64 | $output .= "\t'$value',\n"; |
65 | } |
66 | } |
67 | } |
68 | $output .= "];\n"; |
69 | } |
70 | |
71 | file_put_contents( $location, $output ); |
72 | } |
73 | |
74 | /** |
75 | * It makes pretty array vals. Dur. |
76 | * @param string|null $key Use null to omit outputting the key |
77 | * @param array $value |
78 | * @param int $level |
79 | * @return string |
80 | */ |
81 | protected function makePrettyArrayOuts( $key, $value, $level = 1 ) { |
82 | $subKeys = ''; |
83 | $isAssoc = $this->isAssoc( $value ); |
84 | $tabs = str_repeat( "\t", $level ); |
85 | |
86 | foreach ( $value as $subkey => $subvalue ) { |
87 | $subkey = $isAssoc ? $subkey : null; |
88 | |
89 | if ( is_array( $subvalue ) ) { |
90 | $subKeys .= $this->makePrettyArrayOuts( $subkey, $subvalue, $level + 1 ); |
91 | } else { |
92 | $subkey = $isAssoc ? $this->formatKey( $subkey ) : ''; |
93 | $subvalue = addcslashes( $subvalue, "'" ); |
94 | $subKeys .= "$tabs\t$subkey'$subvalue',\n"; |
95 | } |
96 | } |
97 | |
98 | if ( $subKeys === '' ) { |
99 | return ''; |
100 | } |
101 | |
102 | $key = $key !== null ? $this->formatKey( $key ) : ''; |
103 | return "$tabs$key" . "[\n$subKeys$tabs],\n"; |
104 | } |
105 | |
106 | /** |
107 | * It makes pretty array keys. Dur. |
108 | * @param string $key |
109 | * @return string |
110 | */ |
111 | protected function formatKey( $key ) { |
112 | $key = addcslashes( $key, "'" ); |
113 | if ( !is_numeric( $key ) ) { |
114 | $key = "'$key'"; |
115 | } |
116 | |
117 | return "$key => "; |
118 | } |
119 | |
120 | /** |
121 | * Checks if array is associative or sequential. |
122 | * |
123 | * @param array $arr |
124 | * @return bool |
125 | */ |
126 | protected function isAssoc( array $arr ) { |
127 | return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
128 | } |
129 | } |