|
1
|
|
|
<?php namespace Limoncello\Flute\Validation\JsonApi; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Flute\Contracts\Validation\JsonApiDataValidatingParserInterface; |
|
20
|
|
|
use Limoncello\Flute\Http\JsonApiResponse; |
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Document\ErrorInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Limoncello\Flute |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class DataParserWrapper implements JsonApiDataValidatingParserInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var JsonApiDataValidatingParserInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $validator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $httpErrorCode; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $wrapperCaptures; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $wrapperErrors; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
private $overrideNotReplace; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param JsonApiDataValidatingParserInterface $validator |
|
56
|
|
|
* @param int $httpErrorCode |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function __construct( |
|
59
|
|
|
JsonApiDataValidatingParserInterface $validator, |
|
60
|
|
|
int $httpErrorCode = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
|
61
|
|
|
) { |
|
62
|
2 |
|
$this->validator = $validator; |
|
63
|
2 |
|
$this->httpErrorCode = $httpErrorCode; |
|
64
|
|
|
|
|
65
|
2 |
|
$this->initWrapper(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function assert($jsonData): JsonApiDataValidatingParserInterface |
|
72
|
|
|
{ |
|
73
|
2 |
|
if ($this->parse($jsonData) === false) { |
|
74
|
1 |
|
throw new JsonApiException($this->getJsonApiErrors(), $this->getHttpErrorCode()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function getJsonApiCaptures(): array |
|
84
|
|
|
{ |
|
85
|
2 |
|
$captures = $this->getWrapperCaptures(); |
|
86
|
|
|
|
|
87
|
2 |
|
return $this->isOverrideCapturesNotReplace() === true ? |
|
88
|
2 |
|
array_merge($this->getWrappedParser()->getJsonApiCaptures(), $captures) : $captures; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function getJsonApiErrors(): array |
|
95
|
|
|
{ |
|
96
|
2 |
|
return array_merge($this->getWrappedParser()->getJsonApiErrors(), $this->getWrapperErrors()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
2 |
|
protected function initWrapper(): self |
|
103
|
|
|
{ |
|
104
|
2 |
|
$this->wrapperCaptures = []; |
|
105
|
2 |
|
$this->wrapperErrors = []; |
|
106
|
2 |
|
$this->overrideNotReplace = true; |
|
107
|
|
|
|
|
108
|
2 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $wrapperCaptures |
|
114
|
|
|
* |
|
115
|
|
|
* @return self |
|
116
|
|
|
*/ |
|
117
|
1 |
|
protected function setWrapperCaptures(array $wrapperCaptures): self |
|
118
|
|
|
{ |
|
119
|
1 |
|
$this->wrapperCaptures = $wrapperCaptures; |
|
120
|
1 |
|
$this->overrideNotReplace = true; |
|
121
|
|
|
|
|
122
|
1 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param array $wrapperCaptures |
|
127
|
|
|
* |
|
128
|
|
|
* @return self |
|
129
|
|
|
*/ |
|
130
|
1 |
|
protected function setCaptureReplacements(array $wrapperCaptures): self |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->wrapperCaptures = $wrapperCaptures; |
|
133
|
1 |
|
$this->overrideNotReplace = false; |
|
134
|
|
|
|
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
2 |
|
protected function getWrapperCaptures(): array |
|
142
|
|
|
{ |
|
143
|
2 |
|
assert($this->wrapperCaptures !== null, 'Haven\'t you forgotten to call init function?'); |
|
144
|
|
|
|
|
145
|
2 |
|
return $this->wrapperCaptures; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
2 |
|
protected function getWrapperErrors(): array |
|
152
|
|
|
{ |
|
153
|
2 |
|
assert($this->wrapperCaptures !== null, 'Haven\'t you forgotten to call init function?'); |
|
154
|
|
|
|
|
155
|
2 |
|
return $this->wrapperErrors; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param ErrorInterface[] $wrapperErrors |
|
160
|
|
|
* |
|
161
|
|
|
* @return self |
|
162
|
|
|
*/ |
|
163
|
1 |
|
protected function setWrapperErrors(array $wrapperErrors): self |
|
164
|
|
|
{ |
|
165
|
1 |
|
if (empty($wrapperErrors) === false) { |
|
166
|
1 |
|
assert(call_user_func(function () use ($wrapperErrors) : bool { |
|
167
|
1 |
|
$allAreErrors = true; |
|
168
|
|
|
|
|
169
|
1 |
|
foreach ($wrapperErrors as $error) { |
|
170
|
1 |
|
$allAreErrors = $allAreErrors === true && $error instanceof ErrorInterface; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
return $allAreErrors; |
|
174
|
1 |
|
}), 'All errors should implement ErrorInterface.'); |
|
175
|
|
|
|
|
176
|
|
|
// if we set errors then captures should not be shown |
|
177
|
1 |
|
$this->setCaptureReplacements([]); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
$this->wrapperErrors = $wrapperErrors; |
|
181
|
|
|
|
|
182
|
1 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return JsonApiDataValidatingParserInterface |
|
187
|
|
|
*/ |
|
188
|
2 |
|
protected function getWrappedParser(): JsonApiDataValidatingParserInterface |
|
189
|
|
|
{ |
|
190
|
2 |
|
return $this->validator; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return int |
|
195
|
|
|
*/ |
|
196
|
1 |
|
private function getHttpErrorCode(): int |
|
197
|
|
|
{ |
|
198
|
1 |
|
return $this->httpErrorCode; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
2 |
|
private function isOverrideCapturesNotReplace(): bool |
|
205
|
|
|
{ |
|
206
|
2 |
|
return $this->overrideNotReplace; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|