File tree 5 files changed +35
-2
lines changed
src/Symfony/Component/Serializer
Tests/Normalizer/Features
5 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 17
17
* Annotation class for @Context().
18
18
*
19
19
* @Annotation
20
+ *
20
21
* @NamedArgumentConstructor
22
+ *
21
23
* @Target({"PROPERTY", "METHOD"})
22
24
*
23
25
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
24
26
*/
25
- #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )]
27
+ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute:: TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )]
26
28
class Context
27
29
{
28
30
private array $ groups ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ CHANGELOG
4
4
6.4
5
5
---
6
6
7
+ * Allow ` Context ` attribute to target class
7
8
* Deprecate Doctrine annotations support in favor of native attributes
8
9
* Deprecate passing an annotation reader to the constructor of ` AnnotationLoader `
9
10
* Allow the ` Groups ` attribute/annotation on classes
Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
57
57
$ className = $ reflectionClass ->name ;
58
58
$ loaded = false ;
59
59
$ classGroups = [];
60
+ $ classContextAnnotation = null ;
60
61
61
62
$ attributesMetadata = $ classMetadata ->getAttributesMetadata ();
62
63
@@ -71,6 +72,12 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
71
72
72
73
if ($ annotation instanceof Groups) {
73
74
$ classGroups = $ annotation ->getGroups ();
75
+
76
+ continue ;
77
+ }
78
+
79
+ if ($ annotation instanceof Context) {
80
+ $ classContextAnnotation = $ annotation ;
74
81
}
75
82
}
76
83
@@ -81,6 +88,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
81
88
}
82
89
83
90
if ($ property ->getDeclaringClass ()->name === $ className ) {
91
+ if (null !== $ classContextAnnotation ) {
92
+ $ this ->setAttributeContextsForGroups ($ annotation , $ attributesMetadata [$ property ->name ]);
93
+ }
94
+
84
95
foreach ($ classGroups as $ group ) {
85
96
$ attributesMetadata [$ property ->name ]->addGroup ($ group );
86
97
}
Original file line number Diff line number Diff line change @@ -82,6 +82,7 @@ public function contextMetadataDummyProvider(): array
82
82
return [
83
83
[ContextMetadataDummy::class],
84
84
[ContextChildMetadataDummy::class],
85
+ [ClassAndPropertyContextMetadataDummy::class],
85
86
];
86
87
}
87
88
@@ -133,6 +134,24 @@ class ContextChildMetadataDummy
133
134
public $ date ;
134
135
}
135
136
137
+ #[Context(context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339 ])]
138
+ #[Context(
139
+ context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339_EXTENDED ],
140
+ groups: ['extended ' ],
141
+ )]
142
+ class ClassAndPropertyContextMetadataDummy
143
+ {
144
+ /**
145
+ * @var \DateTime
146
+ */
147
+ #[Groups(['extended ' , 'simple ' ])]
148
+ #[Context(
149
+ denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'd/m/Y ' ],
150
+ groups: ['simple ' ],
151
+ )]
152
+ public $ date ;
153
+ }
154
+
136
155
class ContextMetadataNamingDummy
137
156
{
138
157
/**
Original file line number Diff line number Diff line change 13
13
14
14
use Symfony \Component \Serializer \Annotation \Context ;
15
15
16
- #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE )]
16
+ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute:: TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE )]
17
17
class DummyContextChild extends Context
18
18
{
19
19
}
You can’t perform that action at this time.
0 commit comments