Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

Validator::__construct()   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\Support\Arr;
8
use Illuminate\Validation\Validator as BaseValidator;
9
use Proengsoft\JsValidation\Exceptions\BadRequestHttpException;
10
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
11
use Proengsoft\JsValidation\Support\RuleListTrait;
12
13
/**
14
 * Class RemoteValidator.
15
 */
16
class Validator
17
{
18
    use AccessProtectedTrait, RuleListTrait;
19
20
    /**
21
     * Validator extension name.
22
     */
23
    const EXTENSION_NAME = 'jsvalidation';
24
25
    /**
26
     * @var \Illuminate\Validation\Validator
27
     */
28
    protected $validator;
29
30
    /**
31
     * RemoteValidator constructor.
32
     *
33
     * @param \Illuminate\Validation\Validator $validator
34
     */
35 5
    public function __construct(BaseValidator $validator)
36
    {
37 5
        $this->validator = $validator;
38 5
    }
39
40 5
    public function validate($attribute, $value, $parameters)
41
    {
42 5
        $validationData = $this->parseJsRemoteRequest($attribute, $value, $parameters);
43 5
        $validationResult = $this->validateJsRemoteRequest($validationData[1]);
44 3
        throw new HttpResponseException(
45 3
            new JsonResponse($validationResult, 200));
46
    }
47
48
    /**
49
     *  Parse Validation input request data.
50
     *
51
     * @param $attribute
52
     * @param $value
53
     * @param $parameters
54
     * @return array
55
     */
56 5
    protected function parseJsRemoteRequest($attribute, $value, $parameters)
57
    {
58 5
        parse_str("$value=", $attr_parts);
59 5
        $attr_parts = is_null($attr_parts) ? [] : $attr_parts;
60 5
        $newAttr = array_keys(Arr::dot($attr_parts));
61
62 5
        return [$attribute, array_pop($newAttr), $parameters];
63
    }
64
65
    /**
66
     * Validate remote Javascript Validations.
67
     *
68
     * @param $attribute
69
     * @return array|bool
70
     */
71 5
    protected function validateJsRemoteRequest($attribute)
72
    {
73 5
        $validator = $this->validator;
74 5
        $validator = $this->setRemoteValidation($attribute, $validator);
75
76 3
        if ($validator->passes()) {
77 2
            return true;
78
        }
79
80 1
        return $validator->messages()->get($attribute);
81
    }
82
83
    /**
84
     * Sets data for validate remote rules.
85
     *
86
     * @param $attribute
87
     *
88
     * @return \Illuminate\Validation\Validator
89
     */
90 5
    protected function setRemoteValidation($attribute, BaseValidator $validator)
91
    {
92 5
        if (! array_key_exists($attribute, $validator->getRules())) {
93 1
            throw new BadRequestHttpException("Undefined '$attribute' attribute");
94
        }
95
96 4
        $rules = $validator->getRules()[$attribute];
97 4
        $rules = $this->purgeNonRemoteRules($rules, $validator);
98 4
        $validator->setRules([$attribute => $rules]);
99
100 4
        if (empty($validator->getRules()[$attribute])) {
101 1
            throw new BadRequestHttpException("No validations available for '$attribute'");
102
        }
103
104 3
        return $validator;
105
    }
106
107 4
    protected function purgeNonRemoteRules($rules, $validator)
108
    {
109 4
        $disabled = $this->validationDisabled($rules);
110 4
        $protectedValidator = $this->createProtectedCaller($validator);
111
112 4
        foreach ($rules as $i => $rule) {
113 4
            $parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]);
114 4
            if ($disabled || ! $this->isRemoteRule($parsedRule[0])) {
115 4
                unset($rules[$i]);
116 4
            }
117 4
        }
118
119 4
        return $rules;
120
    }
121
}
122