ValidatesData   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createValidator() 0 4 1
A tryValidateData() 0 13 2
A getValidatorFactory() 0 4 1
1
<?php
2
3
namespace Nord\Lumen\Core\Traits;
4
5
use Closure;
6
use Illuminate\Contracts\Validation\Factory;
7
use Illuminate\Contracts\Validation\Validator;
8
9
trait ValidatesData
10
{
11
    /**
12
     * @param array $data
13
     * @param array $rules
14
     * @param array $messages
15
     * @param array $customAttributes
16
     *
17
     * @return Validator
18
     */
19
    private function createValidator(array $data, array $rules, array $messages = [], array $customAttributes = [])
20
    {
21
        return $this->getValidatorFactory()->make($data, $rules, $messages, $customAttributes);
22
    }
23
24
    /**
25
     * @param array   $data
26
     * @param array   $rules
27
     * @param Closure $validationFailed
28
     * @param array   $messages
29
     * @param array   $customAttributes
30
     *
31
     * @return array
32
     */
33
    private function tryValidateData(
34
        array $data,
35
        array $rules,
36
        Closure $validationFailed,
37
        array $messages = [],
38
        array $customAttributes = []
39
    ) {
40
        $validator = $this->createValidator($data, $rules, $messages, $customAttributes);
41
42
        if ($validator->fails()) {
43
            call_user_func($validationFailed, $validator->errors()->getMessages());
44
        }
45
    }
46
47
    /**
48
     * @return Factory
49
     */
50
    private function getValidatorFactory()
51
    {
52
        return app('validator');
53
    }
54
}
55