Completed
Push — master ( 3dd5b4...85a166 )
by Philip
02:53 queued 29s
created

Validator   B

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 15
Bugs 0 Features 5
Metric Value
wmc 13
c 15
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
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
        $this->availableValidators = array(
26
            'boolean' => new \Valdi\Validator\Boolean(),
27
            'dateTime' => new \Valdi\Validator\DateTime(),
28
            'email' => new \Valdi\Validator\Email(),
29
            'floating' => new \Valdi\Validator\Floating(),
30
            'inSet' => new \Valdi\Validator\InSet(),
31
            'integer' => new \Valdi\Validator\Integer(),
32
            'ip' => new \Valdi\Validator\IP(),
33
            'ipv4' => new \Valdi\Validator\IPv4(),
34
            'ipv6' => new \Valdi\Validator\IPv6(),
35
            'max' => new \Valdi\Validator\Max(),
36
            'min' => new \Valdi\Validator\Min(),
37
            'regexp' => new \Valdi\Validator\Regexp(),
38
            'required' => new \Valdi\Validator\Required(),
39
            'url' => new \Valdi\Validator\Url()
40
        );
41
    }
42
43
    protected function validateRule($name, $parameters, $value) {
44
        if (!array_key_exists($name, $this->availableValidators)) {
45
            throw new ValidatorException('"' . $name . '" not found as available validator.');
46
        }
47
        return $this->availableValidators[$name]->validate($value, $parameters);
48
    }
49
50
    protected function validateField($fieldRules, $value) {
51
        $result = array();
52
        foreach ($fieldRules as $rule) {
53
            $name = $rule;
54
            $parameters = array();
55
            if (is_array($rule)) {
56
                $parameters = $rule;
57
                $name = array_shift($parameters);
58
            }
59
            $valid = $this->validateRule($name, $parameters, $value);
60
            if (!$valid) {
61
                $result[] = $name;
62
            }
63
        }
64
        return $result;
65
    }
66
67
    public function __construct() {
68
        $this->setupValidators();
69
    }
70
71
    public function addValidator($name, ValidatorInterface $validator) {
72
        $this->availableValidators[$name] = $validator;
73
    }
74
75
    public function validate(array $rules, array $data) {
76
        $errors = array();
77
        foreach ($rules as $field => $fieldRules) {
78
            $value = isset($data[$field]) ? $data[$field] : null;
79
            $fieldErrors = $this->validateField($fieldRules, $value);
80
            if (!empty($fieldErrors)) {
81
                $errors[$field] = $fieldErrors;
82
            }
83
        }
84
        return array(
85
            'valid' => count($errors) === 0,
86
            'errors' => $errors
87
        );
88
    }
89
90
}
91