Throw exception on key overwrite

This commit is contained in:
Leonel Quinteros 2013-03-17 18:16:48 -03:00
parent 0eb93e8d87
commit 2f1c034a78
2 changed files with 15 additions and 5 deletions

View File

@ -17,8 +17,6 @@ Supports commit: f68d014bfd4a84a64fb5f6a7c1a83a4162415d4b
TODO
----
- Throw exception on extra chars after keygroup definition
- Throw exception on key or keygroup overwriting
- Write better tests.
- Support Unicode and \b chars.

View File

@ -96,13 +96,19 @@ class Toml
$keygroup = substr($line, 1, -1);
$aKeygroup = explode('.', $keygroup);
$last = count($aKeygroup) - 1;
foreach($aKeygroup as $keygroup)
foreach($aKeygroup as $i => $keygroup)
{
if( !isset($pointer[$keygroup]) )
{
$pointer[$keygroup] = array();
}
elseif($i == $last)
{
// Overwrite key
throw new Exception('Key overwrite previous keys: ' . $line);
}
// Move pointer forward
$pointer = & $pointer[$keygroup];
@ -112,9 +118,15 @@ class Toml
elseif(strpos($line, '='))
{
$kv = explode('=', $line, 2);
if(!isset($pointer[ trim($kv[0]) ]))
{
$pointer[ trim($kv[0]) ] = self::parseValue( $kv[1] );
}
else
{
throw new Exception('Key overwrite previous keys: ' . $line);
}
}
elseif($line[0] == '[' && substr($line, -1) != ']')
{
throw new Exception('Key groups have to be on a line by themselves: ' . $line);