Completed
Push — master ( d4d365...f5f4dc )
by Alexandr
01:51
created

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 2
A create() 0 4 1
B rules() 0 20 7
1
<?php
2
3
namespace Bricks\Data;
4
5
use Bricks\Exception\ValidationException;
6
7
/**
8
 * Class Validator
9
 * @package Bricks\Data
10
 */
11
class Validator extends \Valitron\Validator
12
{
13
    /**
14
     * @return bool
15
     * @throws ValidationException
16
     */
17 5
    public function validate(): bool
18
    {
19 5
        if (parent::validate() === false) {
20 3
            throw new ValidationException($this->_errors);
21
        }
22 2
        return true;
23
    }
24
25
    /**
26
     * @param array $data
27
     * @return static
28
     */
29 4
    public static function create(array $data = [])
30
    {
31 4
        return new static($data);
32
    }
33
34
    /**
35
     * @param array $rules
36
     */
37 6
    public function rules($rules)
38
    {
39 6
        foreach ($rules as $ruleType => $params) {
40 6
            if (is_array($params)) {
41 6
                foreach ($params as $innerParams) {
42 6
                    if (!is_array($innerParams)) {
43 3
                        $innerParams = (array)$innerParams;
44
                    }
45 6
                    array_unshift($innerParams, $ruleType);
46 6
                    call_user_func_array([$this, 'rule'], $innerParams);
47 6
                    $arguments = array_pop($innerParams);
48 6
                    if (is_array($arguments) && isset($arguments['message'])) {
49 6
                        $this->message($arguments['message']);
50
                    }
51
                }
52
            } else {
53 6
                $this->rule($ruleType, $params);
54
            }
55
        }
56
    }
57
}