Passed
Push — master ( 54a427...0365a3 )
by Alexey
06:00 queued 03:41
created

Validator::isIntValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 12
rs 10
1
<?php
2
3
4
namespace Lexuss1979\Validol;
5
6
7
use Lexuss1979\Validol\Validations\ValidationFactory;
8
9
class Validator
10
{
11
    private $validated;
12
    private $errors;
13
    private $validationFactory;
14
15 40
    public function __construct()
16
    {
17 40
        $this->validationFactory = new ValidationFactory();
18 40
    }
19
20 40
    public function validate($data, $rules)
21
    {
22 40
        $validationResult = true;
23 40
        $this->validated = [];
24 40
        $this->errors = [];
25
26 40
        foreach ($rules as $dataKey => $rule){
27 40
            $testedValue = $data[$dataKey] ?? null;
28 40
            $validations = explode(" ",$rule);
29
30 40
            $valueIsValid = true;
31 40
            foreach ($validations as $validation){
32 40
                $validation = $this->validationFactory->get($validation);
33 40
                if(!$validation->validate($data, $dataKey)){
34 25
                    $this->errors[$dataKey][] = $validation->error();
35 25
                    $valueIsValid = false;
36
                }
37
            }
38
39 40
            if($valueIsValid) {
40 20
                $this->validated[$dataKey] = $testedValue;
41
            } else {
42 25
                $validationResult = false;
43
            }
44
45
        }
46 40
        return $validationResult;
47
    }
48
49 3
    public function errors(){
50 3
        return $this->errors;
51
    }
52
53 2
    public function validated(){
54 2
        return $this->validated;
55
    }
56
}