Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cea8b69

Browse filesBrowse files
author
Bradley Zeggelaar
committed
[Serializer] Add support to only check first element of $data in ArrayDenormalizer
1 parent 0fc8f7a commit cea8b69
Copy full SHA for cea8b69

File tree

2 files changed

+102
-2
lines changed
Filter options

2 files changed

+102
-2
lines changed

‎src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
+19-2Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
2929
{
3030
use DenormalizerAwareTrait;
3131

32+
/**
33+
* @var bool
34+
*/
35+
private $checkFirstElement;
36+
37+
public function __construct(bool $checkFirstElement = false)
38+
{
39+
$this->checkFirstElement = $checkFirstElement;
40+
}
41+
3242
/**
3343
* {@inheritdoc}
3444
*
@@ -72,8 +82,15 @@ public function supportsDenormalization($data, string $type, string $format = nu
7282
throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__));
7383
}
7484

75-
return str_ends_with($type, '[]')
76-
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
85+
if (!str_ends_with($type, '[]')) {
86+
return false;
87+
}
88+
89+
if ($this->checkFirstElement) {
90+
$data = \count($data) === 0 ? null : reset($data);
91+
}
92+
93+
return $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
7794
}
7895

7996
/**

‎src/Symfony/Component/Serializer/Tests/Normalizer/ArrayDenormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ArrayDenormalizerTest.php
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,89 @@ public function testSupportsNoArray()
155155
)
156156
);
157157
}
158+
159+
public function testSupportsOtherDatatype()
160+
{
161+
$this->assertFalse(
162+
$this->denormalizer->supportsDenormalization(
163+
'83fd8e7c-61d4-4318-af88-fb34bd05e31f',
164+
__NAMESPACE__.'\Uuid'
165+
)
166+
);
167+
168+
$denormalizer2 = new ArrayDenormalizer(true);
169+
$denormalizer2->setDenormalizer($this->serializer);
170+
171+
$this->assertFalse(
172+
$denormalizer2->supportsDenormalization(
173+
'83fd8e7c-61d4-4318-af88-fb34bd05e31f',
174+
__NAMESPACE__.'\Uuid'
175+
)
176+
);
177+
}
178+
179+
public function testSupportsValidFirstArrayElement()
180+
{
181+
$denormalizer = new ArrayDenormalizer(true);
182+
$denormalizer->setDenormalizer($this->serializer);
183+
184+
$this->serializer->expects($this->once())
185+
->method('supportsDenormalization')
186+
->with(['foo' => 'one', 'bar' => 'two'], ArrayDummy::class, 'json', [])
187+
->willReturn(true);
188+
189+
$this->assertTrue(
190+
$denormalizer->supportsDenormalization(
191+
[
192+
['foo' => 'one', 'bar' => 'two'],
193+
['foo' => 'three', 'bar' => 'four'],
194+
],
195+
__NAMESPACE__.'\ArrayDummy[]',
196+
'json'
197+
)
198+
);
199+
}
200+
201+
public function testSupportsInValidFirstArrayElement()
202+
{
203+
$denormalizer = new ArrayDenormalizer(true);
204+
$denormalizer->setDenormalizer($this->serializer);
205+
206+
$this->serializer->expects($this->once())
207+
->method('supportsDenormalization')
208+
->with(['foo' => 'one', 'bar' => 'two'], ArrayDummy::class, 'json', [])
209+
->willReturn(false);
210+
211+
$this->assertFalse(
212+
$denormalizer->supportsDenormalization(
213+
[
214+
['foo' => 'one', 'bar' => 'two'],
215+
['foo' => 'three', 'bar' => 'four'],
216+
],
217+
__NAMESPACE__.'\ArrayDummy[]',
218+
'json'
219+
)
220+
);
221+
}
222+
223+
public function testSupportsNoFirstArrayElement()
224+
{
225+
$denormalizer = new ArrayDenormalizer(true);
226+
$denormalizer->setDenormalizer($this->serializer);
227+
228+
$this->serializer->expects($this->once())
229+
->method('supportsDenormalization')
230+
->with($this->isNull(), ArrayDummy::class, 'json', [])
231+
->willReturn(true);
232+
233+
$this->assertTrue(
234+
$denormalizer->supportsDenormalization(
235+
[],
236+
__NAMESPACE__.'\ArrayDummy[]',
237+
'json'
238+
)
239+
);
240+
}
158241
}
159242

160243
class ArrayDummy

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.