From 3cc3cb8ac532ac6b10a57b515ab537b09f650ab0 Mon Sep 17 00:00:00 2001 From: Michiel Brunsting Date: Wed, 10 Nov 2021 11:59:00 +0000 Subject: [PATCH] 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. --- src/PathParserTrait.php | 2 +- tests/PathParserTraitTest.php | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/PathParserTrait.php b/src/PathParserTrait.php index 61aa960..60bdc80 100644 --- a/src/PathParserTrait.php +++ b/src/PathParserTrait.php @@ -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; }, diff --git a/tests/PathParserTraitTest.php b/tests/PathParserTraitTest.php index 4af7a1e..1c34eca 100644 --- a/tests/PathParserTraitTest.php +++ b/tests/PathParserTraitTest.php @@ -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], + ], ]; } }