Completed
Push — master ( 87a075...2a291a )
by Albert
37:13 queued 33:30
created

Validator::throwValidationException()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
nc 2
cc 3
eloc 5
nop 2
crap 3.072
1
<?php
2
3
namespace Proengsoft\JsValidation\Remote;
4
5
use Illuminate\Http\JsonResponse;
6
use Proengsoft\JsValidation\Support\RuleListTrait;
7
use Illuminate\Http\Exception\HttpResponseException;
8
use Illuminate\Validation\Validator as BaseValidator;
9
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
10
11
/**
12
 * Class RemoteValidator.
13
 */
14
class Validator
15
{
16
    use AccessProtectedTrait, RuleListTrait;
17
18
    /**
19
     * Validator extension name.
20
     */
21
    const EXTENSION_NAME = 'jsvalidation';
22
23
    /**
24
     * @var \Illuminate\Validation\Validator
25
     */
26
    protected $validator;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $validateAll = false;
32
33
    /**
34
     * RemoteValidator constructor.
35
     *
36
     * @param \Illuminate\Validation\Validator $validator
37
     */
38 5
    public function __construct(BaseValidator $validator)
39
    {
40 5
        $this->validator = $validator;
41 5
    }
42
43
    /**
44
     * Force validate all rules.
45
     *
46
     * @param $validateAll
47
     */
48 2
    public function setValidateAll($validateAll)
49
    {
50 2
        $this->validateAll = $validateAll;
51 2
    }
52
53
    /**
54
     * Validate request.
55
     *
56
     * @param $attribute
57
     * @param $value
58
     * @param $parameters
59
     */
60 5
    public function validate($attribute, $value, $parameters)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62 5
        $validationData = $this->parseJsRemoteRequest($value);
63
64 5
        $validationResult = $this->validateJsRemoteRequest($validationData);
65 5
        $this->throwValidationException($validationResult, $this->validator);
66
67
    }
68
69
    /**
70
     * Throw the failed validation exception.
71
     *
72
     * @param  mixed $result
73
     * @param  \Illuminate\Validation\Validator  $validator
74
     * @return void
75
     *
76
     * @throws \Illuminate\Validation\ValidationException|\Illuminate\Http\Exception\HttpResponseException
77
     */
78 5
    protected function throwValidationException($result, $validator) {
79 5
        $response =  new JsonResponse($result, 200);
80
81 5
        if ($result!==true && class_exists('\Illuminate\Validation\ValidationException')) {
82
            throw new \Illuminate\Validation\ValidationException($validator, $response);
83
        }
84 5
        throw new HttpResponseException($response);
85
    }
86
87
    /**
88
     *  Parse Validation input request data.
89
     *
90
     * @param $data
91
     * @return array
92
     */
93 5
    protected function parseJsRemoteRequest($data)
94
    {
95 5
        parse_str($data, $attrParts);
96 5
        $attrParts = is_null($attrParts) ? [] : $attrParts;
97 5
        $newAttr = array_keys(array_dot($attrParts));
98
99 5
        return array_pop($newAttr);
100
    }
101
102
    /**
103
     * Validate remote Javascript Validations.
104
     *
105
     * @param $attribute
106
     * @return array|bool
107
     */
108 5
    protected function validateJsRemoteRequest($attribute)
109
    {
110 5
        $validator = $this->validator;
111 5
        $validator = $this->setRemoteValidation($attribute, $validator);
112
113 5
        if ($validator->passes()) {
114 3
            return true;
115
        }
116
117 2
        return $validator->messages()->get($attribute);
118
    }
119
120
    /**
121
     * Sets data for validate remote rules.
122
     *
123
     * @param $attribute
124
     *
125
     * @return \Illuminate\Validation\Validator
126
     */
127 5
    protected function setRemoteValidation($attribute, BaseValidator $validator)
128
    {
129 5
        $rules = $validator->getRules();
130 5
        $rules = isset($rules[$attribute]) ? $rules[$attribute] : [];
131 5
        if (in_array('no_js_validation', $rules)) {
132 1
            $validator->setRules([$attribute => []]);
133
134 1
            return $validator;
135
        }
136
137 4
        $rules = $this->purgeNonRemoteRules($rules, $validator);
138 4
        $validator->setRules([$attribute => $rules]);
139
140 4
        return $validator;
141
    }
142
143
    /**
144
     * Remove rules that should not be validated remotely.
145
     *
146
     * @param $rules
147
     * @param $validator
148
     * @return mixed
149
     */
150 4
    protected function purgeNonRemoteRules($rules, $validator)
151
    {
152 4
        if ($this->validateAll) {
153 1
            return $rules;
154
        }
155
156 3
        $protectedValidator = $this->createProtectedCaller($validator);
157
158 3
        foreach ($rules as $i => $rule) {
159 3
            $parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]);
160 3
            if (! $this->isRemoteRule($parsedRule[0])) {
161 3
                unset($rules[$i]);
162 3
            }
163 3
        }
164
165 3
        return $rules;
166
    }
167
}
168