コード例 #1
0
ファイル: FactoryTest.php プロジェクト: retrinko/ini
 public function test_fromIniSections_withProperSectionsArray_returnsIniFile()
 {
     $sectionA = new \Retrinko\Ini\IniSection('section A');
     $sectionA->set('key1', 'val 1');
     $sectionA->set('key2', 'val 2');
     $sectionB = new \Retrinko\Ini\IniSection('section B', $sectionA);
     $sectionB->set('key3', 'val 3');
     $sections = [$sectionA, $sectionB];
     $iniFile = \Retrinko\Ini\IniFile\Factory::fromIniSections($sections);
     $this->assertTrue($iniFile instanceof \Retrinko\Ini\IniFile);
     $this->assertEquals('val 1', $iniFile->get('section A', 'key1'));
     $this->assertEquals('val 2', $iniFile->get('section A', 'key2'));
     $this->assertEquals('default', $iniFile->get('section A', 'key3', 'default'));
     $this->assertEquals('val 1', $iniFile->get('section B', 'key1'));
     $this->assertEquals('val 2', $iniFile->get('section B', 'key2'));
     $this->assertEquals('val 3', $iniFile->get('section B', 'key3'));
 }
コード例 #2
0
ファイル: load-array.php プロジェクト: retrinko/ini
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Retrinko\Ini\IniFile;
try {
    // Array with ini data
    $data = ['section 1' => ['key1' => 'value 1.1', 'key2' => 'value 1.2'], 'section 2' => ['key3' => 'value 2.3'], 'section 3 : section 1' => ['key1' => 'value 3.1'], 'section 4' => ['intVal' => 1, 'floatVal' => 1.2, 'boolVal' => false, 'nullVal' => null, 'arrayVal' => ['a' => 'A', 'b' => 'B', 'c' => 'C']]];
    // Get an IniFile instance from the previous array
    $iniFile = IniFile\Factory::fromArray($data);
    // Obtain some data
    $val = $iniFile->get('section 1', 'key1');
    printLn('section 1, key1: %s', $val);
    $val = $iniFile->get('section 2', 'key1', 'no value!');
    printLn('section 2, key1: %s', $val);
    $val = $iniFile->get('section 3', 'key1');
    printLn('section 3, key1: %s', $val);
    $val = $iniFile->get('section 3', 'key2');
    printLn('section 3, key2: %s', $val);
    $val = $iniFile->get('section 4', 'intVal');
    printLn('section 4, intVal: %s', $val);
    $val = $iniFile->get('section 4', 'floatVal');
    printLn('section 4, floatVal: %s', $val);
    $val = $iniFile->get('section 4', 'boolVal');
    printLn('section 4, boolVal: %s', is_bool($val) ? true == $val ? 'true' : 'false' : $val);
    $val = $iniFile->get('section 4', 'nullVal');
    printLn('section 4, nullVal: %s', is_null($val) ? 'null' : $val);
    $val = $iniFile->get('section 4', 'arrayVal');
    printLn('section 4, arrayVal: %s', var_export($val, true));
    // Save to ini file
    $outputFile = __DIR__ . '/array-to-inifile.ini';
    $iniFile->save($outputFile);