diff --git a/README.md b/README.md index 07f5954..54eb9a7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/toml.php b/src/toml.php index 10e705c..06faf5e 100755 --- a/src/toml.php +++ b/src/toml.php @@ -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,8 +118,14 @@ class Toml elseif(strpos($line, '=')) { $kv = explode('=', $line, 2); - - $pointer[ trim($kv[0]) ] = self::parseValue( $kv[1] ); + 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) != ']') {