1212use Patchlevel \Hydrator \Middleware \Stack ;
1313use Patchlevel \Hydrator \Middleware \TransformMiddleware ;
1414use Patchlevel \Hydrator \Normalizer \HydratorAwareNormalizer ;
15+ use Patchlevel \Hydrator \Normalizer \InvalidType ;
1516use ReflectionClass ;
1617
1718use function array_key_exists ;
19+ use function is_array ;
1820
1921use const PHP_VERSION_ID ;
2022
@@ -33,21 +35,28 @@ public function __construct(
3335
3436 /**
3537 * @param class-string<T> $class
36- * @param array<string, mixed> $data
3738 * @param array<string, mixed> $context
3839 *
3940 * @return T
4041 *
4142 * @template T of object
4243 */
43- public function hydrate (string $ class , array $ data , array $ context = []): object
44+ public function hydrate (string $ class , mixed $ data , array $ context = []): object
4445 {
4546 try {
4647 $ metadata = $ this ->metadata ($ class );
4748 } catch (ClassNotFound $ e ) {
4849 throw new ClassNotSupported ($ class , $ e );
4950 }
5051
52+ if ($ metadata ->normalizer ) {
53+ return $ metadata ->normalizer ->denormalize ($ data , $ context );
54+ }
55+
56+ if (!is_array ($ data )) {
57+ throw new InvalidType ();
58+ }
59+
5160 if (PHP_VERSION_ID < 80400 ) {
5261 $ stack = new Stack ($ this ->middlewares );
5362
@@ -71,14 +80,15 @@ function () use ($metadata, $data, $context): object {
7180 );
7281 }
7382
74- /**
75- * @param array<string, mixed> $context
76- *
77- * @return array<string, mixed>
78- */
79- public function extract (object $ object , array $ context = []): array
83+ /** @param array<string, mixed> $context */
84+ public function extract (object $ object , array $ context = []): mixed
8085 {
8186 $ metadata = $ this ->metadata ($ object ::class);
87+
88+ if ($ metadata ->normalizer ) {
89+ return $ metadata ->normalizer ->normalize ($ object , $ context );
90+ }
91+
8292 $ stack = new Stack ($ this ->middlewares );
8393
8494 return $ stack ->next ()->extract ($ metadata , $ object , $ context , $ stack );
0 commit comments