Completed
Push — master ( 328c23...368b19 )
by
unknown
07:01 queued 12s
created

Resolver::validatorClosure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0046
1
<?php
2
3
namespace Proengsoft\JsValidation\Remote;
4
5
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
6
use Illuminate\Support\Arr;
7
use Illuminate\Validation\Validator as BaseValidator;
8
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
9
10
class Resolver
11
{
12
    use AccessProtectedTrait;
13
14
    /**
15
     * @var \Closure
16
     */
17
    protected $resolver;
18
19
    /**
20
     * @var \Illuminate\Contracts\Validation\Factory
21
     */
22
    protected $factory;
23
24
    /**
25
     * RemoteValidator constructor.
26
     *
27
     * @param \Illuminate\Contracts\Validation\Factory $factory
28
     */
29 72
    public function __construct(ValidationFactory $factory)
30
    {
31 72
        $this->factory = $factory;
32 72
        $this->resolver = $this->getProtected($factory, 'resolver');
33 72
    }
34
35
    /**
36
     * Closure used to resolve Validator instance.
37
     *
38
     * @param $field
39
     * @return \Closure
40
     */
41 30
    public function resolver($field)
42
    {
43 30
        return function ($translator, $data, $rules, $messages, $customAttributes) use ($field) {
44 36
            return $this->resolve($translator, $data, $rules, $messages, $customAttributes, $field);
45 60
        };
46
    }
47
48
    /**
49
     * Resolves Validator instance.
50
     *
51
     * @param $translator
52
     * @param $data
53
     * @param $rules
54
     * @param $messages
55
     * @param $customAttributes
56
     * @param $field
57
     * @return \Illuminate\Validation\Validator
58
     */
59 36
    protected function resolve($translator, $data, $rules, $messages, $customAttributes, $field)
60
    {
61 36
        $validateAll = Arr::get($data, $field.'_validate_all', false);
62 36
        $validationRule = 'bail|'.Validator::EXTENSION_NAME.':'.$validateAll;
63 36
        $rules = [$field => $validationRule] + $rules;
64 36
        $validator = $this->createValidator($translator, $data, $rules, $messages, $customAttributes);
65
66 36
        return $validator;
67
    }
68
69
    /**
70
     * Create new validator instance.
71
     *
72
     * @param $translator
73
     * @param $data
74
     * @param $rules
75
     * @param $messages
76
     * @param $customAttributes
77
     * @return \Illuminate\Validation\Validator
78
     */
79 36
    protected function createValidator($translator, $data, $rules, $messages, $customAttributes)
80
    {
81 36
        if (is_null($this->resolver)) {
82 12
            return new BaseValidator($translator, $data, $rules, $messages, $customAttributes);
83
        }
84
85 24
        return call_user_func($this->resolver, $translator, $data, $rules, $messages, $customAttributes);
86
    }
87
88
    /**
89
     * Closure used to trigger JsValidations.
90
     *
91
     * @return \Closure
92
     */
93 15
    public function validatorClosure()
94
    {
95 21
        return function ($attribute, $value, $parameters, BaseValidator $validator) {
96 12
            $remoteValidator = new Validator($validator);
97 12
            $remoteValidator->validate($value, $parameters);
98
99
            return $attribute;
100 36
        };
101
    }
102
}
103