/** * @param IniSection[] $iniSections * * @return IniFile * @throws InvalidDataException */ public static function fromIniSections(array $iniSections) { $iniFile = new IniFile(); foreach ($iniSections as $iniSection) { if (false === $iniSection instanceof IniSection) { throw new InvalidDataException('Invalid data! '); } $iniFile->addSection($iniSection); } return $iniFile; }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Retrinko\Ini\IniFile; try { // Load contents from ini file $iniFile = IniFile::load(__DIR__ . '/sample.ini'); // Read key1 value from default section $key1 = $iniFile->get('default', 'key1'); printLn('default section, key1 value: %s', $key1); // Read key1 value from A section $key1 = $iniFile->get('A', 'key1'); printLn('A section, key1 value: %s', $key1); // Read key1 value from B section $key1 = $iniFile->get('B', 'key1'); printLn('B section, key1 value: %s', $key1); // Read boolYes value from B section $boolYes = $iniFile->get('B', 'boolYes'); printLn('B section, boolYes value: %s', $boolYes); // Get ini file contents as array $array = $iniFile->toArray(); printLn('Contents as array: %s', PHP_EOL . var_export($array, true)); } catch (\Exception $e) { printLn('Exception! %s', $e->getMessage()); } function printLn($string) { $vars = func_get_args(); array_shift($vars); vprintf('>>> ' . $string . PHP_EOL, $vars); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Retrinko\Ini\IniFile; use Retrinko\Ini\IniSection; try { // Create new IniFile instance $iniFile = new IniFile(); // Create section "base" $section = new IniSection('base'); // Add items to section "base" $section->set('hello', 'world'); $section->set('colors', ['red', 'green']); $section->set('rgb', ['red' => 'AA0000', 'green' => '00AA00']); $section->set('width', 25); $section->set('height', 50.33); $section->set('bool', true); $section->set('nullValue', null); // Add section "base" to ini file $iniFile->addSection($section); // Add child section "child" $childSection = new IniSection('child', $section); // Add items to section "child" $childSection->set('height', 20); $childSection->set('width', 20); // Add section "child" to ini file $iniFile->addSection($childSection); // Add item values to sections $iniFile->set('base', 'new-item', 'value'); $iniFile->set('child', 'last-item', 'last'); // Save to file