diff --git a/src/IniWriter.php b/src/IniWriter.php index b18af46..685cdfe 100644 --- a/src/IniWriter.php +++ b/src/IniWriter.php @@ -92,6 +92,9 @@ public function writeToString(array $config, $header = '') $value = array($value); } + // adding comments + $ini .= $this->addComments($comments, $sectionName, $option); + if (is_array($value)) { foreach ($value as $currentValue) { $ini .= $option . '[] = ' . $this->encodeValue($currentValue) . "\n"; @@ -107,6 +110,26 @@ public function writeToString(array $config, $header = '') return $ini; } + private function addComments($comments, $sectionName, $option) { + $result = ""; + + if (isset($comments[$sectionName][$option])) { + $comment = explode("\n", $comments[$sectionName][$option]); + array_pop($comment); + $next = array_shift($comment); + while ($next !== null) { + if ($next === "") { + $result .= "\n"; + } else { + $result .= "; " . $next . "\n"; + } + $next = array_shift($comment); + } + } + + return $result; + } + private function encodeValue($value) { if (is_bool($value)) {