Completed
Push — master ( c73a7c...2189c9 )
by Albert
02:08
created

Validator::setValidateAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Remote;
4
5
use Illuminate\Http\Exception\HttpResponseException;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Validation\Validator as BaseValidator;
8
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
9
use Proengsoft\JsValidation\Support\RuleListTrait;
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)
61
    {
62 5
        $validationData = $this->parseJsRemoteRequest($attribute, $value, $parameters);
63 5
        $validationResult = $this->validateJsRemoteRequest($validationData[1]);
64 5
        throw new HttpResponseException(
65 5
            new JsonResponse($validationResult, 200));
66
    }
67
68
    /**
69
     *  Parse Validation input request data.
70
     *
71
     * @param $attribute
72
     * @param $value
73
     * @param $parameters
74
     * @return array
75
     */
76 5
    protected function parseJsRemoteRequest($attribute, $value, $parameters)
77
    {
78 5
        parse_str($value, $attrParts);
79 5
        $attrParts = is_null($attrParts) ? [] : $attrParts;
80 5
        $newAttr = array_keys(array_dot($attrParts));
81
82 5
        return [$attribute, array_pop($newAttr), $parameters];
83
    }
84
85
    /**
86
     * Validate remote Javascript Validations.
87
     *
88
     * @param $attribute
89
     * @return array|bool
90
     */
91 5
    protected function validateJsRemoteRequest($attribute)
92
    {
93 5
        $validator = $this->validator;
94 5
        $validator = $this->setRemoteValidation($attribute, $validator);
95
96 5
        if ($validator->passes()) {
97 3
            return true;
98
        }
99
100 2
        return $validator->messages()->get($attribute);
101
    }
102
103
    /**
104
     * Sets data for validate remote rules.
105
     *
106
     * @param $attribute
107
     *
108
     * @return \Illuminate\Validation\Validator
109
     */
110 5
    protected function setRemoteValidation($attribute, BaseValidator $validator)
111
    {
112 5
        $rules = $validator->getRules();
113 5
        $rules = isset($rules[$attribute]) ? $rules[$attribute] : [];
114 5
        if (in_array('no_js_validation', $rules)) {
115 1
            $validator->setRules([$attribute => []]);
116
117 1
            return $validator;
118
        }
119
120 4
        $rules = $this->purgeNonRemoteRules($rules, $validator);
121 4
        $validator->setRules([$attribute => $rules]);
122
123 4
        return $validator;
124
    }
125
126
    /**
127
     * Remove rules that should not be validated remotely.
128
     *
129
     * @param $rules
130
     * @param $validator
131
     * @return mixed
132
     */
133 4
    protected function purgeNonRemoteRules($rules, $validator)
134
    {
135 4
        if ($this->validateAll) {
136 1
            return $rules;
137
        }
138
139 3
        $protectedValidator = $this->createProtectedCaller($validator);
140
141 3
        foreach ($rules as $i => $rule) {
142 3
            $parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]);
143 3
            if (! $this->isRemoteRule($parsedRule[0])) {
144 3
                unset($rules[$i]);
145 3
            }
146 3
        }
147
148 3
        return $rules;
149
    }
150
}
151