MediaWiki  master
StaticArrayWriter.php
Go to the documentation of this file.
1 <?php
19 namespace Wikimedia;
20 
27 
34  public function create( array $data, $header = 'Automatically generated' ) {
35  $code = "<?php\n"
36  . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
37  . "return [\n";
38  foreach ( $data as $key => $value ) {
39  $code .= $this->encode( $key, $value, 1 );
40  }
41  $code .= "];\n";
42  return $code;
43  }
44 
54  private function encode( $key, $value, $indent ) {
55  $tabs = str_repeat( "\t", $indent );
56  $line = $tabs .
57  var_export( $key, true ) .
58  ' => ';
59  if ( is_array( $value ) ) {
60  $line .= "[\n";
61  foreach ( $value as $key2 => $value2 ) {
62  $line .= $this->encode( $key2, $value2, $indent + 1 );
63  }
64  $line .= "$tabs]";
65  } else {
66  $line .= var_export( $value, true );
67  }
68 
69  $line .= ",\n";
70  return $line;
71  }
72 }
encode( $key, $value, $indent)
Recursively turn one k/v pair into properly-indented PHP.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
create(array $data, $header='Automatically generated')
$header
$line
Definition: mcc.php:119
Format a static PHP array to be written to a file.