88use Patchlevel \Hydrator \DenormalizationFailure ;
99use Patchlevel \Hydrator \Metadata \ClassMetadata ;
1010use Patchlevel \Hydrator \NormalizationFailure ;
11+ use Patchlevel \Hydrator \Normalizer \ContextAwareNormalizer ;
1112use Patchlevel \Hydrator \TypeMismatch ;
1213use ReflectionParameter ;
1314use Throwable ;
@@ -25,12 +26,13 @@ final class TransformMiddleware implements Middleware
2526 /**
2627 * @param ClassMetadata<T> $metadata
2728 * @param array<string, mixed> $data
29+ * @param array<string, mixed> $context
2830 *
2931 * @return T
3032 *
3133 * @template T of object
3234 */
33- public function hydrate (ClassMetadata $ metadata , array $ data , Stack $ stack ): object
35+ public function hydrate (ClassMetadata $ metadata , array $ data , array $ context , Stack $ stack ): object
3436 {
3537 $ object = $ metadata ->newInstance ();
3638
@@ -62,8 +64,14 @@ public function hydrate(ClassMetadata $metadata, array $data, Stack $stack): obj
6264
6365 if ($ normalizer ) {
6466 try {
65- /** @psalm-suppress MixedAssignment */
66- $ value = $ normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ]);
67+ if ($ propertyMetadata ->normalizer instanceof ContextAwareNormalizer) {
68+ /** @psalm-suppress MixedAssignment */
69+ $ value = $ normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ], $ context );
70+
71+ } else {
72+ /** @psalm-suppress MixedAssignment */
73+ $ value = $ normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ]);
74+ }
6775 } catch (Throwable $ e ) {
6876 throw new DenormalizationFailure (
6977 $ metadata ->className ,
@@ -90,8 +98,12 @@ public function hydrate(ClassMetadata $metadata, array $data, Stack $stack): obj
9098 return $ object ;
9199 }
92100
93- /** @return array<string, mixed> */
94- public function extract (ClassMetadata $ metadata , object $ object , Stack $ stack ): array
101+ /**
102+ * @param array<string, mixed> $context
103+ *
104+ * @return array<string, mixed>
105+ */
106+ public function extract (ClassMetadata $ metadata , object $ object , array $ context , Stack $ stack ): array
95107 {
96108 $ objectId = spl_object_id ($ object );
97109
@@ -110,10 +122,18 @@ public function extract(ClassMetadata $metadata, object $object, Stack $stack):
110122 foreach ($ metadata ->properties as $ propertyMetadata ) {
111123 if ($ propertyMetadata ->normalizer ) {
112124 try {
113- /** @psalm-suppress MixedAssignment */
114- $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
115- $ propertyMetadata ->getValue ($ object ),
116- );
125+ if ($ propertyMetadata ->normalizer instanceof ContextAwareNormalizer) {
126+ /** @psalm-suppress MixedAssignment */
127+ $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
128+ $ propertyMetadata ->getValue ($ object ),
129+ $ context ,
130+ );
131+ } else {
132+ /** @psalm-suppress MixedAssignment */
133+ $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
134+ $ propertyMetadata ->getValue ($ object ),
135+ );
136+ }
117137 } catch (CircularReference $ e ) {
118138 throw $ e ;
119139 } catch (Throwable $ e ) {
0 commit comments