20 public function create( array $data, $header =
'Automatically generated' ) {
21 return self::write( $data, $header );
32 public static function write( array $data, $header ) {
34 .
"// " . implode(
"\n// ", explode(
"\n", $header ) ) .
"\n"
35 .
"return " . self::encodeArray( $data ) .
";\n";
50 public static function writeClass( array $data, array $layout ) {
52 .
"// " . implode(
"\n// ", explode(
"\n", $layout[
'header'] ) ) .
"\n"
54 .
"namespace {$layout['namespace']};\n"
56 .
"class {$layout['class']} {\n"
57 .
"\tpublic const {$layout['const']} = " . self::encodeArray( $data,
"\t\t" ) .
";\n}\n";
68 private static function encodeArray( array $array,
string $tabs =
"\t" ): string {
70 if ( array_is_list( $array ) ) {
71 foreach ( $array as $value ) {
72 $code .= $tabs . self::encodeValue( $value, $tabs ) .
",\n";
75 foreach ( $array as $key => $value ) {
76 $code .= $tabs . var_export( $key,
true ) .
' => ' .
77 self::encodeValue( $value, $tabs ) .
",\n";
80 return $code . substr( $tabs, 0, -1 ) .
']';
90 private static function encodeValue( $value,
string $tabs ): string {
91 if ( is_array( $value ) ) {
92 return self::encodeArray( $value, $tabs .
"\t" );
97 return $value ===
null ?
'null' : var_export( $value,
true );