@@ -62,16 +62,17 @@ public function __construct(FacebookApp $app = null, array $requests = [], $acce
62
62
}
63
63
64
64
/**
65
- * A a new request to the array.
65
+ * Adds a new request to the array.
66
66
*
67
67
* @param FacebookRequest|array $request
68
- * @param string|null $name
68
+ * @param string|null|array $options Array of batch request options e.g. 'name', 'omit_response_on_success'.
69
+ * If a string is given, it is the value of the 'name' option.
69
70
*
70
71
* @return FacebookBatchRequest
71
72
*
72
73
* @throws \InvalidArgumentException
73
74
*/
74
- public function add ($ request , $ name = null )
75
+ public function add ($ request , $ options = null )
75
76
{
76
77
if (is_array ($ request )) {
77
78
foreach ($ request as $ key => $ req ) {
@@ -85,17 +86,28 @@ public function add($request, $name = null)
85
86
throw new \InvalidArgumentException ('Argument for add() must be of type array or FacebookRequest. ' );
86
87
}
87
88
89
+ if (null === $ options ) {
90
+ $ options = [];
91
+ } elseif (!is_array ($ options )) {
92
+ $ options = ['name ' => $ options ];
93
+ }
94
+
88
95
$ this ->addFallbackDefaults ($ request );
96
+
97
+ // File uploads
98
+ $ attachedFiles = $ this ->extractFileAttachments ($ request );
99
+
100
+ $ name = isset ($ options ['name ' ]) ? $ options ['name ' ] : null ;
101
+
102
+ unset($ options ['name ' ]);
103
+
89
104
$ requestToAdd = [
90
105
'name ' => $ name ,
91
106
'request ' => $ request ,
107
+ 'options ' => $ options ,
108
+ 'attached_files ' => $ attachedFiles ,
92
109
];
93
110
94
- // File uploads
95
- $ attachedFiles = $ this ->extractFileAttachments ($ request );
96
- if ($ attachedFiles ) {
97
- $ requestToAdd ['attached_files ' ] = $ attachedFiles ;
98
- }
99
111
$ this ->requests [] = $ requestToAdd ;
100
112
101
113
return $ this ;
@@ -189,8 +201,15 @@ public function convertRequestsToJson()
189
201
{
190
202
$ requests = [];
191
203
foreach ($ this ->requests as $ request ) {
192
- $ attachedFiles = isset ($ request ['attached_files ' ]) ? $ request ['attached_files ' ] : null ;
193
- $ requests [] = $ this ->requestEntityToBatchArray ($ request ['request ' ], $ request ['name ' ], $ attachedFiles );
204
+ $ options = [];
205
+
206
+ if (null !== $ request ['name ' ]) {
207
+ $ options ['name ' ] = $ request ['name ' ];
208
+ }
209
+
210
+ $ options += $ request ['options ' ];
211
+
212
+ $ requests [] = $ this ->requestEntityToBatchArray ($ request ['request ' ], $ options , $ request ['attached_files ' ]);
194
213
}
195
214
196
215
return json_encode ($ requests );
@@ -215,14 +234,22 @@ public function validateBatchRequestCount()
215
234
/**
216
235
* Converts a Request entity into an array that is batch-friendly.
217
236
*
218
- * @param FacebookRequest $request The request entity to convert.
219
- * @param string|null $requestName The name of the request.
220
- * @param string|null $attachedFiles Names of files associated with the request.
237
+ * @param FacebookRequest $request The request entity to convert.
238
+ * @param string|null|array $options Array of batch request options e.g. 'name', 'omit_response_on_success'.
239
+ * If a string is given, it is the value of the 'name' option.
240
+ * @param string|null $attachedFiles Names of files associated with the request.
221
241
*
222
242
* @return array
223
243
*/
224
- public function requestEntityToBatchArray (FacebookRequest $ request , $ requestName = null , $ attachedFiles = null )
244
+ public function requestEntityToBatchArray (FacebookRequest $ request , $ options = null , $ attachedFiles = null )
225
245
{
246
+
247
+ if (null === $ options ) {
248
+ $ options = [];
249
+ } elseif (!is_array ($ options )) {
250
+ $ options = ['name ' => $ options ];
251
+ }
252
+
226
253
$ compiledHeaders = [];
227
254
$ headers = $ request ->getHeaders ();
228
255
foreach ($ headers as $ name => $ value ) {
@@ -242,18 +269,12 @@ public function requestEntityToBatchArray(FacebookRequest $request, $requestName
242
269
$ batch ['body ' ] = $ body ;
243
270
}
244
271
245
- if (isset ($ requestName )) {
246
- $ batch ['name ' ] = $ requestName ;
247
- }
272
+ $ batch += $ options ;
248
273
249
- if (isset ( $ attachedFiles) ) {
274
+ if (null !== $ attachedFiles ) {
250
275
$ batch ['attached_files ' ] = $ attachedFiles ;
251
276
}
252
277
253
- // @TODO Add support for "omit_response_on_success"
254
- // @TODO Add support for "depends_on"
255
- // @TODO Add support for JSONP with "callback"
256
-
257
278
return $ batch ;
258
279
}
259
280
0 commit comments