Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Facebook/FacebookBatchResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function addResponse($key, $response)

$httpResponseBody = isset($response['body']) ? $response['body'] : null;
$httpResponseCode = isset($response['code']) ? $response['code'] : null;
$httpResponseHeaders = isset($response['headers']) ? $response['headers'] : [];
// @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name')
$httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless private function, it just can be array_column($response['headers'], 'value', 'name') ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and your efficient ways. :) Since array_column() is a PHP 5.5 feature, we'll need to wait to add this until PHP SDK v6.0 (which I'm assuming will require PHP 5.5.) :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right!! My bad, I forgot that we support 5.4 in v5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About v6.0, it can even require at least 5.6 :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the way you think. :)


$this->responses[$originalRequestName] = new FacebookResponse(
$originalRequest,
Expand Down Expand Up @@ -151,4 +152,23 @@ public function offsetGet($offset)
{
return isset($this->responses[$offset]) ? $this->responses[$offset] : null;
}

/**
* Converts the batch header array into a standard format.
* @TODO replace with array_column() when PHP 5.5 is supported.
*
* @param array $batchHeaders
*
* @return array
*/
private function normalizeBatchHeaders(array $batchHeaders)
{
$headers = [];

foreach ($batchHeaders as $header) {
$headers[$header['name']] = $header['value'];
}

return $headers;
}
}
26 changes: 26 additions & 0 deletions tests/FacebookBatchResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,30 @@ public function testTheOriginalRequestCanBeObtainedForEachRequest()
$this->assertEquals('foo_token_two', $batchResponse[1]->getAccessToken());
$this->assertEquals('foo_token_three', $batchResponse[2]->getAccessToken());
}

public function testHeadersFromBatchRequestCanBeAccessed()
{
$graphResponseJson = '[';
$graphResponseJson .= '{"code":200,"headers":[{"name":"Facebook-API-Version","value":"v2.0"},{"name":"ETag","value":"\"fooTag\""}],"body":"{\"foo\":\"bar\"}"}';
$graphResponseJson .= ',{"code":200,"headers":[{"name":"Facebook-API-Version","value":"v2.5"},{"name":"ETag","value":"\"barTag\""}],"body":"{\"foo\":\"bar\"}"}';
$graphResponseJson .= ']';
$response = new FacebookResponse($this->request, $graphResponseJson, 200);

$requests = [
new FacebookRequest($this->app, 'foo_token_one', 'GET', '/me'),
new FacebookRequest($this->app, 'foo_token_two', 'GET', '/you'),
];

$batchRequest = new FacebookBatchRequest($this->app, $requests);
$batchResponse = new FacebookBatchResponse($batchRequest, $response);

$this->assertEquals('v2.0', $batchResponse[0]->getGraphVersion());
$this->assertEquals('"fooTag"', $batchResponse[0]->getETag());
$this->assertEquals('v2.5', $batchResponse[1]->getGraphVersion());
$this->assertEquals('"barTag"', $batchResponse[1]->getETag());
$this->assertEquals([
'Facebook-API-Version' => 'v2.5',
'ETag' => '"barTag"',
], $batchResponse[1]->getHeaders());
}
}