-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDocument.php
229 lines (192 loc) · 4.77 KB
/
Document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
* Copyright 2021 Cloud Creativity Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);
namespace LaravelJsonApi\Encoder\Neomerx;
use LaravelJsonApi\Contracts\Encoder\JsonApiDocument;
use LaravelJsonApi\Core\Document\JsonApi;
use LaravelJsonApi\Core\Document\Links;
use LaravelJsonApi\Core\Json\Hash;
use LaravelJsonApi\Encoder\Neomerx\Encoder\Encoder as ExtendedEncoder;
abstract class Document implements JsonApiDocument
{
/**
* @var ExtendedEncoder
*/
private ExtendedEncoder $encoder;
/**
* @var Mapper
*/
private Mapper $mapper;
/**
* @var JsonApi|null
*/
private ?JsonApi $jsonApi;
/**
* @var Links|null
*/
private ?Links $links = null;
/**
* @var Hash|null
*/
private ?Hash $meta = null;
/**
* JsonApiDocument constructor.
*
* @param ExtendedEncoder $encoder
* @param Mapper $mapper
*/
public function __construct(ExtendedEncoder $encoder, Mapper $mapper)
{
$this->encoder = $encoder;
$this->mapper = $mapper;
}
/**
* @return array
*/
abstract protected function serialize(): array;
/**
* @return string
*/
abstract protected function encode(): string;
/**
* @inheritDoc
*/
public function withJsonApi($jsonApi): self
{
if ($value = JsonApi::nullable($jsonApi)) {
$this->jsonApi = $jsonApi;
}
return $this;
}
/**
* @inheritDoc
*/
public function withoutJsonApi(): JsonApiDocument
{
$this->jsonApi = null;
return $this;
}
/**
* @inheritDoc
*/
public function withLinks($links): self
{
$this->links = Links::cast($links);
return $this;
}
/**
* @inheritDoc
*/
public function withoutLinks(): JsonApiDocument
{
$this->links = null;
return $this;
}
/**
* @inheritDoc
*/
public function withMeta($meta): self
{
$this->meta = Hash::cast($meta);
return $this;
}
/**
* @inheritDoc
*/
public function withoutMeta(): JsonApiDocument
{
$this->meta = null;
return $this;
}
/**
* @return string
*/
public function __toString()
{
return $this->toString();
}
/**
* @inheritDoc
*/
public function toString(): string
{
return $this->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
/**
* @inheritDoc
*/
public function toArray()
{
try {
return json_decode($this->toJson(), true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable $ex) {
throw new \LogicException('Unable to convert document to an array.', 0, $ex);
}
}
/**
* @inheritDoc
*/
public function jsonSerialize()
{
try {
$this->prepareEncoder();
return $this->serialize();
} catch (\Throwable $ex) {
throw new \LogicException('Unable to serialize compound document.', 0, $ex);
}
}
/**
* @inheritDoc
*/
public function toJson($options = 0)
{
try {
$this->prepareEncoder();
$this->encoder->withEncodeOptions($options | JSON_THROW_ON_ERROR);
return $this->encode();
} catch (\Throwable $ex) {
throw new \LogicException('Unable to encode compound document.', 0, $ex);
}
}
/**
* Reset the encoder.
*
* @return void
*/
private function prepareEncoder(): void
{
if ($this->jsonApi && $version = $this->jsonApi->version()) {
$this->encoder->withJsonApiVersion($version);
}
if ($this->jsonApi && $this->jsonApi->hasMeta()) {
$this->encoder->withJsonApiMeta($this->jsonApi->meta());
}
if ($this->meta && $this->meta->isNotEmpty()) {
$this->encoder->withMeta($this->meta);
}
if ($this->links && $this->links->isNotEmpty()) {
$this->encoder->withLinks($this->mapper->allLinks($this->links));
}
}
/**
* @return ExtendedEncoder
*/
protected function encoder(): ExtendedEncoder
{
return $this->encoder;
}
}