devsuite/cds/IniWriter.php

57 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2024-05-27 00:44:14 -04:00
<?php
namespace Crispage\DevSuite;
defined("ROOT") or die();
use \Crispage\Config;
use \Crispage\Utils\FileUtils;
class IniWriter {
public const INI_FALSE = "False";
public const INI_TRUE = "True";
public static function inival(mixed $value): string {
if (is_bool($value))
return ($value) ? self::INI_TRUE : self::INI_FALSE;
if (is_int($value) || is_float($value))
return strval($value);
if (is_string($value)) return "\"$value\"";
return "";
}
public static function section(array $data, ?string $section = null): string {
$str = "";
if ($section) $str .= "[$section]\n";
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v)
$str .= "{$key}[$k] = " . self::inival($v) . "\n";
}
else $str .= "$key = " . self::inival($value) . "\n";
}
$str .= "\n";
return $str;
}
public static function ini(array $data, bool $sections = true): string {
$ini = "";
if ($sections) {
foreach ($data as $section => $sdata)
$ini .= self::section($sdata, $section);
}
else $ini .= self::section($data);
return $ini;
}
public static function write(string $path, array $data, bool $sections = true): int|false {
$ini = self::ini($data, $sections);
return file_put_contents($path, $ini);
}
}
?>