Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.4.6
=====

* (improvement) Made detection of rewritable paths in `ImportData` more robust.


3.4.5
=====

Expand Down
9 changes: 5 additions & 4 deletions src/Import/ImportData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
Expand All @@ -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}]";
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Import/ImportDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading