diff options
author | Timo Tijhof <krinklemail@gmail.com> | 2020-06-16 03:01:38 +0100 |
---|---|---|
committer | Krinkle <krinklemail@gmail.com> | 2020-06-16 02:14:01 +0000 |
commit | 267b895f576b6ebbdb6f323a61d7aff42fcdbf33 (patch) | |
tree | f1b19b453d55c2c38b6b8589924f43c0f046d811 /includes/libs/StaticArrayWriter.php | |
parent | 67247d933f53622990f5b595c4630988cd833b5a (diff) | |
download | mediawikicore-267b895f576b6ebbdb6f323a61d7aff42fcdbf33.tar.gz mediawikicore-267b895f576b6ebbdb6f323a61d7aff42fcdbf33.zip |
StaticArrayWriter: Add a public static write() method
This is a simple utility. I would be worried if this nears
the kind of complexity that warrants constructor parameters
and state.
Allow it to be used without instantiation for simpler use.
Follows-up I022c074e8a708fb (3b1e8a5cef1be91).
Change-Id: I044f423c5219d72bc76bb35ecbba906df0305e49
Diffstat (limited to 'includes/libs/StaticArrayWriter.php')
-rw-r--r-- | includes/libs/StaticArrayWriter.php | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/includes/libs/StaticArrayWriter.php b/includes/libs/StaticArrayWriter.php index 1e0e1dc9ce97..f5613681bc54 100644 --- a/includes/libs/StaticArrayWriter.php +++ b/includes/libs/StaticArrayWriter.php @@ -24,19 +24,27 @@ namespace Wikimedia; * @since 1.32 */ class StaticArrayWriter { - /** * @param array $data Array with keys/values to export * @param string $header - * * @return string PHP code */ public function create( array $data, $header = 'Automatically generated' ) { + return self::write( $data, $header ); + } + + /** + * @since 1.35 + * @param array $data Array with keys/values to export + * @param string $header + * @return string PHP code + */ + public static function write( array $data, $header ) { $code = "<?php\n" . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n" . "return [\n"; foreach ( $data as $key => $value ) { - $code .= $this->encode( $key, $value, 1 ); + $code .= self::encode( $key, $value, 1 ); } $code .= "];\n"; return $code; @@ -46,20 +54,17 @@ class StaticArrayWriter { * Recursively turn one k/v pair into properly-indented PHP * * @param string|int $key - * @param array|mixed $value + * @param mixed $value * @param int $indent Indentation level - * - * @return string + * @return string PHP code */ - private function encode( $key, $value, $indent ) { + private static function encode( $key, $value, $indent ) { $tabs = str_repeat( "\t", $indent ); - $line = $tabs . - var_export( $key, true ) . - ' => '; + $line = $tabs . var_export( $key, true ) . ' => '; if ( is_array( $value ) ) { $line .= "[\n"; - foreach ( $value as $key2 => $value2 ) { - $line .= $this->encode( $key2, $value2, $indent + 1 ); + foreach ( $value as $subkey => $subvalue ) { + $line .= self::encode( $subkey, $subvalue, $indent + 1 ); } $line .= "$tabs]"; } else { |