class MultiresponseJsonNormalizer in Subrequests 3.x
Same name and namespace in other branches
- 8.2 src/Normalizer/MultiresponseJsonNormalizer.php \Drupal\subrequests\Normalizer\MultiresponseJsonNormalizer
Normalizes multiple response objects into a single string.
Hierarchy
- class \Drupal\subrequests\Normalizer\MultiresponseJsonNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface
Expanded class hierarchy of MultiresponseJsonNormalizer
1 file declares its use of MultiresponseJsonNormalizer
- MultiresponseJsonNormalizerTest.php in tests/
src/ Unit/ Normalizer/ MultiresponseJsonNormalizerTest.php
1 string reference to 'MultiresponseJsonNormalizer'
1 service uses MultiresponseJsonNormalizer
File
- src/
Normalizer/ MultiresponseJsonNormalizer.php, line 12
Namespace
Drupal\subrequests\NormalizerView source
class MultiresponseJsonNormalizer implements NormalizerInterface {
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
// Prepare the root content type header.
$content_type = sprintf('application/json; type=%s', $context['sub-content-type']);
$headers = [
'Content-Type' => $content_type,
];
// Join the content responses as a JSON object with the separator.
$output = array_reduce((array) $object, function ($carry, Response $part_response) {
$part_response->headers
->set('Status', $part_response
->getStatusCode());
$content_id = $part_response->headers
->get('Content-ID');
$content_id = substr($content_id, 1, strlen($content_id) - 2);
$carry[$content_id] = [
'headers' => $part_response->headers
->all(),
'body' => $part_response
->getContent(),
];
return $carry;
}, []);
$content = Json::encode($output);
return [
'content' => $content,
'headers' => $headers,
];
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = NULL) {
if ($format !== 'json') {
return FALSE;
}
if (!is_array($data)) {
return FALSE;
}
$responses = array_filter($data, function ($response) {
return $response instanceof Response;
});
if (count($responses) !== count($data)) {
return FALSE;
}
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MultiresponseJsonNormalizer:: |
public | function | ||
MultiresponseJsonNormalizer:: |
public | function |