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
@@ -58,17 +60,20 @@ public function hydrate(ClassMetadata $metadata, array $data, Stack $stack): obj
5860 continue ;
5961 }
6062
61- $ normalizer = $ propertyMetadata ->normalizer ;
62-
63- if ($ normalizer ) {
63+ if ($ propertyMetadata ->normalizer ) {
6464 try {
65- /** @psalm-suppress MixedAssignment */
66- $ value = $ normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ]);
65+ if ($ propertyMetadata ->normalizer instanceof ContextAwareNormalizer) {
66+ /** @psalm-suppress MixedAssignment */
67+ $ value = $ propertyMetadata ->normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ], $ context );
68+ } else {
69+ /** @psalm-suppress MixedAssignment */
70+ $ value = $ propertyMetadata ->normalizer ->denormalize ($ data [$ propertyMetadata ->fieldName ]);
71+ }
6772 } catch (Throwable $ e ) {
6873 throw new DenormalizationFailure (
6974 $ metadata ->className ,
7075 $ propertyMetadata ->propertyName ,
71- $ normalizer ::class,
76+ $ propertyMetadata -> normalizer ::class,
7277 $ e ,
7378 );
7479 }
@@ -90,8 +95,12 @@ public function hydrate(ClassMetadata $metadata, array $data, Stack $stack): obj
9095 return $ object ;
9196 }
9297
93- /** @return array<string, mixed> */
94- public function extract (ClassMetadata $ metadata , object $ object , Stack $ stack ): array
98+ /**
99+ * @param array<string, mixed> $context
100+ *
101+ * @return array<string, mixed>
102+ */
103+ public function extract (ClassMetadata $ metadata , object $ object , array $ context , Stack $ stack ): array
95104 {
96105 $ objectId = spl_object_id ($ object );
97106
@@ -110,10 +119,18 @@ public function extract(ClassMetadata $metadata, object $object, Stack $stack):
110119 foreach ($ metadata ->properties as $ propertyMetadata ) {
111120 if ($ propertyMetadata ->normalizer ) {
112121 try {
113- /** @psalm-suppress MixedAssignment */
114- $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
115- $ propertyMetadata ->getValue ($ object ),
116- );
122+ if ($ propertyMetadata ->normalizer instanceof ContextAwareNormalizer) {
123+ /** @psalm-suppress MixedAssignment */
124+ $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
125+ $ propertyMetadata ->getValue ($ object ),
126+ $ context ,
127+ );
128+ } else {
129+ /** @psalm-suppress MixedAssignment */
130+ $ data [$ propertyMetadata ->fieldName ] = $ propertyMetadata ->normalizer ->normalize (
131+ $ propertyMetadata ->getValue ($ object ),
132+ );
133+ }
117134 } catch (CircularReference $ e ) {
118135 throw $ e ;
119136 } catch (Throwable $ e ) {
0 commit comments