Completed
Push — master ( b1c3f0...fcda50 )
by Philip
02:39
created

Validator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 16
Bugs 0 Features 5
Metric Value
wmc 14
c 16
b 0
f 5
lcom 1
cbo 1
dl 0
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setupValidators() 0 25 2
A validateRule() 0 6 2
A validateField() 0 16 4
A __construct() 0 3 1
A addValidator() 0 3 1
A validate() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Valdi;
13
14
use Valdi\Validator\ValidatorInterface;
15
16
/**
17
 * The Validator is used to chain Validators together and validate a set of data
18
 * with it.
19
 */
20
class Validator {
21
22
    protected $availableValidators;
23
24
    protected function setupValidators() {
25
26
        $validators = array(
27
            'boolean' => 'Boolean',
28
            'dateTime' => 'DateTime',
29
            'email' => 'Email',
30
            'floating' => 'Floating',
31
            'inSet' => 'InSet',
32
            'integer' => 'Integer',
33
            'ip' => 'IP',
34
            'ipv4' => 'IPv4',
35
            'ipv6' => 'IPv6',
36
            'max' => 'Max',
37
            'min' => 'Min',
38
            'regexp' => 'Regexp',
39
            'required' => 'Required',
40
            'url' => 'Url'
41
        );
42
43
        $this->availableValidators = array();
44
        foreach ($validators as $name => $type) {
45
            $class = '\\Valdi\\Validator\\' . $type;
46
            $this->availableValidators[$name] = new $class();
47
        }
48
    }
49
50
    protected function validateRule($name, $parameters, $value) {
51
        if (!array_key_exists($name, $this->availableValidators)) {
52
            throw new ValidatorException('"' . $name . '" not found as available validator.');
53
        }
54
        return $this->availableValidators[$name]->validate($value, $parameters);
55
    }
56
57
    protected function validateField($fieldRules, $value) {
58
        $result = array();
59
        foreach ($fieldRules as $rule) {
60
            $name = $rule;
61
            $parameters = array();
62
            if (is_array($rule)) {
63
                $parameters = $rule;
64
                $name = array_shift($parameters);
65
            }
66
            $valid = $this->validateRule($name, $parameters, $value);
67
            if (!$valid) {
68
                $result[] = $name;
69
            }
70
        }
71
        return $result;
72
    }
73
74
    public function __construct() {
75
        $this->setupValidators();
76
    }
77
78
    public function addValidator($name, ValidatorInterface $validator) {
79
        $this->availableValidators[$name] = $validator;
80
    }
81
82
    public function validate(array $rules, array $data) {
83
        $errors = array();
84
        foreach ($rules as $field => $fieldRules) {
85
            $value = isset($data[$field]) ? $data[$field] : null;
86
            $fieldErrors = $this->validateField($fieldRules, $value);
87
            if (!empty($fieldErrors)) {
88
                $errors[$field] = $fieldErrors;
89
            }
90
        }
91
        return array(
92
            'valid' => count($errors) === 0,
93
            'errors' => $errors
94
        );
95
    }
96
97
}
98