X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FStaticArrayWriter.php;h=1e0e1dc9ce97f0a96f3725273867e4036f108412;hb=520b167979260edf2cee41842e526ec985c37f92;hp=cd1860ffe4db7065cb0ddd8d1f1cce6f272b41e0;hpb=5410cfccce9b0b84bc909506c9b66d3f27d6dba7;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/StaticArrayWriter.php b/includes/libs/StaticArrayWriter.php index cd1860ffe4..1e0e1dc9ce 100644 --- a/includes/libs/StaticArrayWriter.php +++ b/includes/libs/StaticArrayWriter.php @@ -26,24 +26,47 @@ namespace Wikimedia; class StaticArrayWriter { /** - * @param string[] $data Array with string keys/values to export + * @param array $data Array with keys/values to export * @param string $header * * @return string PHP code */ public function create( array $data, $header = 'Automatically generated' ) { - $format = "\t%s => %s,\n"; $code = " $value ) { - $code .= sprintf( - $format, - var_export( $key, true ), - var_export( $value, true ) - ); + $code .= $this->encode( $key, $value, 1 ); } $code .= "];\n"; return $code; } + + /** + * Recursively turn one k/v pair into properly-indented PHP + * + * @param string|int $key + * @param array|mixed $value + * @param int $indent Indentation level + * + * @return string + */ + private function encode( $key, $value, $indent ) { + $tabs = str_repeat( "\t", $indent ); + $line = $tabs . + var_export( $key, true ) . + ' => '; + if ( is_array( $value ) ) { + $line .= "[\n"; + foreach ( $value as $key2 => $value2 ) { + $line .= $this->encode( $key2, $value2, $indent + 1 ); + } + $line .= "$tabs]"; + } else { + $line .= var_export( $value, true ); + } + + $line .= ",\n"; + return $line; + } }