33 public function create( array $data,
$header =
'Automatically generated' ) {
34 return self::write( $data,
$header );
47 .
"// " . implode(
"\n// ", explode(
"\n",
$header ) ) .
"\n"
48 .
"return " . self::encodeArray( $data ) .
";\n";
63 public static function writeClass( array $data, array $layout ) {
65 .
"// " . implode(
"\n// ", explode(
"\n", $layout[
'header'] ) ) .
"\n"
67 .
"namespace {$layout['namespace']};\n"
69 .
"class {$layout['class']} {\n"
70 .
"\tpublic const {$layout['const']} = " . self::encodeArray( $data,
"\t\t" ) .
";\n}\n";
81 private static function encodeArray( array $array,
string $tabs =
"\t" ): string {
83 if ( array_is_list( $array ) ) {
84 foreach ( $array as $value ) {
85 $code .= $tabs . self::encodeValue( $value, $tabs ) .
",\n";
88 foreach ( $array as $key => $value ) {
89 $code .= $tabs . var_export( $key,
true ) .
' => ' .
90 self::encodeValue( $value, $tabs ) .
",\n";
93 return $code . substr( $tabs, 0, -1 ) .
']';
103 private static function encodeValue( $value,
string $tabs ): string {
104 if ( is_array( $value ) ) {
105 return self::encodeArray( $value, $tabs .
"\t" );
110 return $value ===
null ?
'null' : var_export( $value,
true );