Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\jsonapi;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Core\Cache\CacheableResponseInterface;
|
Chris@18
|
6 use Drupal\Core\Cache\CacheableResponseTrait;
|
Chris@18
|
7 use Symfony\Component\HttpFoundation\Response;
|
Chris@18
|
8
|
Chris@18
|
9 /**
|
Chris@18
|
10 * Contains data for serialization before sending the response.
|
Chris@18
|
11 *
|
Chris@18
|
12 * We do not want to abuse the $content property on the Response class to store
|
Chris@18
|
13 * our response data. $content implies that the provided data must either be a
|
Chris@18
|
14 * string or an object with a __toString() method, which is not a requirement
|
Chris@18
|
15 * for data used here.
|
Chris@18
|
16 *
|
Chris@18
|
17 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
|
Chris@18
|
18 * class may change at any time and this will break any dependencies on it.
|
Chris@18
|
19 *
|
Chris@18
|
20 * @see https://www.drupal.org/project/jsonapi/issues/3032787
|
Chris@18
|
21 * @see jsonapi.api.php
|
Chris@18
|
22 *
|
Chris@18
|
23 * @see \Drupal\rest\ModifiedResourceResponse
|
Chris@18
|
24 */
|
Chris@18
|
25 class ResourceResponse extends Response implements CacheableResponseInterface {
|
Chris@18
|
26
|
Chris@18
|
27 use CacheableResponseTrait;
|
Chris@18
|
28
|
Chris@18
|
29 /**
|
Chris@18
|
30 * Response data that should be serialized.
|
Chris@18
|
31 *
|
Chris@18
|
32 * @var mixed
|
Chris@18
|
33 */
|
Chris@18
|
34 protected $responseData;
|
Chris@18
|
35
|
Chris@18
|
36 /**
|
Chris@18
|
37 * Constructor for ResourceResponse objects.
|
Chris@18
|
38 *
|
Chris@18
|
39 * @param mixed $data
|
Chris@18
|
40 * Response data that should be serialized.
|
Chris@18
|
41 * @param int $status
|
Chris@18
|
42 * The response status code.
|
Chris@18
|
43 * @param array $headers
|
Chris@18
|
44 * An array of response headers.
|
Chris@18
|
45 */
|
Chris@18
|
46 public function __construct($data = NULL, $status = 200, array $headers = []) {
|
Chris@18
|
47 $this->responseData = $data;
|
Chris@18
|
48 parent::__construct('', $status, $headers);
|
Chris@18
|
49 }
|
Chris@18
|
50
|
Chris@18
|
51 /**
|
Chris@18
|
52 * Returns response data that should be serialized.
|
Chris@18
|
53 *
|
Chris@18
|
54 * @return mixed
|
Chris@18
|
55 * Response data that should be serialized.
|
Chris@18
|
56 */
|
Chris@18
|
57 public function getResponseData() {
|
Chris@18
|
58 return $this->responseData;
|
Chris@18
|
59 }
|
Chris@18
|
60
|
Chris@18
|
61 }
|