MediaWiki  master
StaticArrayWriter.php
Go to the documentation of this file.
1 <?php
19 namespace Wikimedia;
20 
33  public function create( array $data, $header = 'Automatically generated' ) {
34  return self::write( $data, $header );
35  }
36 
43  public static function write( array $data, $header ) {
44  $code = "<?php\n"
45  . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
46  . "return [\n";
47  foreach ( $data as $key => $value ) {
48  $code .= self::encode( $key, $value, 1 );
49  }
50  $code .= "];\n";
51  return $code;
52  }
53 
62  private static function encode( $key, $value, $indent ) {
63  $tabs = str_repeat( "\t", $indent );
64  $line = $tabs . var_export( $key, true ) . ' => ';
65  if ( is_array( $value ) ) {
66  $line .= "[\n";
67  foreach ( $value as $subkey => $subvalue ) {
68  $line .= self::encode( $subkey, $subvalue, $indent + 1 );
69  }
70  $line .= "$tabs]";
71  } else {
72  $line .= var_export( $value, true );
73  }
74 
75  $line .= ",\n";
76  return $line;
77  }
78 }
Wikimedia\StaticArrayWriter\write
static write(array $data, $header)
Definition: StaticArrayWriter.php:43
Wikimedia\StaticArrayWriter\create
create(array $data, $header='Automatically generated')
Definition: StaticArrayWriter.php:33
$header
$header
Definition: updateCredits.php:41
Wikimedia\StaticArrayWriter
Format a static PHP array to be written to a file.
Definition: StaticArrayWriter.php:27
Wikimedia\StaticArrayWriter\encode
static encode( $key, $value, $indent)
Recursively turn one k/v pair into properly-indented PHP.
Definition: StaticArrayWriter.php:62
$line
$line
Definition: mcc.php:119
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...