66
77use Illuminate \Http \Response as IlluminateResponse ;
88use Symfony \Component \HttpFoundation \Response ;
9- use Symfony \ Component \ Serializer \SerializerInterface ;
9+ use WayOfDev \ Serializer \ Contracts \SerializerInterface ;
1010
1111final class ResponseFactory
1212{
13+ /**
14+ * @property SerializerInterface $serializer
15+ */
1316 private SerializerInterface $ serializer ;
1417
1518 private array $ context = [];
1619
1720 private int $ status = Response::HTTP_OK ;
1821
19- public function __construct (SerializerInterface $ serializer )
22+ public function __construct (SerializerManager $ serializer )
2023 {
21- $ this ->serializer = $ serializer ;
24+ $ this ->serializer = $ serializer-> getSerializer ( ' json ' ) ;
2225 }
2326
2427 public function withStatusCode (int $ code ): void
@@ -33,24 +36,28 @@ public function withContext(array $context): void
3336
3437 public function create (object $ response ): Response
3538 {
36- $ content = $ this ->serializer ->serialize (
37- $ response ,
38- 'json ' ,
39- $ this ->context
39+ $ content = $ this ->serializer ->normalize (
40+ data: $ response ,
41+ context: $ this ->context
4042 );
4143
42- return $ this ->respondWithJson ($ content , $ this ->status );
44+ return $ this ->respondWithJson (
45+ $ this ->serializer ->serialize ($ content ),
46+ $ this ->status
47+ );
4348 }
4449
4550 public function fromArray (array $ response ): Response
4651 {
47- $ content = $ this ->serializer ->serialize (
48- $ response ,
49- 'json ' ,
50- $ this ->context
52+ $ content = $ this ->serializer ->normalize (
53+ data: $ response ,
54+ context: $ this ->context
5155 );
5256
53- return $ this ->respondWithJson ($ content , $ this ->status );
57+ return $ this ->respondWithJson (
58+ $ this ->serializer ->serialize ($ content ),
59+ $ this ->status
60+ );
5461 }
5562
5663 private function respondWithJson ($ content , int $ status ): IlluminateResponse
0 commit comments