1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Representation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Representation\BaseWriterInterface; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\BaseLinkInterface; |
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface; |
24
|
|
|
use Neomerx\JsonApi\Contracts\Schema\LinkInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Neomerx\JsonApi |
28
|
|
|
*/ |
29
|
|
|
abstract class BaseWriter implements BaseWriterInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $data; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $urlPrefix; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $isDataAnArray; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
*/ |
49
|
79 |
|
public function __construct() |
50
|
|
|
{ |
51
|
79 |
|
$this->reset(); |
52
|
79 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
23 |
|
public function setDataAsArray(): BaseWriterInterface |
58
|
|
|
{ |
59
|
23 |
|
assert($this->isDataAnArray() === false); |
60
|
23 |
|
assert(array_key_exists(DocumentInterface::KEYWORD_DATA, $this->data) === false); |
61
|
|
|
|
62
|
23 |
|
$this->data[DocumentInterface::KEYWORD_DATA] = []; |
63
|
23 |
|
$this->isDataAnArray = true; |
64
|
|
|
|
65
|
23 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
77 |
|
public function getDocument(): array |
72
|
|
|
{ |
73
|
77 |
|
return $this->data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
6 |
|
public function setMeta($meta): BaseWriterInterface |
80
|
|
|
{ |
81
|
6 |
|
assert(is_resource($meta) === false); |
82
|
|
|
|
83
|
6 |
|
$this->data[DocumentInterface::KEYWORD_META] = $meta; |
84
|
|
|
|
85
|
6 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
2 |
|
public function setJsonApiVersion(string $version): BaseWriterInterface |
92
|
|
|
{ |
93
|
2 |
|
$this->data[DocumentInterface::KEYWORD_JSON_API][DocumentInterface::KEYWORD_VERSION] = $version; |
94
|
|
|
|
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
2 |
|
public function setJsonApiMeta($meta): BaseWriterInterface |
102
|
|
|
{ |
103
|
2 |
|
assert(is_resource($meta) === false); |
104
|
|
|
|
105
|
2 |
|
$this->data[DocumentInterface::KEYWORD_JSON_API][DocumentInterface::KEYWORD_META] = $meta; |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
79 |
|
public function setUrlPrefix(string $prefix): BaseWriterInterface |
114
|
|
|
{ |
115
|
79 |
|
$this->urlPrefix = $prefix; |
116
|
|
|
|
117
|
79 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
5 |
|
public function setLinks(iterable $links): BaseWriterInterface |
124
|
|
|
{ |
125
|
5 |
|
$representation = $this->getLinksRepresentation( |
126
|
5 |
|
$this->getUrlPrefix(), |
127
|
5 |
|
$links |
128
|
|
|
); |
129
|
|
|
|
130
|
5 |
|
if (empty($representation) === false) { |
131
|
5 |
|
$this->data[DocumentInterface::KEYWORD_LINKS] = $representation; |
132
|
|
|
} |
133
|
|
|
|
134
|
5 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
1 |
|
public function setProfile(iterable $links): BaseWriterInterface |
141
|
|
|
{ |
142
|
1 |
|
$representation = $this->getLinksListRepresentation( |
143
|
1 |
|
$this->getUrlPrefix(), |
144
|
1 |
|
$links |
145
|
|
|
); |
146
|
|
|
|
147
|
1 |
|
if (empty($representation) === false) { |
148
|
1 |
|
$this->data[DocumentInterface::KEYWORD_LINKS][DocumentInterface::KEYWORD_PROFILE] = $representation; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
79 |
|
protected function reset(): void |
158
|
|
|
{ |
159
|
79 |
|
$this->data = []; |
160
|
79 |
|
$this->urlPrefix = ''; |
161
|
79 |
|
$this->isDataAnArray = false; |
162
|
79 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
61 |
|
protected function getUrlPrefix(): string |
168
|
|
|
{ |
169
|
61 |
|
return $this->urlPrefix; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param null|string $prefix |
174
|
|
|
* @param iterable $links |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
61 |
|
protected function getLinksRepresentation(?string $prefix, iterable $links): array |
179
|
|
|
{ |
180
|
61 |
|
$result = []; |
181
|
|
|
|
182
|
61 |
|
foreach ($links as $name => $link) { |
183
|
61 |
|
assert($link instanceof LinkInterface); |
184
|
61 |
|
$result[$name] = $link->canBeShownAsString() === true ? |
185
|
61 |
|
$link->getStringRepresentation($prefix) : $link->getArrayRepresentation($prefix); |
186
|
|
|
} |
187
|
|
|
|
188
|
61 |
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param null|string $prefix |
193
|
|
|
* @param iterable $links |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
5 |
|
protected function getLinksListRepresentation(?string $prefix, iterable $links): array |
198
|
|
|
{ |
199
|
5 |
|
$result = []; |
200
|
|
|
|
201
|
5 |
|
foreach ($links as $link) { |
202
|
5 |
|
assert($link instanceof BaseLinkInterface); |
203
|
5 |
|
$result[] = $link->canBeShownAsString() === true ? |
204
|
5 |
|
$link->getStringRepresentation($prefix) : $link->getArrayRepresentation($prefix); |
205
|
|
|
} |
206
|
|
|
|
207
|
5 |
|
return $result; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
66 |
|
protected function isDataAnArray(): bool |
214
|
|
|
{ |
215
|
66 |
|
return $this->isDataAnArray; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|