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, 1 ) .
";\n}\n";
81 private static function encodeArray( array $array, $indent = 0 ) {
83 $tabs = str_repeat(
"\t", $indent );
84 if ( array_is_list( $array ) ) {
85 foreach ( $array as $item ) {
86 $code .= self::encodeItem( $item, $indent + 1 );
89 foreach ( $array as $key => $value ) {
90 $code .= self::encodePair( $key, $value, $indent + 1 );
105 private static function encodePair( $key, $value, $indent = 0 ) {
106 $tabs = str_repeat(
"\t", $indent );
107 $line = $tabs . var_export( $key,
true ) .
' => ';
108 $line .= self::encodeValue( $value, $indent );
121 private static function encodeItem( $value, $indent = 0 ) {
122 $tabs = str_repeat(
"\t", $indent );
123 $line = $tabs . self::encodeValue( $value, $indent );
138 if ( is_array( $value ) ) {
139 return self::encodeArray( $value, $indent );
141 $exportedValue = var_export( $value,
true );
142 if ( $exportedValue ===
'NULL' ) {
145 $exportedValue =
'null';
147 return $exportedValue;