Skip to content

Commit

Permalink
Handle dotted and quoted keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelquinteros committed Jul 3, 2018
1 parent d24c0d8 commit 33e0157
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
78 changes: 72 additions & 6 deletions src/Toml.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,7 @@ public static function parse($toml)
}

// Set key=value
if(is_array($pointer)) {
$pointer[ trim($kv[0]) ] = self::parseValue( trim($kv[1]) );
} else {
$pointer->{trim($kv[0])} = self::parseValue( trim($kv[1]) );
}

$key = self::parseKeyValue(trim($kv[0]), trim($kv[1]), $pointer);
}
else
{
Expand Down Expand Up @@ -739,6 +734,77 @@ private static function parseInlineTable($val)
return $result;
}

/**
* Function that takes a key expression and the current pointer to position in the right hierarchy.
* Then it sets the corresponding value on that position.
*
* @param (string) $key
* @param (&) $pointer
*/
private static function parseKeyValue($key, $val, & $pointer)
{
// Flags
$openQuote = false;
$openDoubleQuote = false;

// Buffer
$buff = '';

// Parse
for($i = 0; $i < strlen($key); $i++) {
// Handle quoting
if($key[$i] == '"') {
if(!$openQuote) {
if($openDoubleQuote) {
$openDoubleQuote = false;
} else {
$openDoubleQuote = true;
}
continue;
}
}
if($key[$i] == "'") {
if(!$openDoubleQuote) {
if($openQuote) {
$openQuote = false;
} else {
$openQuote = true;
}
continue;
}
}

// Handle dotted keys
if($key[$i] == "." && !$openQuote && !$openDoubleQuote) {
if(is_array($pointer)) {
if(!isset($pointer[$buff])) {
$pointer[$buff] = new stdClass();
}
$pointer = & $pointer[$buff];
} else {
if(!isset($pointer->{$buff})) {
$pointer->{$buff} = new stdClass();
}
$pointer = & $pointer->{$buff};
}

// Cleanup buffer
$buff = '';
continue;
}

$buff .= $key[$i];
}

if(is_array($pointer)) {
$pointer[$buff] = self::parseValue( $val );
} else {
$pointer->{$buff} = self::parseValue( $val );
}

return $buff;
}

/**
* Function that checks the data type of the first and last elements of an array,
* and returns false if they don't match
Expand Down
20 changes: 20 additions & 0 deletions travis-test/example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
# the line.


################################################################################
## Keys

# Quoted keys follow the exact same rules as either basic strings or literal strings and allow you to use a much broader set of key names.
# Best practice is to use bare keys except when absolutely necessary.

key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"

# Dotted keys are a sequence of bare or quoted keys joined with a dot.
# This allows for grouping similar properties together:

name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true


################################################################################
## Table

Expand Down

0 comments on commit 33e0157

Please sign in to comment.