Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/TomlKeystore.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ protected function addArrayTableNode(ArrayTableNode $arrayTableNode): void
continue;
}

if (str_starts_with($header, $arrayTableHeader)) {
if (str_starts_with($header, $arrayTableHeader . '.')) {
$key = $arrayTable.substr($header, strlen($arrayTableHeader));

break;
}
}

if ($index === 0 && ! $this->tables->filter(
static fn ($table) => str_starts_with((string) $table, $header))->isEmpty()
static fn ($table) => str_starts_with((string) $table, $header . "."))->isEmpty()
) {
throw new TomlError('key duplication');
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/TomlDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function () {
{ "name": "plantain" }
]
}
],
"fruit": [
{
"name": "strawberry"
}
]
}
JSON_STRING;
Expand Down Expand Up @@ -111,12 +116,49 @@ function () {

[[fruits.varieties]]
name = "plantain"

[[fruit]] # prefix of existing array
name = "strawberry"
TOML_STRING;

expect(toml_decode($toml))->toEqual(json_decode($json, false))
->and(toml_decode($toml, true))->toEqual(json_decode($json, true));
});

it('can decode array tables whose names share a prefix',
/**
* @throws TomlError
*/
function () {
$toml = <<<'TOML_STRING'
[[items]]
name = "first"

[[itemsExtended]]
name = "second"

[[itemsExtendedMore]]
name = "third"

[[itemsExtendedMore.entries]]
name = "first entry"

[itemsExtendedMore.entries.metadata]
type = "primary"

[[itemsExtendedMore.entries]]
name = "second entry"

[itemsExtendedMore.entries.metadata]
type = "secondary"
TOML_STRING;

$decoded = toml_decode($toml, true);

expect($decoded)->toHaveCount(3)
->and($decoded['itemsExtendedMore'][0]['entries'][1]['metadata']['type'])->toBe('secondary');
});

it('can decode TOML datetime formats',
/**
* @throws TomlError
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/TomlErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ function () {

expect(static fn () => toml_decode($toml, true))->toThrow(TomlError::class, $message);
});

it('throws on invalid array order',
/**
* @throws TomlError
*/
function () {

$message = <<<'MESSAGE'
Invalid TOML document: key duplication

5: # This requires a to be an array table
6: [[a]]
^
7: y = 2
MESSAGE;

$toml = <<<'TOML_STRING'
# This creates a as a normal (non-array) table
[[a.b]]
x = 1

# This requires a to be an array table
[[a]]
y = 2
TOML_STRING;

expect(static fn () => toml_decode($toml, true))->toThrow(TomlError::class, $message);
});