Skip to content

Commit

Permalink
Merge pull request #14 from mediact/feature/APD-1837
Browse files Browse the repository at this point in the history
Keep leading zeros when parsing a path

Allow keys to keep leading zeros when parsing a path. Paths like
"foo.000.bar" were turned into ["foo", 0, "bar"] resulting in a
different structure after setting values in the data container.

The key is now only converted into a string if the value is numeric,
and has no leading zero.
  • Loading branch information
michielfb authored Nov 11, 2021
2 parents 92e6157 + 3cc3cb8 commit ddff0d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PathParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private function parsePath(string $path): array
{
return array_map(
function (string $key) {
return ctype_digit($key)
return ctype_digit($key) && (strlen($key) === 1 || substr($key, 0, 1) !== "0")
? intval($key)
: $key;
},
Expand Down
30 changes: 29 additions & 1 deletion tests/PathParserTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,35 @@ public function dataProvider(): array
[
'$path' => '"fo""o".."baz.a"..',
'$expected' => ['fo"o', 'baz.a']
]
],
[
'$path' => 'foo.000',
'$expected' => ['foo', '000'],
],
[
'$path' => 'foo.0001',
'$expected' => ['foo', '0001'],
],
[
'$path' => 'foo.0001.bar',
'$expected' => ['foo', '0001', 'bar'],
],
[
'$path' => 'foo.0',
'$expected' => ['foo', 0],
],
[
'$path' => 'foo.0.bar',
'$expected' => ['foo', 0, 'bar'],
],
[
'$path' => 'foo.1000',
'$expected' => ['foo', 1000],
],
[
'$path' => 'foo.1001',
'$expected' => ['foo', 1001],
],
];
}
}

0 comments on commit ddff0d9

Please sign in to comment.