Completed
Branch delegated-factory (5dc99b)
by Albert
03:28
created

RemoteValidator::parseJsRemoteRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace Proengsoft\JsValidation;
4
5
use Illuminate\Http\Exception\HttpResponseException;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Support\Arr;
8
use Illuminate\Validation\Validator;
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 RemoteValidator
17
{
18
    use AccessProtectedTrait, RuleListTrait;
19
20
    /**
21
     * Validator extension name.
22
     */
23
    const EXTENSION_NAME = 'jsvalidation';
24
25
    /**
26
     * @var Validator
27
     */
28
    protected $validator;
29
30
    /**
31
     * RemoteValidator constructor.
32
     *
33
     * @param Validator $validator
34
     */
35
    public function __construct(Validator $validator)
36
    {
37
        $this->validator = $validator;
38
    }
39
40
    public function validate($attribute, $value, $parameters)
41
    {
42
        $validationData = $this->parseJsRemoteRequest($attribute, $value, $parameters);
43
        $this->validateJsRemoteRequest($validationData[1]);
44
    }
45
46
    /**
47
     *  Parse Validation input request data.
48
     *
49
     * @param $attribute
50
     * @param $value
51
     * @param $parameters
52
     *
53
     * @return array
54
     */
55
    protected function parseJsRemoteRequest($attribute, $value, $parameters)
56
    {
57
        $attr_parts = array();
58
        parse_str("$value=", $attr_parts);
59
        $newAttr = array_keys(Arr::dot($attr_parts));
0 ignored issues
show
Bug introduced by
It seems like $attr_parts can also be of type null; however, Illuminate\Support\Arr::dot() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
61
        return [$attribute, array_pop($newAttr), $parameters];
62
    }
63
64
    /**
65
     * Validate remote Javascript Validations.
66
     *
67
     * @param $attribute
68
     */
69
    protected function validateJsRemoteRequest($attribute)
70
    {
71
        $validator = $this->validator;
72
        $validator = $this->setRemoteValidation($attribute, $validator);
73
74
        if ($validator->passes()) {
75
            $message = true;
76
        } else {
77
            $message = $validator->messages()->get($attribute);
78
        }
79
80
        throw new HttpResponseException(
81
            new JsonResponse($message, 200));
82
    }
83
84
    /**
85
     * Sets data for validate remote rules.
86
     *
87
     * @param $attribute
88
     *
89
     * @return Validator
90
     */
91
    protected function setRemoteValidation($attribute, Validator $validator)
92
    {
93
        if (!array_key_exists($attribute, $validator->getRules())) {
94
            throw new BadRequestHttpException("Undefined '$attribute' attribute");
95
        }
96
97
        $rules = $validator->getRules()[$attribute];
98
        $disabled = $this->validationDisabled($rules);
99
        $protectedValidator = $this->createProtectedCaller($validator);
100
101
        foreach ($rules as $i => $rule) {
102
            $parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]);
103
            if ($disabled || !$this->isRemoteRule($parsedRule[0])) {
104
                unset($rules[$i]);
105
            }
106
        }
107
        $validator->setRules([$attribute => $rules]);
108
109
        if (empty($validator->getRules())) {
110
            throw new BadRequestHttpException("No validations available for '$attribute''");
111
        }
112
113
        return $validator;
114
    }
115
}
116