Skip to content

Commit c29422b

Browse files
feature #57935 [DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept ReadableCollection (timdev)
This PR was merged into the 7.2 branch. Discussion ---------- [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 tranform instances of ReadableCollection. | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT 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. This change does not break BC, but does make `doctrine/collections` a non-dev dependency in order to require at least `1.8.0` of that library (previously, the lower-bound version was implicitly `^1.5` via `doctrine/o[rd]m`. This is one of several reasons why I'm considering this an addition and not a bugfix. Symfony maintainers are, of course, welcome to backport this patch to earlier versions of the framework at their discretion. I've omitted a test here, since the signal:noise ratio would be poor due the the necessary inclusion of a boilerplate ReadableCollection implementation. I'm happy to add one if desired, however. Commits ------- a13f0ac92e [DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept ReadableCollection
2 parents 7290325 + d14a661 commit c29422b

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

CHANGELOG.md

+5
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

+3-2
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

+113
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

+2-1
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)