MediaWiki REL1_37
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
45 public static function write( array $data, $header ) {
46 $code = "<?php\n"
47 . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
48 . "return [\n";
49 foreach ( $data as $key => $value ) {
50 $code .= self::encode( $key, $value, 1 );
51 }
52 $code .= "];\n";
53 return $code;
54 }
55
67 public static function writeClass( array $data, array $layout ) {
68 $code = "<?php\n"
69 . "// " . implode( "\n// ", explode( "\n", $layout['header'] ) ) . "\n"
70 . "\n"
71 . "namespace {$layout['namespace']};\n"
72 . "\n"
73 . "class {$layout['class']} {\n"
74 . "\tpublic const {$layout['const']} = [\n";
75 foreach ( $data as $key => $value ) {
76 $code .= self::encode( $key, $value, 2 );
77 }
78 $code .= "\t];\n}\n";
79 return $code;
80 }
81
90 private static function encode( $key, $value, $indent ) {
91 $tabs = str_repeat( "\t", $indent );
92 $line = $tabs . var_export( $key, true ) . ' => ';
93 if ( is_array( $value ) ) {
94 $line .= "[\n";
95 foreach ( $value as $subkey => $subvalue ) {
96 $line .= self::encode( $subkey, $subvalue, $indent + 1 );
97 }
98 $line .= "$tabs]";
99 } else {
100 $line .= var_export( $value, true );
101 }
102
103 $line .= ",\n";
104 return $line;
105 }
106}
Format a static PHP array to be written to a file.
create(array $data, $header='Automatically generated')
static write(array $data, $header)
Create a PHP file that returns the array.
static encode( $key, $value, $indent)
Recursively turn one k/v pair into properly-indented PHP.
static writeClass(array $data, array $layout)
Create an PHP class file with the array as a class constant.
$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