@@ -34,35 +34,43 @@ public function __construct(
3434
3535 public function process (mixed $ data , Operation $ operation , array $ uriVariables = [], array $ context = []): object |array |null
3636 {
37- $ class = $ operation ->getInput ()['class ' ] ?? $ operation ->getClass ();
38-
3937 if (
4038 $ data instanceof Response
4139 || !$ this ->objectMapper
4240 || !$ operation ->canWrite ()
4341 || null === $ data
44- || !is_a ($ data , $ class , true )
4542 || !$ operation ->canMap ()
4643 ) {
4744 return $ this ->decorated ->process ($ data , $ operation , $ uriVariables , $ context );
4845 }
4946
5047 $ request = $ context ['request ' ] ?? null ;
51- $ persisted = $ this ->decorated ->process (
52- // maps the Resource to an Entity
53- $ this ->objectMapper ->map ($ data , $ request ?->attributes->get ('mapped_data ' )),
54- $ operation ,
55- $ uriVariables ,
56- $ context ,
57- );
48+ $ resourceClass = $ operation ->getClass ();
49+ $ inputClass = $ operation ->getInput ()['class ' ] ?? null ;
50+ $ outputClass = $ operation ->getOutput ()['class ' ] ?? null ;
51+
52+ $ hasCustomInput = null !== $ inputClass && $ inputClass !== $ resourceClass ;
53+ $ hasCustomOutput = null !== $ outputClass && $ outputClass !== $ resourceClass ;
54+
55+ if (!$ hasCustomInput && !$ hasCustomOutput ) {
56+ return $ this ->decorated ->process ($ data , $ operation , $ uriVariables , $ context );
57+ }
5858
59+ if ($ hasCustomInput ) {
60+ if (!is_a ($ data , $ inputClass , true )) {
61+ return $ this ->decorated ->process ($ data , $ operation , $ uriVariables , $ context );
62+ }
63+
64+ $ data = $ this ->objectMapper ->map ($ data , $ request ?->attributes->get ('mapped_data ' ));
65+ }
66+
67+ $ persisted = $ this ->decorated ->process ($ data , $ operation , $ uriVariables , $ context );
5968 $ request ?->attributes->set ('persisted_data ' , $ persisted );
6069
61- // return the Resource representation of the persisted entity
62- return $ this ->objectMapper ->map (
63- // persist the entity
64- $ persisted ,
65- $ operation ->getClass ()
66- );
70+ if ($ hasCustomOutput ) {
71+ return $ this ->objectMapper ->map ($ persisted , $ outputClass );
72+ }
73+
74+ return $ persisted ;
6775 }
6876}
0 commit comments