ResourceResponse.php 1.6 KB

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