1515use ReflectionClass ;
1616
1717use function array_key_exists ;
18+ use function is_array ;
1819
1920use const PHP_VERSION_ID ;
2021
@@ -33,21 +34,34 @@ public function __construct(
3334
3435 /**
3536 * @param class-string<T> $class
36- * @param array<string, mixed> $data
3737 * @param array<string, mixed> $context
3838 *
3939 * @return T
4040 *
4141 * @template T of object
4242 */
43- public function hydrate (string $ class , array $ data , array $ context = []): object
43+ public function hydrate (string $ class , mixed $ data , array $ context = []): object
4444 {
4545 try {
4646 $ metadata = $ this ->metadata ($ class );
4747 } catch (ClassNotFound $ e ) {
4848 throw new ClassNotSupported ($ class , $ e );
4949 }
5050
51+ if ($ metadata ->normalizer ) {
52+ $ return = $ metadata ->normalizer ->denormalize ($ data , $ context );
53+
54+ if (!$ return instanceof $ class ) {
55+ throw new ObjectRequired ($ class , $ metadata ->normalizer ::class);
56+ }
57+
58+ return $ return ;
59+ }
60+
61+ if (!is_array ($ data )) {
62+ throw new ArrayDataRequired ($ class );
63+ }
64+
5165 if (PHP_VERSION_ID < 80400 ) {
5266 $ stack = new Stack ($ this ->middlewares );
5367
@@ -71,14 +85,15 @@ function () use ($metadata, $data, $context): object {
7185 );
7286 }
7387
74- /**
75- * @param array<string, mixed> $context
76- *
77- * @return array<string, mixed>
78- */
79- public function extract (object $ object , array $ context = []): array
88+ /** @param array<string, mixed> $context */
89+ public function extract (object $ object , array $ context = []): mixed
8090 {
8191 $ metadata = $ this ->metadata ($ object ::class);
92+
93+ if ($ metadata ->normalizer ) {
94+ return $ metadata ->normalizer ->normalize ($ object , $ context );
95+ }
96+
8297 $ stack = new Stack ($ this ->middlewares );
8398
8499 return $ stack ->next ()->extract ($ metadata , $ object , $ context , $ stack );
0 commit comments