diff --git a/CHANGELOG.md b/CHANGELOG.md index 3017773..75f37ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +3.4.6 +===== + +* (improvement) Made detection of rewritable paths in `ImportData` more robust. + + 3.4.5 ===== diff --git a/src/Import/ImportData.php b/src/Import/ImportData.php index 25bdaf0..a55fa14 100644 --- a/src/Import/ImportData.php +++ b/src/Import/ImportData.php @@ -4,7 +4,7 @@ use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException; use Symfony\Component\PropertyAccess\PropertyAccess; -use Symfony\Component\PropertyAccess\PropertyAccessor; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Torr\Rad\Exception\Import\InvalidImportDataException; /** @@ -14,13 +14,14 @@ */ readonly class ImportData implements \IteratorAggregate, \Countable { - private PropertyAccessor $accessor; + private PropertyAccessorInterface $accessor; public function __construct ( private array $data, + ?PropertyAccessorInterface $accessor = null, ) { - $this->accessor = PropertyAccess::createPropertyAccessor(); + $this->accessor = $accessor ?? PropertyAccess::createPropertyAccessor(); } /** @@ -30,7 +31,7 @@ public function get (string $path) : mixed { // for simple paths, we automatically wrap it in [...], so that you don't have to write it explicitly. // we only require it for nested paths / complex names - if (preg_match('~^[a-z0-9\\-_]+$~', $path)) + if (!preg_match('~[\[\].]~', $path)) { $path = "[{$path}]"; } diff --git a/tests/Import/ImportDataTest.php b/tests/Import/ImportDataTest.php index 25cc147..e6af71b 100644 --- a/tests/Import/ImportDataTest.php +++ b/tests/Import/ImportDataTest.php @@ -3,6 +3,7 @@ namespace Tests\Torr\Rad\Import; use PHPUnit\Framework\TestCase; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Tests\Torr\Rad\Fixtures\ExampleBackedEnum; use Torr\Rad\Exception\Import\InvalidImportDataException; use Torr\Rad\Import\ImportData; @@ -204,4 +205,32 @@ public function testInvalid ( $callback($data); } + + /** + * + */ + public static function providePathRewrite () : iterable + { + yield ["abc", "[abc]"]; + yield ["aBc1_", "[aBc1_]"]; + yield ["abc def", "[abc def]"]; + yield ["test.abc", "test.abc"]; + yield ["[abc]", "[abc]"]; + yield ["[abc][def]", "[abc][def]"]; + } + + /** + * @dataProvider providePathRewrite + */ + public function testPathRewrite (string $input, string $expected) : void + { + $accessor = self::createMock(PropertyAccessorInterface::class); + $accessor + ->expects(self::once()) + ->method("getValue") + ->with([], $expected); + + $import = new ImportData([], $accessor); + $import->get($input); + } }