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

Resolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 93
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolver() 0 6 1
A resolve() 0 9 1
A createValidator() 0 8 2
A validatorClosure() 0 9 1
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