Completed
Push — master ( 90d90f...48c4f2 )
by Neomerx
04:40
created

ValidatorWrapper::setWrapperCaptures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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 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 ValidatorWrapper implements JsonApiValidatorInterface
28
{
29
    /**
30
     * @var JsonApiValidatorInterface
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 JsonApiValidatorInterface $validator
56
     * @param int                       $httpErrorCode
57
     */
58
    public function __construct(
59
        JsonApiValidatorInterface $validator,
60
        int $httpErrorCode = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY
61
    ) {
62
        $this->validator     = $validator;
63
        $this->httpErrorCode = $httpErrorCode;
64
65
        $this->initWrapper();
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function assert($jsonData): JsonApiValidatorInterface
72
    {
73
        if ($this->validate($jsonData) === false) {
74
            throw new JsonApiException($this->getJsonApiErrors(), $this->getHttpErrorCode());
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getJsonApiCaptures(): array
84
    {
85
        $captures = $this->getWrapperCaptures();
86
87
        return $this->isOverrideCapturesNotReplace() === true ?
88
            array_merge($this->getWrappedValidator()->getJsonApiCaptures(), $captures) : $captures;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function getJsonApiErrors(): array
95
    {
96
        return array_merge($this->getWrappedValidator()->getJsonApiErrors(), $this->getWrapperErrors());
97
    }
98
99
    /**
100
     * @return self
101
     */
102
    protected function initWrapper(): self
103
    {
104
        $this->wrapperCaptures    = [];
105
        $this->wrapperErrors      = [];
106
        $this->overrideNotReplace = true;
107
108
        return $this;
109
    }
110
111
112
    /**
113
     * @param array $wrapperCaptures
114
     *
115
     * @return self
116
     */
117
    protected function setWrapperCaptures(array $wrapperCaptures): self
118
    {
119
        $this->wrapperCaptures    = $wrapperCaptures;
120
        $this->overrideNotReplace = true;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param array $wrapperCaptures
127
     *
128
     * @return self
129
     */
130
    protected function setCaptureReplacements(array $wrapperCaptures): self
131
    {
132
        $this->wrapperCaptures    = $wrapperCaptures;
133
        $this->overrideNotReplace = false;
134
135
        return $this;
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    protected function getWrapperCaptures(): array
142
    {
143
        assert($this->wrapperCaptures !== null, 'Haven\'t you forgotten to call init function?');
144
145
        return $this->wrapperCaptures;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    protected function getWrapperErrors(): array
152
    {
153
        assert($this->wrapperCaptures !== null, 'Haven\'t you forgotten to call init function?');
154
155
        return $this->wrapperErrors;
156
    }
157
158
    /**
159
     * @param ErrorInterface[] $wrapperErrors
160
     *
161
     * @return self
162
     */
163
    protected function setWrapperErrors(array $wrapperErrors): self
164
    {
165
        if (empty($wrapperErrors) === false) {
166
            assert(call_user_func(function () use ($wrapperErrors) : bool {
167
                $allAreErrors = true;
168
169
                foreach ($wrapperErrors as $error) {
170
                    $allAreErrors = $allAreErrors === true && $error instanceof ErrorInterface;
171
                }
172
173
                return $allAreErrors;
174
            }), 'All errors should implement ErrorInterface.');
175
176
            // if we set errors then captures should not be shown
177
            $this->setCaptureReplacements([]);
178
        }
179
180
        $this->wrapperErrors = $wrapperErrors;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return JsonApiValidatorInterface
187
     */
188
    protected function getWrappedValidator(): JsonApiValidatorInterface
189
    {
190
        return $this->validator;
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    private function getHttpErrorCode(): int
197
    {
198
        return $this->httpErrorCode;
199
    }
200
201
    /**
202
     * @return bool
203
     */
204
    private function isOverrideCapturesNotReplace(): bool
205
    {
206
        return $this->overrideNotReplace;
207
    }
208
}
209