From 792fd3de0ee3838fe2b96e1da87183f3fa56abf2 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 7 Aug 2022 17:15:41 +0800 Subject: [PATCH] chore: update readme add more docs --- README.md | 27 ++++++++++++++++++++++++++- README.zh-CN.md | 24 ++++++++++++++++++++++++ src/IniEncoder.php | 3 +++ test/IniEncoderTest.php | 4 ++-- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4ff730a..c060951 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ val_arr_two[some_key] = "some_key_value" Decode from INI string. ```php -use PhpPkg\Ini\Ini;use Toolkit\Stdlib\Std\Collection; +use PhpPkg\Ini\Ini; +use Toolkit\Stdlib\Std\Collection; $data = Ini::decode($ini); vdump($data); @@ -150,7 +151,31 @@ $data = Ini::decodeFile($iniFile); Encode data to INI string. ```php +$data = [ + 'key0' => 'val0', + 'key1' => 'val1', + 'key2' => 'val2', + 'key3' => 'val3', + 'key4' => 'val4', + 'arrKey' => [ + 'abc', + 'def', + ], +]; + $iniString = Ini::encode($data); +echo $iniString; +``` + +**Output**: + +```ini +key0 = val0 +key1 = val1 +key2 = val2 +key3 = val3 +key4 = val4 +arrKey = [abc, def] ``` ## License diff --git a/README.zh-CN.md b/README.zh-CN.md index 4f004bd..ad2512e 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -149,7 +149,31 @@ $data = Ini::decodeFile($iniFile); Encode data to INI string. ```php +$data = [ + 'key0' => 'val0', + 'key1' => 'val1', + 'key2' => 'val2', + 'key3' => 'val3', + 'key4' => 'val4', + 'arrKey' => [ + 'abc', + 'def', + ], +]; + $iniString = Ini::encode($data); +echo $iniString; +``` + +**Output**: + +```ini +key0 = val0 +key1 = val1 +key2 = val2 +key3 = val3 +key4 = val4 +arrKey = [abc, def] ``` ## License diff --git a/src/IniEncoder.php b/src/IniEncoder.php index 3fb6431..c270de7 100644 --- a/src/IniEncoder.php +++ b/src/IniEncoder.php @@ -87,6 +87,9 @@ public function encode(array|Traversable $data, int $flags = 0): string $defSecIni = $this->encodeSection('', $defSec) . PHP_EOL; } + if (!$strings) { + return $defSecIni; + } return $defSecIni . implode("\n", $strings) . PHP_EOL; } diff --git a/test/IniEncoderTest.php b/test/IniEncoderTest.php index 9bc8f96..99f3008 100644 --- a/test/IniEncoderTest.php +++ b/test/IniEncoderTest.php @@ -22,8 +22,8 @@ public function testEncode_simple(): void 'key3' => 'val3', 'key4' => 'val4', 'arrKey' => [ - 0 => 'abc', - 1 => 'def', + 'abc', + 'def', ], ];