Skip to content

[DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept ReadableCollection #57935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"async-aws/sqs": "^1.0|^2.0",
"async-aws/sns": "^1.0",
"cache/integration-tests": "dev-master",
"doctrine/collections": "^1.0|^2.0",
"doctrine/collections": "^1.8|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^3.6|^4",
"doctrine/orm": "^2.15|^3",
Expand Down Expand Up @@ -163,6 +163,7 @@
"conflict": {
"ext-psr": "<1.1|>=2",
"async-aws/core": "<1.5",
"doctrine/collections": "<1.8",
"doctrine/dbal": "<3.6",
"doctrine/orm": "<2.15",
"egulias/email-validator": "~3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Accept `ReadableCollection` in `CollectionToArrayTransformer`

7.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ReadableCollection;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

Expand Down Expand Up @@ -40,8 +41,8 @@ public function transform(mixed $collection): mixed
return $collection;
}

if (!$collection instanceof Collection) {
throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.');
if (!$collection instanceof ReadableCollection) {
throw new TransformationFailedException(\sprintf('Expected a "%s" object.', ReadableCollection::class));
}

return $collection->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Tests\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\ReadableCollection;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
Expand Down Expand Up @@ -66,6 +67,118 @@ public function testTransformExpectsArrayOrCollection()
$this->transformer->transform('Foo');
}

public function testTransformReadableCollection()
{
$array = [
2 => 'foo',
3 => 'bar',
];

$collection = new class($array) implements ReadableCollection
{
public function __construct(private readonly array $array)
{
}

public function contains($element): bool
{
}

public function isEmpty(): bool
{
}

public function containsKey($key): bool
{
}

public function get($key): mixed
{
}

public function getKeys(): array
{
}

public function getValues(): array
{
}

public function toArray(): array
{
return $this->array;
}

public function first(): mixed
{
}

public function last(): mixed
{
}

public function key(): string|int|null
{
}

public function current(): mixed
{
}

public function next(): mixed
{
}

public function slice($offset, $length = null): array
{
}

public function exists(\Closure $p): bool
{
}

public function filter(\Closure $p): ReadableCollection
{
}

public function map(\Closure $func): ReadableCollection
{
}

public function partition(\Closure $p): array
{
}

public function forAll(\Closure $p): bool
{
}

public function indexOf($element): int|string|bool
{
}

public function findFirst(\Closure $p): mixed
{
}

public function reduce(\Closure $func, mixed $initial = null): mixed
{
}

public function getIterator(): \Traversable
{
return new \ArrayIterator($this->array);
}

public function count(): int
{
return count($this->array);
}
};

$this->assertSame($array, $this->transformer->transform($collection));
}

public function testReverseTransform()
{
$array = [
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@
"symfony/uid": "^6.4|^7.0",
"symfony/validator": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0",
"doctrine/collections": "^1.0|^2.0",
"doctrine/collections": "^1.8|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^3.6|^4",
"doctrine/orm": "^2.15|^3",
"psr/log": "^1|^2|^3"
},
"conflict": {
"doctrine/collections": "<1.8",
"doctrine/dbal": "<3.6",
"doctrine/lexer": "<1.1",
"doctrine/orm": "<2.15",
Expand Down
Loading