Completed
Push — develop ( c694a4...b6f561 )
by Neomerx
04:02 queued 02:14
created

ValidatorWrapper::initWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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