Skip to content

Commit d14a661

Browse files
timdevnicolas-grekas
authored andcommitted
[DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept ReadableCollection
Since 1.8.0 doctrine/collections ships ReadableCollection, which the Collection interface extends. This commit relaxes the behavior of CollectionToArrayTransformer::transform(), allowing it to transform instances of ReadableCollection.
1 parent 1c9fbd6 commit d14a661

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Accept `ReadableCollection` in `CollectionToArrayTransformer`
8+
49
7.1
510
---
611

Form/DataTransformer/CollectionToArrayTransformer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Collections\ArrayCollection;
1515
use Doctrine\Common\Collections\Collection;
16+
use Doctrine\Common\Collections\ReadableCollection;
1617
use Symfony\Component\Form\DataTransformerInterface;
1718
use Symfony\Component\Form\Exception\TransformationFailedException;
1819

@@ -40,8 +41,8 @@ public function transform(mixed $collection): mixed
4041
return $collection;
4142
}
4243

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

4748
return $collection->toArray();

Tests/Form/DataTransformer/CollectionToArrayTransformerTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Form\DataTransformer;
1313

1414
use Doctrine\Common\Collections\ArrayCollection;
15+
use Doctrine\Common\Collections\ReadableCollection;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
1718
use Symfony\Component\Form\Exception\TransformationFailedException;
@@ -66,6 +67,118 @@ public function testTransformExpectsArrayOrCollection()
6667
$this->transformer->transform('Foo');
6768
}
6869

70+
public function testTransformReadableCollection()
71+
{
72+
$array = [
73+
2 => 'foo',
74+
3 => 'bar',
75+
];
76+
77+
$collection = new class($array) implements ReadableCollection
78+
{
79+
public function __construct(private readonly array $array)
80+
{
81+
}
82+
83+
public function contains($element): bool
84+
{
85+
}
86+
87+
public function isEmpty(): bool
88+
{
89+
}
90+
91+
public function containsKey($key): bool
92+
{
93+
}
94+
95+
public function get($key): mixed
96+
{
97+
}
98+
99+
public function getKeys(): array
100+
{
101+
}
102+
103+
public function getValues(): array
104+
{
105+
}
106+
107+
public function toArray(): array
108+
{
109+
return $this->array;
110+
}
111+
112+
public function first(): mixed
113+
{
114+
}
115+
116+
public function last(): mixed
117+
{
118+
}
119+
120+
public function key(): string|int|null
121+
{
122+
}
123+
124+
public function current(): mixed
125+
{
126+
}
127+
128+
public function next(): mixed
129+
{
130+
}
131+
132+
public function slice($offset, $length = null): array
133+
{
134+
}
135+
136+
public function exists(\Closure $p): bool
137+
{
138+
}
139+
140+
public function filter(\Closure $p): ReadableCollection
141+
{
142+
}
143+
144+
public function map(\Closure $func): ReadableCollection
145+
{
146+
}
147+
148+
public function partition(\Closure $p): array
149+
{
150+
}
151+
152+
public function forAll(\Closure $p): bool
153+
{
154+
}
155+
156+
public function indexOf($element): int|string|bool
157+
{
158+
}
159+
160+
public function findFirst(\Closure $p): mixed
161+
{
162+
}
163+
164+
public function reduce(\Closure $func, mixed $initial = null): mixed
165+
{
166+
}
167+
168+
public function getIterator(): \Traversable
169+
{
170+
return new \ArrayIterator($this->array);
171+
}
172+
173+
public function count(): int
174+
{
175+
return count($this->array);
176+
}
177+
};
178+
179+
$this->assertSame($array, $this->transformer->transform($collection));
180+
}
181+
69182
public function testReverseTransform()
70183
{
71184
$array = [

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@
4343
"symfony/uid": "^6.4|^7.0",
4444
"symfony/validator": "^6.4|^7.0",
4545
"symfony/var-dumper": "^6.4|^7.0",
46-
"doctrine/collections": "^1.0|^2.0",
46+
"doctrine/collections": "^1.8|^2.0",
4747
"doctrine/data-fixtures": "^1.1",
4848
"doctrine/dbal": "^3.6|^4",
4949
"doctrine/orm": "^2.15|^3",
5050
"psr/log": "^1|^2|^3"
5151
},
5252
"conflict": {
53+
"doctrine/collections": "<1.8",
5354
"doctrine/dbal": "<3.6",
5455
"doctrine/lexer": "<1.1",
5556
"doctrine/orm": "<2.15",

0 commit comments

Comments
 (0)