wiki.techinc.nl/includes/libs/StaticArrayWriter.php
Kunal Mehta 3b1e8a5cef Move wfMakeStaticArrayFile() into a class
And include tests :)

This code is independent of MediaWiki, but not really large enough to be
worth librarizing right now.

Bug: T200626
Change-Id: I022c074e8a708fb5219bc4ff4b53e7e31f60dc4b
2018-08-17 22:48:19 -07:00

49 lines
1.4 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
namespace Wikimedia;
/**
* Format a static PHP array to be written to a file
*
* @since 1.32
*/
class StaticArrayWriter {
/**
* @param string[] $data Array with string keys/values to export
* @param string $header
*
* @return string PHP code
*/
public function create( array $data, $header = 'Automatically generated' ) {
$format = "\t%s => %s,\n";
$code = "<?php\n"
. "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
. "return [\n";
foreach ( $data as $key => $value ) {
$code .= sprintf(
$format,
var_export( $key, true ),
var_export( $value, true )
);
}
$code .= "];\n";
return $code;
}
}