Completed
Push — master ( 274293...37ce8a )
by Philip
02:20
created

Validator   B

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 14
Bugs 0 Features 5
Metric Value
wmc 13
c 14
b 0
f 5
lcom 1
cbo 16
dl 0
loc 71
rs 8.4614

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setupValidators() 0 18 1
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
use Valdi\Validator\Boolean;
16
use Valdi\Validator\DateTime;
17
use Valdi\Validator\Email;
18
use Valdi\Validator\Floating;
19
use Valdi\Validator\InSet;
20
use Valdi\Validator\Integer;
21
use Valdi\Validator\IP;
22
use Valdi\Validator\IPv4;
23
use Valdi\Validator\IPv6;
24
use Valdi\Validator\Max;
25
use Valdi\Validator\Min;
26
use Valdi\Validator\Regexp;
27
use Valdi\Validator\Required;
28
use Valdi\Validator\Url;
29
30
/**
31
 * The Validator is used to chain Validators together and validate a set of data
32
 * with it.
33
 */
34
class Validator {
35
36
    protected $availableValidators;
37
38
    protected function setupValidators() {
39
        $this->availableValidators = array(
40
            'boolean' => new Boolean(),
41
            'dateTime' => new DateTime(),
42
            'email' => new Email(),
43
            'floating' => new Floating(),
44
            'inSet' => new InSet(),
45
            'integer' => new Integer(),
46
            'ip' => new IP(),
47
            'ipv4' => new IPv4(),
48
            'ipv6' => new IPv6(),
49
            'max' => new Max(),
50
            'min' => new Min(),
51
            'regexp' => new Regexp(),
52
            'required' => new Required(),
53
            'url' => new Url()
54
        );
55
    }
56
57
    protected function validateRule($name, $parameters, $value) {
58
        if (!array_key_exists($name, $this->availableValidators)) {
59
            throw new ValidatorException('"' . $name . '" not found as available validator.');
60
        }
61
        return $this->availableValidators[$name]->validate($value, $parameters);
62
    }
63
64
    protected function validateField($fieldRules, $value) {
65
        $result = array();
66
        foreach ($fieldRules as $rule) {
67
            $name = $rule;
68
            $parameters = array();
69
            if (is_array($rule)) {
70
                $parameters = $rule;
71
                $name = array_shift($parameters);
72
            }
73
            $valid = $this->validateRule($name, $parameters, $value);
74
            if (!$valid) {
75
                $result[] = $name;
76
            }
77
        }
78
        return $result;
79
    }
80
81
    public function __construct() {
82
        $this->setupValidators();
83
    }
84
85
    public function addValidator($name, ValidatorInterface $validator) {
86
        $this->availableValidators[$name] = $validator;
87
    }
88
89
    public function validate(array $rules, array $data) {
90
        $errors = array();
91
        foreach ($rules as $field => $fieldRules) {
92
            $value = isset($data[$field]) ? $data[$field] : null;
93
            $fieldErrors = $this->validateField($fieldRules, $value);
94
            if (!empty($fieldErrors)) {
95
                $errors[$field] = $fieldErrors;
96
            }
97
        }
98
        return array(
99
            'valid' => count($errors) === 0,
100
            'errors' => $errors
101
        );
102
    }
103
104
}
105