Skip to content

Commit eeab858

Browse files
committed
Add full batch support without breaking BC
1 parent 5de90f6 commit eeab858

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

src/Facebook/FacebookBatchRequest.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,17 @@ public function __construct(FacebookApp $app = null, array $requests = [], $acce
6262
}
6363

6464
/**
65-
* A a new request to the array.
65+
* Adds a new request to the array.
6666
*
6767
* @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.
6970
*
7071
* @return FacebookBatchRequest
7172
*
7273
* @throws \InvalidArgumentException
7374
*/
74-
public function add($request, $name = null)
75+
public function add($request, $options = null)
7576
{
7677
if (is_array($request)) {
7778
foreach ($request as $key => $req) {
@@ -85,17 +86,28 @@ public function add($request, $name = null)
8586
throw new \InvalidArgumentException('Argument for add() must be of type array or FacebookRequest.');
8687
}
8788

89+
if (null === $options) {
90+
$options = [];
91+
} elseif (!is_array($options)) {
92+
$options = ['name' => $options];
93+
}
94+
8895
$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+
89104
$requestToAdd = [
90105
'name' => $name,
91106
'request' => $request,
107+
'options' => $options,
108+
'attached_files' => $attachedFiles,
92109
];
93110

94-
// File uploads
95-
$attachedFiles = $this->extractFileAttachments($request);
96-
if ($attachedFiles) {
97-
$requestToAdd['attached_files'] = $attachedFiles;
98-
}
99111
$this->requests[] = $requestToAdd;
100112

101113
return $this;
@@ -189,8 +201,15 @@ public function convertRequestsToJson()
189201
{
190202
$requests = [];
191203
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']);
194213
}
195214

196215
return json_encode($requests);
@@ -215,14 +234,22 @@ public function validateBatchRequestCount()
215234
/**
216235
* Converts a Request entity into an array that is batch-friendly.
217236
*
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.
221241
*
222242
* @return array
223243
*/
224-
public function requestEntityToBatchArray(FacebookRequest $request, $requestName = null, $attachedFiles = null)
244+
public function requestEntityToBatchArray(FacebookRequest $request, $options = null, $attachedFiles = null)
225245
{
246+
247+
if (null === $options) {
248+
$options = [];
249+
} elseif (!is_array($options)) {
250+
$options = ['name' => $options];
251+
}
252+
226253
$compiledHeaders = [];
227254
$headers = $request->getHeaders();
228255
foreach ($headers as $name => $value) {
@@ -242,18 +269,12 @@ public function requestEntityToBatchArray(FacebookRequest $request, $requestName
242269
$batch['body'] = $body;
243270
}
244271

245-
if (isset($requestName)) {
246-
$batch['name'] = $requestName;
247-
}
272+
$batch += $options;
248273

249-
if (isset($attachedFiles)) {
274+
if (null !== $attachedFiles) {
250275
$batch['attached_files'] = $attachedFiles;
251276
}
252277

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-
257278
return $batch;
258279
}
259280

0 commit comments

Comments
 (0)