comparison core/modules/jsonapi/src/JsonApiResource/JsonApiDocumentTopLevel.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\jsonapi\JsonApiResource;
4
5 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
6
7 /**
8 * Represents a JSON:API document's "top level".
9 *
10 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
11 * may change at any time and could break any dependencies on it.
12 *
13 * @see https://www.drupal.org/project/jsonapi/issues/3032787
14 * @see jsonapi.api.php
15 *
16 * @see http://jsonapi.org/format/#document-top-level
17 *
18 * @todo Add support for the missing optional 'jsonapi' member or document why not.
19 */
20 class JsonApiDocumentTopLevel {
21
22 /**
23 * The data to normalize.
24 *
25 * @var \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface|\Drupal\jsonapi\JsonApiResource\Data|\Drupal\jsonapi\JsonApiResource\ErrorCollection|\Drupal\Core\Field\EntityReferenceFieldItemListInterface
26 */
27 protected $data;
28
29 /**
30 * The metadata to normalize.
31 *
32 * @var array
33 */
34 protected $meta;
35
36 /**
37 * The links.
38 *
39 * @var \Drupal\jsonapi\JsonApiResource\LinkCollection
40 */
41 protected $links;
42
43 /**
44 * The includes to normalize.
45 *
46 * @var \Drupal\jsonapi\JsonApiResource\IncludedData
47 */
48 protected $includes;
49
50 /**
51 * Resource objects that will be omitted from the response for access reasons.
52 *
53 * @var \Drupal\jsonapi\JsonApiResource\OmittedData
54 */
55 protected $omissions;
56
57 /**
58 * Instantiates a JsonApiDocumentTopLevel object.
59 *
60 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface|\Drupal\jsonapi\JsonApiResource\Data|\Drupal\jsonapi\JsonApiResource\ErrorCollection|\Drupal\Core\Field\EntityReferenceFieldItemListInterface $data
61 * The data to normalize. It can be either a ResourceObject, or a stand-in
62 * for one, or a collection of the same.
63 * @param \Drupal\jsonapi\JsonApiResource\IncludedData $includes
64 * A JSON:API Data object containing resources to be included in the
65 * response document or NULL if there should not be includes.
66 * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links
67 * A collection of links to resources related to the top-level document.
68 * @param array $meta
69 * (optional) The metadata to normalize.
70 */
71 public function __construct($data, IncludedData $includes, LinkCollection $links, array $meta = []) {
72 assert($data instanceof ResourceIdentifierInterface || $data instanceof Data || $data instanceof ErrorCollection || $data instanceof EntityReferenceFieldItemListInterface);
73 assert(!$data instanceof ErrorCollection || $includes instanceof NullIncludedData);
74 $this->data = $data instanceof ResourceObjectData ? $data->getAccessible() : $data;
75 $this->includes = $includes->getAccessible();
76 $this->links = $links->withContext($this);
77 $this->meta = $meta;
78 $this->omissions = $data instanceof ResourceObjectData
79 ? OmittedData::merge($data->getOmissions(), $includes->getOmissions())
80 : $includes->getOmissions();
81 }
82
83 /**
84 * Gets the data.
85 *
86 * @return \Drupal\jsonapi\JsonApiResource\ResourceObject|\Drupal\jsonapi\JsonApiResource\Data|\Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject|\Drupal\jsonapi\JsonApiResource\ErrorCollection
87 * The data.
88 */
89 public function getData() {
90 return $this->data;
91 }
92
93 /**
94 * Gets the links.
95 *
96 * @return \Drupal\jsonapi\JsonApiResource\LinkCollection
97 * The top-level links.
98 */
99 public function getLinks() {
100 return $this->links;
101 }
102
103 /**
104 * Gets the metadata.
105 *
106 * @return array
107 * The metadata.
108 */
109 public function getMeta() {
110 return $this->meta;
111 }
112
113 /**
114 * Gets a JSON:API Data object of resources to be included in the response.
115 *
116 * @return \Drupal\jsonapi\JsonApiResource\IncludedData
117 * The includes.
118 */
119 public function getIncludes() {
120 return $this->includes;
121 }
122
123 /**
124 * Gets an OmittedData instance containing resources to be omitted.
125 *
126 * @return \Drupal\jsonapi\JsonApiResource\OmittedData
127 * The omissions.
128 */
129 public function getOmissions() {
130 return $this->omissions;
131 }
132
133 }