Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commit527bc77

Browse files
committed
[Serializer] Add the possibilitiy to filter attributes
1 parent7c5dcfc commit527bc77

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,15 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
236236
*/
237237
protectedfunctionisAllowedAttribute($classOrObject,$attribute,$format =null,array$context =array())
238238
{
239-
return !in_array($attribute,$this->ignoredAttributes);
239+
if (in_array($attribute,$this->ignoredAttributes)) {
240+
returnfalse;
241+
}
242+
243+
if (isset($context['attributes']) &&is_array($context['attributes'])) {
244+
returnin_array($attribute,$context['attributes'],true);
245+
}
246+
247+
returntrue;
240248
}
241249

242250
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,33 @@ public function testExtractAttributesRespectsContext()
643643

644644
$this->assertSame(array('foo' =>'bar','bar' =>'foo'),$normalizer->normalize($data,null,array('include_foo_and_bar' =>true)));
645645
}
646+
647+
publicfunctiontestAttributesContextNormalize()
648+
{
649+
$normalizer =newObjectNormalizer();
650+
651+
$objectDummy =newObjectDummy();
652+
$objectDummy->setFoo('foo');
653+
$objectDummy->setBaz('baz');
654+
655+
$context =array('attributes' =>array('foo','baz'));
656+
$this->assertEquals(array('foo' =>'foo','baz' =>'baz'),$normalizer->normalize($objectDummy,null,$context));
657+
}
658+
659+
publicfunctiontestAttributesContextDenormalize()
660+
{
661+
$normalizer =newObjectNormalizer();
662+
663+
$expected =newObjectDummy();
664+
$expected->setFoo('foo');
665+
$expected->setBaz('baz');
666+
667+
$context =array('attributes' =>array('foo','baz'));
668+
$this->assertEquals(
669+
$expected,
670+
$normalizer->denormalize(array('foo' =>'foo','baz' =>'baz','bar' =>'bar'), ObjectDummy::class,null,$context)
671+
);
672+
}
646673
}
647674

648675
class ObjectDummy

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp