|
1
|
|
|
<?php namespace Limoncello\Flute\Validation; |
|
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\JsonApiValidatorInterface; |
|
20
|
|
|
use Neomerx\JsonApi\Contracts\Document\ErrorInterface; |
|
21
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\Flute |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class ValidatorWrapper implements JsonApiValidatorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var JsonApiValidatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $validator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $httpErrorCode; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $wrapperCaptures = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $wrapperErrors = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $overrideNotReplace = true; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param JsonApiValidatorInterface $validator |
|
55
|
|
|
* @param int $httpErrorCode |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
JsonApiValidatorInterface $validator, |
|
59
|
|
|
int $httpErrorCode = JsonApiException::DEFAULT_HTTP_CODE |
|
60
|
|
|
) { |
|
61
|
|
|
$this->validator = $validator; |
|
62
|
|
|
$this->httpErrorCode = $httpErrorCode; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function assert($jsonData): JsonApiValidatorInterface |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->validate($jsonData) === false) { |
|
71
|
|
|
throw new JsonApiException($this->getJsonApiErrors(), $this->getHttpErrorCode()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getJsonApiCaptures(): array |
|
81
|
|
|
{ |
|
82
|
|
|
$captures = $this->getWrapperCaptures(); |
|
83
|
|
|
|
|
84
|
|
|
return $this->isOverrideCapturesNotReplace() === true ? |
|
85
|
|
|
array_merge($this->getWrappedValidator()->getJsonApiCaptures(), $captures) : $captures; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getJsonApiErrors(): array |
|
92
|
|
|
{ |
|
93
|
|
|
return array_merge($this->getWrappedValidator()->getJsonApiErrors(), $this->getWrapperErrors()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $wrapperCaptures |
|
98
|
|
|
* |
|
99
|
|
|
* @return self |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function setWrapperCaptures(array $wrapperCaptures): self |
|
102
|
|
|
{ |
|
103
|
|
|
$this->wrapperCaptures = $wrapperCaptures; |
|
104
|
|
|
$this->overrideNotReplace = true; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $wrapperCaptures |
|
111
|
|
|
* |
|
112
|
|
|
* @return self |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function setCaptureReplacements(array $wrapperCaptures): self |
|
115
|
|
|
{ |
|
116
|
|
|
$this->wrapperCaptures = $wrapperCaptures; |
|
117
|
|
|
$this->overrideNotReplace = false; |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getWrapperCaptures(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->wrapperCaptures; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getWrapperErrors(): array |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->wrapperErrors; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param ErrorInterface[] $wrapperErrors |
|
140
|
|
|
* |
|
141
|
|
|
* @return self |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function setWrapperErrors(array $wrapperErrors): self |
|
144
|
|
|
{ |
|
145
|
|
|
assert(call_user_func(function () use ($wrapperErrors) : bool { |
|
146
|
|
|
$allAreErrors = true; |
|
147
|
|
|
|
|
148
|
|
|
foreach ($wrapperErrors as $error) { |
|
149
|
|
|
$allAreErrors = $allAreErrors === true && $error instanceof ErrorInterface; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $allAreErrors; |
|
153
|
|
|
}), 'All errors should implement ErrorInterface.'); |
|
154
|
|
|
|
|
155
|
|
|
$this->wrapperErrors = $wrapperErrors; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return JsonApiValidatorInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getWrappedValidator(): JsonApiValidatorInterface |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->validator; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return int |
|
170
|
|
|
*/ |
|
171
|
|
|
private function getHttpErrorCode(): int |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->httpErrorCode; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
private function isOverrideCapturesNotReplace(): bool |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->overrideNotReplace; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|