diff --git a/src/EventSubscriber/ResponseSubscriber.php b/src/EventSubscriber/ResponseSubscriber.php index 753032e7c39e28c5941921a74f1513d5b2d84763..06eb3f11599dae44c1237e73916ff0b5d07fb446 100644 --- a/src/EventSubscriber/ResponseSubscriber.php +++ b/src/EventSubscriber/ResponseSubscriber.php @@ -101,8 +101,7 @@ class ResponseSubscriber implements EventSubscriberInterface { if ($need_parse) { $content = $response->getContent(); if (strpos($content, '{"jsonapi"') === 0) { - $content = $this->jsonapiInclude->parse($content); - $event->getResponse()->setContent($content); + $this->jsonapiInclude->parse($response); } } } diff --git a/src/JsonapiParse.php b/src/JsonapiParse.php index 9f7c2aa434ee48e3a1f404269e36172319af141d..88a57943653db2c4116827ff9dbc6a73bc378407 100644 --- a/src/JsonapiParse.php +++ b/src/JsonapiParse.php @@ -4,6 +4,7 @@ namespace Drupal\jsonapi_include; use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\NestedArray; +use Symfony\Component\HttpFoundation\Response; /** * The class for parse Jsonapi content. @@ -20,17 +21,17 @@ class JsonapiParse implements JsonapiParseInterface { protected $included; /** - * Parse json api. - * - * @param string|object $response - * The response data from jsonapi. - * - * @return mixed - * Parse jsonapi data. + * {@inheritdoc} */ public function parse($response) { - $content = $this->parseJsonContent($response); - return is_string($content) ? $content : Json::encode($content); + // @todo Remove in the release 2.0 with removed the deprecated srings input. + if (!$response instanceof Response) { + @trigger_error('Parsing strings is deprecated deprecated in jsonapi_include:8.x-1.7 and is removed from jsonapi_include:8.x-2.0. Pass the full Response object instead. See https://www.drupal.org/project/jsonapi_include/issues/3374410', E_USER_DEPRECATED); + $content = $this->parseJsonContent($response); + return Json::encode($content); + } + $this->parseJsonContent($response); + return $response; } /** @@ -202,46 +203,59 @@ class JsonapiParse implements JsonapiParseInterface { } /** - * Parse json content. + * Integrates includes into the content of a Response. * - * @param object $response - * The jsonapi object. + * @param \Symfony\Component\HttpFoundation\Response|string|array $response + * A Response object or string/array with a response content. * - * @return mixed - * Parse result. + * @return \Symfony\Component\HttpFoundation\Response|array + * Returns + * Returns an array with the response content, if the input is string or + * array, or void if input is a Response. */ protected function parseJsonContent($response) { - if (is_string($response)) { - $json = Json::decode($response); + if ($response instanceof Response) { + $content = $response->getContent(); + if (is_string($content)) { + $content = Json::decode($content); + } } - elseif (is_object($response)) { - $json = $response; + // @todo Remove in the release 2.0 with removed the deprecated srings input. + elseif (is_array($response)) { + $content = $response; } - else { - return $response; + elseif (is_string($response)) { + $content = Json::decode($response); } - if (NestedArray::getValue($json, ['jsonapi', 'parsed'])) { - return $json; + if (NestedArray::getValue($content, ['jsonapi', 'parsed'])) { + return $response; } - if (isset($json['errors']) || empty($json['data'])) { - return $json; + if (isset($content['errors']) || empty($content['data'])) { + return $response; } - $this->included = $this->groupIncludes($json); + $this->included = $this->groupIncludes($content); $data = []; - if (!$this->isAssoc($json['data'])) { - foreach ($json['data'] as $item) { + if (!$this->isAssoc($content['data'])) { + foreach ($content['data'] as $item) { $data[] = $this->parseResource($item); } } else { - $data = $this->parseResource($json['data']); + $data = $this->parseResource($content['data']); + } + if (isset($content['included'])) { + unset($content['included']); } - if (isset($json['included'])) { - unset($json['included']); + $content['jsonapi']['parsed'] = TRUE; + $content['data'] = $data; + if ($response instanceof Response) { + $response->setContent(Json::encode($content)); + return $response; + } + // @todo Remove in the release 2.0 with removed the deprecated srings input. + else { + return $content; } - $json['jsonapi']['parsed'] = TRUE; - $json['data'] = $data; - return $json; } } diff --git a/src/JsonapiParseInterface.php b/src/JsonapiParseInterface.php index b0f6cbd7f61a0a20459d69f3ece79b804d6312e4..9060bca05431c0f16f579f6e80bc3b17c56a470a 100644 --- a/src/JsonapiParseInterface.php +++ b/src/JsonapiParseInterface.php @@ -3,20 +3,25 @@ namespace Drupal\jsonapi_include; /** - * Interface for parse Jsonapi content. + * The interface for parsing the JSON:API Response. * * @package Drupal\jsonapi_include */ interface JsonapiParseInterface { /** - * Parse json api. + * Parses the JSON:API Response with integrating includes inside the fields. * - * @param string|object $response - * The response data from jsonapi. + * @param \Symfony\Component\HttpFoundation\Response|string $response + * A Response object with JSON:API response. + * Or string with a Response body (deprecated). * - * @return mixed - * Parse jsonapi data. + * @return \Symfony\Component\HttpFoundation\Response|string + * Returns the Response object, if the input is a Response object. + * Or return a string, if the input is in the string format (deprecated). + * + * @todo With 2.0 release remove the string format and explicitly set the + * Response as the argument and the return value. */ public function parse($response);