@@ -20,12 +20,12 @@ final class ResponseFactory
2020{
2121 /**
2222 * @template TBody
23- * @param TBody|null $body will be encoded as json
23+ * @param TBody|null $body will be encoded as application/ json object
2424 * @param Transformer<TBody>|null $transformer
2525 * @param int<100,505> $code
2626 * @param array<non-empty-string, string|array<string>> $headers
2727 */
28- public static function makeJson (mixed $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
28+ public static function makeObject (mixed $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
2929 {
3030 $ headers ['Content-Type ' ] = 'application/json ' ;
3131 $ factory = new HttpFactory ();
@@ -39,6 +39,32 @@ public static function makeJson(mixed $body, ?Transformer $transformer = null, i
3939 }
4040
4141 /**
42+ * @deprecated use ResponseFactory::makeObject
43+ * @template TBody
44+ * @param TBody|null $body will be encoded as json
45+ * @param Transformer<TBody>|null $transformer
46+ * @param int<100,505> $code
47+ * @param array<non-empty-string, string|array<string>> $headers
48+ */
49+ public static function makeJson (mixed $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
50+ {
51+ return self ::makeObject ($ body , $ transformer , $ code , $ headers );
52+ }
53+
54+ /**
55+ * @template TBody
56+ * @param \Iterator<TBody> $body will be encoded as application/json array
57+ * @param Transformer<TBody>|null $transformer
58+ * @param int<100,505> $code
59+ * @param array<non-empty-string, string|array<string>> $headers
60+ */
61+ public static function makeArray (\Iterator $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
62+ {
63+ return self ::makeJsonStream ($ body , $ transformer , $ code , $ headers );
64+ }
65+
66+ /**
67+ * @deprecated use ResponseFactory::makeArray
4268 * @template TBody
4369 * @param \Iterator<TBody> $body will be encoded as json
4470 * @param Transformer<TBody>|null $transformer
@@ -87,12 +113,40 @@ public static function makeJsonStream(\Iterator $body, ?Transformer $transformer
87113 }));
88114 }
89115
116+ /**
117+ * @template TBody
118+ * @param \Iterator<TBody> $body will be encoded as application/jsonl https://jsonlines.org/
119+ * @param Transformer<TBody>|null $transformer
120+ * @param int<100,505> $code
121+ * @param array<non-empty-string, string|array<string>> $headers
122+ */
123+ public static function makeStream (\Iterator $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
124+ {
125+ $ headers ['Content-Type ' ] = 'application/jsonl ' ;
126+ $ factory = new HttpFactory ();
127+ $ response = self ::makeResponse ($ factory , $ code , $ headers );
128+
129+ return $ response ->withBody (new PumpStream (static function () use ($ body , $ transformer ): ?string {
130+ if (! $ body ->valid ()) {
131+ return null ;
132+ }
133+
134+ $ responseData = $ body ->current ();
135+ $ body ->next ();
136+ if ($ transformer !== null ) {
137+ $ responseData = $ transformer ->toApi ($ responseData );
138+ }
139+
140+ return Utils::jsonEncode ($ responseData ) . "\n" ;
141+ }));
142+ }
143+
90144 /**
91145 * @param array<non-empty-string, string|array<string>> $headers
92146 */
93147 public static function makeWebhookResult (bool $ stopDispatching = false , array $ headers = []): ResponseInterface
94148 {
95- return self ::makeJson (new Result ($ stopDispatching ), new ResultTransformer (), HttpCodes::HTTP_OK , $ headers );
149+ return self ::makeObject (new Result ($ stopDispatching ), new ResultTransformer (), HttpCodes::HTTP_OK , $ headers );
96150 }
97151
98152 /**
0 commit comments