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 
45  public static function write( array $data, $header ) {
46  $code = "<?php\n"
47  . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
48  . "return [\n";
49  foreach ( $data as $key => $value ) {
50  $code .= self::encode( $key, $value, 1 );
51  }
52  $code .= "];\n";
53  return $code;
54  }
55 
67  public static function writeClass( array $data, array $layout ) {
68  $code = "<?php\n"
69  . "// " . implode( "\n// ", explode( "\n", $layout['header'] ) ) . "\n"
70  . "\n"
71  . "namespace {$layout['namespace']};\n"
72  . "\n"
73  . "class {$layout['class']} {\n"
74  . "\tpublic const {$layout['const']} = [\n";
75  foreach ( $data as $key => $value ) {
76  $code .= self::encode( $key, $value, 2 );
77  }
78  $code .= "\t];\n}\n";
79  return $code;
80  }
81 
90  private static function encode( $key, $value, $indent ) {
91  $tabs = str_repeat( "\t", $indent );
92  $line = $tabs . var_export( $key, true ) . ' => ';
93  if ( is_array( $value ) ) {
94  $line .= "[\n";
95  foreach ( $value as $subkey => $subvalue ) {
96  $line .= self::encode( $subkey, $subvalue, $indent + 1 );
97  }
98  $line .= "$tabs]";
99  } else {
100  $line .= var_export( $value, true );
101  }
102 
103  $line .= ",\n";
104  return $line;
105  }
106 }
Wikimedia\StaticArrayWriter\write
static write(array $data, $header)
Create a PHP file that returns the array.
Definition: StaticArrayWriter.php:45
Wikimedia\StaticArrayWriter\create
create(array $data, $header='Automatically generated')
Definition: StaticArrayWriter.php:33
$header
$header
Definition: updateCredits.php:37
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:90
$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...
Wikimedia\StaticArrayWriter\writeClass
static writeClass(array $data, array $layout)
Create an PHP class file with the array as a class constant.
Definition: StaticArrayWriter.php:67