public function writeToString($data, $header = '')
    {
        if ($data instanceof IniCollection)
            $data = $data->toArray();
        else if (!is_array($data))
            throw new IniWritingException('The expected type is array or \NoczCore\Ini\IniCollection');

        $ini = '';
        if (!empty($header))
            $ini .= $header.PHP_EOL;
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                if (empty($v))
                    continue;
                $ini .= "[$k]" . PHP_EOL;
                foreach ($this->convertRecursive($v) as $key => $value) {
                    $last = explode('.', $key);
                    $last = end($last);
                    if (strpos($key, '.') !== false && is_numeric($last))
                        $key = substr($key, 0, -strlen('.' . $last)) . '[]';
                    $ini .= "$key = {$this->encode($value)}" . PHP_EOL;
                }
            } else {
                $ini .= "$k = {$this->encode($v)}" . PHP_EOL;
            }
        }
        return $ini;
    }