MediaWiki REL1_35
StaticArrayWriter.php
Go to the documentation of this file.
1<?php
19namespace Wikimedia;
20
33 public function create( array $data, $header = 'Automatically generated' ) {
34 return self::write( $data, $header );
35 }
36
43 public static function write( array $data, $header ) {
44 $code = "<?php\n"
45 . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
46 . "return [\n";
47 foreach ( $data as $key => $value ) {
48 $code .= self::encode( $key, $value, 1 );
49 }
50 $code .= "];\n";
51 return $code;
52 }
53
62 private static function encode( $key, $value, $indent ) {
63 $tabs = str_repeat( "\t", $indent );
64 $line = $tabs . var_export( $key, true ) . ' => ';
65 if ( is_array( $value ) ) {
66 $line .= "[\n";
67 foreach ( $value as $subkey => $subvalue ) {
68 $line .= self::encode( $subkey, $subvalue, $indent + 1 );
69 }
70 $line .= "$tabs]";
71 } else {
72 $line .= var_export( $value, true );
73 }
74
75 $line .= ",\n";
76 return $line;
77 }
78}
Format a static PHP array to be written to a file.
create(array $data, $header='Automatically generated')
static write(array $data, $header)
static encode( $key, $value, $indent)
Recursively turn one k/v pair into properly-indented PHP.
$line
Definition mcc.php:119
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$header