diff --git a/Tests/inifileTest.php b/Tests/inifileTest.php new file mode 100644 index 0000000..eb4f628 --- /dev/null +++ b/Tests/inifileTest.php @@ -0,0 +1,205 @@ + */ + +/** Test the outputjson.php file */ +class test_inifile extends PHPUnit_Framework_TestCase +{ + public function testsetString001 () + { + $this->setExpectedException ("Exception"); + $inifile = new inifile (); + $res = $inifile->setString (1, TRUE); + } + + public function testsetString002 () + { + $inifile = new inifile (); + $res = $inifile->setString (array (), TRUE); + $this->assertEquals("", $res); + } + + //// TEST OF THE SECTION PART //// + public function testsetString103 () + { + $this->setExpectedException ("Exception"); + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"), TRUE); + } + + public function testsetString104 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array ()), TRUE); + $this->assertEquals("[section1]\n\n", $res); + } + + public function testsetString105 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0)), TRUE); + $this->assertEquals("[section1]\n0 = \"0\"\n\n", $res); + } + + public function testsetString106 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>1)), TRUE); + $this->assertEquals("[section1]\n0 = \"1\"\n\n", $res); + } + + public function testsetString107 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null)), TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n\n", $res); + } + + public function testsetString108 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)), + TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n\n", $res); + } + + public function testsetString109 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str")), + TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n", + $res); + } + + public function testsetString110 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str", + 3=>array (1,2,3))), + TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n". + "3[0] = \"1\"\n3[1] = \"2\"\n3[2] = \"3\"\n\n", + $res); + } + + public function testsetString111 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str", + 3=>array ())), + TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n", + $res); + } + + public function testsetString112 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str", + "chain"=>array ("key"=>1,2))), + TRUE); + $this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n". + "chain[key] = \"1\"\nchain[0] = \"2\"\n\n", + $res); + } + + //// TEST OF THE NOT SECTION PART //// + public function testsetString203 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"), FALSE); + $this->assertEquals("0 = \"section1\"\n\n", $res); + } + + public function testsetString204 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array ()), FALSE); + $this->assertEquals("\n", $res); + } + + public function testsetString205 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0)), FALSE); + $this->assertEquals("section1[0] = \"0\"\n\n", $res); + } + + public function testsetString206 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>1)), FALSE); + $this->assertEquals("section1[0] = \"1\"\n\n", $res); + } + + public function testsetString207 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null)), FALSE); + $this->assertEquals("section1[0] = \"null\"\n\n", $res); + } + + public function testsetString208 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)), + FALSE); + $this->assertEquals("section1[0] = \"null\"\nsection1[1] = \"1\"\n\n", + $res); + } + + public function testsetString209 () + { + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str")), + FALSE); + $this->assertEquals("section1[0] = \"null\"\nsection1[1] = \"1\"\n". + "section1[2] = \"str\"\n\n", + $res); + } + + public function testsetString210 () + { + $this->setExpectedException ("Exception"); + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str", + 3=>array (1,2,3))), + FALSE); + } + + public function testsetString211 () + { + $this->setExpectedException ("Exception"); + $inifile = new inifile (); + $res = $inifile->setString (array ("section1"=>array (0=>null, + 1=>1, 2=>"str", + 3=>array ())), + FALSE); + } + + + //// LOOP TEST : CREATE AN INI STRING AND PARSE IT //// + public function testLoop406 () + { + $inifile = new inifile (); + $loop = $inifile->setString (array ("section1"=>array (0=>1)), FALSE); + $res = $inifile->getString ($loop, FALSE); + $this->assertEquals(array ("section1"=>array (0=>1)), $res); + } + + public function testLoop407 () + { + $inifile = new inifile (); + $loop = $inifile->setString (array ("section1"=>array (0=>null)), FALSE); + $res = $inifile->getString ($loop, FALSE); + $this->assertEquals(array ("section1"=>array (0=>null)), $res); + } +} diff --git a/inifile.php b/inifile.php new file mode 100644 index 0000000..e1d30c0 --- /dev/null +++ b/inifile.php @@ -0,0 +1,165 @@ + */ + +/** Manage .ini files like in php.ini + Support the sections (or not) */ +class inifile +{ + /** Return an array with the .ini file content + If the sections are true, the sections are analyzed too + This function is the same as parse_ini_file PHP internal */ + public function getFile ($file, $sections=false) + { + if (! file_exists ($file)) + throw new \Exception (sprintf (_("File '%s' not found"), $file), 404); + if (! is_readable ($file)) + throw new \Exception (sprintf (_("File '%s' not readable"), $file), 500); + $res = parse_ini_file ($file, $sections); + // The DomFramework is PHP 5.3 compatible. I need to overwrite the bools and + // null values. The INI_SCANNER_TYPED is available as PHP 5.6.1 + foreach ($res as $key=>$val) + { + if ($val === "null") + $res[$key] = null; + elseif ($val === "true") + $res[$key] = true; + elseif ($val === "false") + $res[$key] = false; + elseif (is_array ($val)) + { + foreach ($val as $k=>$v) + { + if ($v === "null") + $res[$key][$k] = null; + elseif ($v === "true") + $res[$key][$k] = true; + elseif ($v === "false") + $res[$key][$k] = false; + } + } + } + return $res; + } + + /** Return an array with the .ini string content + If the sections are true, the sections are analyzed too + This function is the same as parse_ini_string PHP internal */ + public function getString ($string, $sections=false) + { + $res = parse_ini_string ($string, $sections); + // The DomFramework is PHP 5.3 compatible. I need to overwrite the bools and + // null values. The INI_SCANNER_TYPED is available as PHP 5.6.1 + foreach ($res as $key=>$val) + { + if ($val === "null") + $res[$key] = null; + elseif ($val === "true") + $res[$key] = true; + elseif ($val === "false") + $res[$key] = false; + elseif (is_array ($val)) + { + foreach ($val as $k=>$v) + { + if ($v === "null") + $res[$key][$k] = null; + elseif ($v === "true") + $res[$key][$k] = true; + elseif ($v === "false") + $res[$key][$k] = false; + } + } + } + return $res; + } + + /** Return a string containing a .ini content from the provided array. + If the sections are true, define the first child of the array as sections + */ + public function setString ($array, $sections=false) + { + if (! is_array ($array)) + throw new \Exception ( + _("inifile::setString : provided data is not an array"), + 500); + $content = ""; + if ($sections !== false) + { + foreach ($array as $section=>$sub) + { + if (! is_array ($sub)) + throw new \Exception ( + _("inifile::setString : provided data is not an array"), + 500); + $content .= "[$section]\n"; + foreach ($sub as $key=>$val) + { + if (is_array ($val)) + { + foreach ($val as $k=>$v) + { + if (!is_scalar ($v) && ! is_null ($v)) + throw new \Exception (sprintf ( + _("Provided value for '%s' is not scalar"), + $key), 500); + if ($v === null) $v = "null"; + $content .= $key."[$k] = \"$v\"\n"; + } + } + else + { + if ($val === null) $val = "null"; + $content .= "$key = \"$val\"\n"; + } + } + $content .= "\n"; + } + } + else + { + foreach ($array as $key=>$val) + { + if (is_array ($val)) + { + foreach ($val as $k=>$v) + { + if (!is_scalar ($v) && ! is_null ($v)) + throw new \Exception (sprintf ( + _("Provided value for '%s' is not scalar"), + $key), 500); + if ($v === null) $v = "null"; + $content .= $key."[$k] = \"$v\"\n"; + } + } + else + { + if ($val === null) $val = "null"; + $content .= "$key = \"$val\"\n"; + } + } + $content .= "\n"; + + } + return $content; + } + + /** Save a file containing a .ini content from the provided array. + Don't create the directory if it doesn't exists + If the sections are true, define the first child of the array as sections + */ + public function setFile ($file, $array, $sections=false) + { + $dir = basename ($file); + if (! file_exists ($dir) || ! is_readable ($dir) || ! is_writeable ($dir)) + throw new \Exception (sprintf ( + _("Directory '%s' available or not readable or not writeable"), + $dir), 500); + if (file_exists ($file) && ! is_writeable ($file)) + throw new \Exception (sprintf (_("File '%s' is not writeable"), $file), + 500); + $content = $this->setString ($array, $sections); + return file_put_contents ($file, $content); + } +}