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

Validator::rules()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 13
nc 3
nop 1
crap 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
}