Validator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 27
c 1
b 0
f 0
dl 0
loc 130
ccs 33
cts 33
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A required() 0 3 1
A buildRule() 0 9 2
A __construct() 0 4 1
A validate() 0 9 4
A passes() 0 3 1
A min() 0 3 1
A parseRule() 0 14 3
A getErrors() 0 3 1
A max() 0 3 1
A failed() 0 3 1
1
<?php
2
namespace Fyuze\Validator;
3
4
class Validator
5
{
6
    /**
7
     * @var array
8
     */
9
    protected $input;
10
11
    /**
12
     * @var array
13
     */
14
    protected $rules;
15
16
    /**
17
     * @var array
18
     */
19
    protected $errors = [];
20
21
    /**
22
     * Validator constructor.
23
     * @param array $input
24
     * @param array $rules
25
     */
26 2
    public function __construct(array $input = [], array $rules = [])
27
    {
28 2
        $this->input = $input + array_fill_keys(array_keys($rules), null);
29 2
        $this->rules = $rules;
30
    }
31
32
    /**
33
     * @return bool
34
     */
35 1
    public function passes()
36
    {
37 1
        return $this->validate();
38
    }
39
40
    /**
41
     * @return bool
42
     */
43 2
    public function failed()
44
    {
45 2
        return !$this->validate();
46
    }
47
48
    /**
49
     * @return array
50
     */
51 1
    public function getErrors()
52
    {
53 1
        return $this->errors;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59 2
    protected function validate()
60
    {
61 2
        foreach ($this->input as $key => $input) {
62 2
            if (array_key_exists($key, $this->rules) && $rule = $this->parseRule($key, $input)) {
63 1
                $this->errors[] = $rule;
64
            }
65
        }
66
67 2
        return count($this->errors) === 0;
68
    }
69
70
    /**
71
     * @param $key
72
     * @param $value
73
     * @return bool|string
74
     */
75 2
    protected function parseRule($key, $value)
76
    {
77 2
        $params = [$value];
78
79 2
        foreach (explode('|', $this->rules[$key]) as $rule) {
80
81 2
            list($rule, $params) = $this->buildRule($rule, $params);
82
83 2
            if (call_user_func_array([$this, $rule], $params) === false) {
84 1
                return sprintf('Error with rule %s on field %s with %s', $rule, $key, implode(',', $params));
85
            }
86
        }
87
88 1
        return false;
89
    }
90
91
    /**
92
     * @param $rule
93
     * @param $params
94
     * @return array
95
     */
96 2
    protected function buildRule($rule, $params)
97
    {
98 2
        if (strpos($rule, ':') !== false) {
99 2
            $data = explode(':', $rule);
100 2
            $params[] = array_pop($data);
101 2
            $rule = reset($data);
102
        }
103
104 2
        return [$rule, $params];
105
    }
106
107
    /**
108
     * @param $value
109
     * @return bool
110
     */
111 2
    protected function required($value)
112
    {
113 2
        return !empty($value);
114
    }
115
116
    /**
117
     * @param $value
118
     * @param $max
119
     * @return bool
120
     */
121 1
    protected function max($value, $max)
122
    {
123 1
        return strlen($value) <= $max;
124
    }
125
126
    /**
127
     * @param $value
128
     * @param $min
129
     * @return bool
130
     */
131 2
    protected function min($value, $min)
132
    {
133 2
        return strlen($value) >= $min;
134
    }
135
}
136