inifile : return an integer/float if the value is a number

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2562 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2016-02-25 08:37:00 +00:00
parent e0b5b720c9
commit 1ad99a3641
2 changed files with 42 additions and 49 deletions

View File

@@ -17,7 +17,7 @@ class test_inifile extends PHPUnit_Framework_TestCase
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array (), TRUE); $res = $inifile->setString (array (), TRUE);
$this->assertEquals("", $res); $this->assertSame("", $res);
} }
//// TEST OF THE SECTION PART //// //// TEST OF THE SECTION PART ////
@@ -32,28 +32,28 @@ class test_inifile extends PHPUnit_Framework_TestCase
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array ()), TRUE); $res = $inifile->setString (array ("section1"=>array ()), TRUE);
$this->assertEquals("[section1]\n\n", $res); $this->assertSame("[section1]\n\n", $res);
} }
public function testsetString105 () public function testsetString105 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0)), TRUE); $res = $inifile->setString (array ("section1"=>array (0)), TRUE);
$this->assertEquals("[section1]\n0 = \"0\"\n\n", $res); $this->assertSame("[section1]\n0 = \"0\"\n\n", $res);
} }
public function testsetString106 () public function testsetString106 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>1)), TRUE); $res = $inifile->setString (array ("section1"=>array (0=>1)), TRUE);
$this->assertEquals("[section1]\n0 = \"1\"\n\n", $res); $this->assertSame("[section1]\n0 = \"1\"\n\n", $res);
} }
public function testsetString107 () public function testsetString107 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>null)), TRUE); $res = $inifile->setString (array ("section1"=>array (0=>null)), TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n\n", $res); $this->assertSame("[section1]\n0 = \"null\"\n\n", $res);
} }
public function testsetString108 () public function testsetString108 ()
@@ -61,7 +61,7 @@ class test_inifile extends PHPUnit_Framework_TestCase
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)), $res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)),
TRUE); TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n\n", $res); $this->assertSame("[section1]\n0 = \"null\"\n1 = \"1\"\n\n", $res);
} }
public function testsetString109 () public function testsetString109 ()
@@ -70,7 +70,7 @@ class test_inifile extends PHPUnit_Framework_TestCase
$res = $inifile->setString (array ("section1"=>array (0=>null, $res = $inifile->setString (array ("section1"=>array (0=>null,
1=>1, 2=>"str")), 1=>1, 2=>"str")),
TRUE); TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n", $this->assertSame("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n",
$res); $res);
} }
@@ -81,7 +81,7 @@ class test_inifile extends PHPUnit_Framework_TestCase
1=>1, 2=>"str", 1=>1, 2=>"str",
3=>array (1,2,3))), 3=>array (1,2,3))),
TRUE); TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n". $this->assertSame("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n".
"3[0] = \"1\"\n3[1] = \"2\"\n3[2] = \"3\"\n\n", "3[0] = \"1\"\n3[1] = \"2\"\n3[2] = \"3\"\n\n",
$res); $res);
} }
@@ -93,7 +93,7 @@ class test_inifile extends PHPUnit_Framework_TestCase
1=>1, 2=>"str", 1=>1, 2=>"str",
3=>array ())), 3=>array ())),
TRUE); TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n", $this->assertSame("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n\n",
$res); $res);
} }
@@ -104,9 +104,9 @@ class test_inifile extends PHPUnit_Framework_TestCase
1=>1, 2=>"str", 1=>1, 2=>"str",
"chain"=>array ("key"=>1,2))), "chain"=>array ("key"=>1,2))),
TRUE); TRUE);
$this->assertEquals("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n". $this->assertSame("[section1]\n0 = \"null\"\n1 = \"1\"\n2 = \"str\"\n".
"chain[key] = \"1\"\nchain[0] = \"2\"\n\n", "chain[key] = \"1\"\nchain[0] = \"2\"\n\n",
$res); $res);
} }
//// TEST OF THE NOT SECTION PART //// //// TEST OF THE NOT SECTION PART ////
@@ -114,35 +114,35 @@ class test_inifile extends PHPUnit_Framework_TestCase
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"), FALSE); $res = $inifile->setString (array ("section1"), FALSE);
$this->assertEquals("0 = \"section1\"\n\n", $res); $this->assertSame("0 = \"section1\"\n\n", $res);
} }
public function testsetString204 () public function testsetString204 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array ()), FALSE); $res = $inifile->setString (array ("section1"=>array ()), FALSE);
$this->assertEquals("\n", $res); $this->assertSame("\n", $res);
} }
public function testsetString205 () public function testsetString205 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0)), FALSE); $res = $inifile->setString (array ("section1"=>array (0)), FALSE);
$this->assertEquals("section1[0] = \"0\"\n\n", $res); $this->assertSame("section1[0] = \"0\"\n\n", $res);
} }
public function testsetString206 () public function testsetString206 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>1)), FALSE); $res = $inifile->setString (array ("section1"=>array (0=>1)), FALSE);
$this->assertEquals("section1[0] = \"1\"\n\n", $res); $this->assertSame("section1[0] = \"1\"\n\n", $res);
} }
public function testsetString207 () public function testsetString207 ()
{ {
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>null)), FALSE); $res = $inifile->setString (array ("section1"=>array (0=>null)), FALSE);
$this->assertEquals("section1[0] = \"null\"\n\n", $res); $this->assertSame("section1[0] = \"null\"\n\n", $res);
} }
public function testsetString208 () public function testsetString208 ()
@@ -150,8 +150,8 @@ class test_inifile extends PHPUnit_Framework_TestCase
$inifile = new inifile (); $inifile = new inifile ();
$res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)), $res = $inifile->setString (array ("section1"=>array (0=>null, 1=>1)),
FALSE); FALSE);
$this->assertEquals("section1[0] = \"null\"\nsection1[1] = \"1\"\n\n", $this->assertSame("section1[0] = \"null\"\nsection1[1] = \"1\"\n\n",
$res); $res);
} }
public function testsetString209 () public function testsetString209 ()
@@ -160,9 +160,9 @@ class test_inifile extends PHPUnit_Framework_TestCase
$res = $inifile->setString (array ("section1"=>array (0=>null, $res = $inifile->setString (array ("section1"=>array (0=>null,
1=>1, 2=>"str")), 1=>1, 2=>"str")),
FALSE); FALSE);
$this->assertEquals("section1[0] = \"null\"\nsection1[1] = \"1\"\n". $this->assertSame("section1[0] = \"null\"\nsection1[1] = \"1\"\n".
"section1[2] = \"str\"\n\n", "section1[2] = \"str\"\n\n",
$res); $res);
} }
public function testsetString210 () public function testsetString210 ()
@@ -191,8 +191,10 @@ class test_inifile extends PHPUnit_Framework_TestCase
{ {
$inifile = new inifile (); $inifile = new inifile ();
$loop = $inifile->setString (array ("section1"=>array (0=>1)), FALSE); $loop = $inifile->setString (array ("section1"=>array (0=>1)), FALSE);
var_dump ($loop);
$res = $inifile->getString ($loop, FALSE); $res = $inifile->getString ($loop, FALSE);
$this->assertEquals(array ("section1"=>array (0=>1)), $res); var_dump ($res);
$this->assertSame(array ("section1"=>array (0=>1)), $res);
} }
public function testLoop407 () public function testLoop407 ()
@@ -200,6 +202,17 @@ class test_inifile extends PHPUnit_Framework_TestCase
$inifile = new inifile (); $inifile = new inifile ();
$loop = $inifile->setString (array ("section1"=>array (0=>null)), FALSE); $loop = $inifile->setString (array ("section1"=>array (0=>null)), FALSE);
$res = $inifile->getString ($loop, FALSE); $res = $inifile->getString ($loop, FALSE);
$this->assertEquals(array ("section1"=>array (0=>null)), $res); $this->assertSame(array ("section1"=>array (0=>null)), $res);
} }
public function testLoop408 ()
{
$inifile = new inifile ();
$loop = $inifile->setString (array ("section1"=>array (0=>"toto")), FALSE);
var_dump ($loop);
$res = $inifile->getString ($loop, FALSE);
var_dump ($res);
$this->assertSame(array ("section1"=>array (0=>"toto")), $res);
}
} }

View File

@@ -18,31 +18,7 @@ class inifile
if (! is_readable ($file)) if (! is_readable ($file))
throw new \Exception (sprintf (dgettext("domframework", throw new \Exception (sprintf (dgettext("domframework",
"File '%s' not readable"), $file), 500); "File '%s' not readable"), $file), 500);
$res = parse_ini_file ($file, $sections); return $this->getString (file_get_contents ($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 /** Return an array with the .ini string content
@@ -71,8 +47,12 @@ class inifile
$res[$key][$k] = true; $res[$key][$k] = true;
elseif ($v === "false") elseif ($v === "false")
$res[$key][$k] = false; $res[$key][$k] = false;
elseif (is_numeric ($v))
$res[$key][$k] = $v + 0;
} }
} }
elseif (is_numeric ($val))
$res[$key] = $val + 0;
} }
return $res; return $res;
} }