Completed
Push — master ( fcda50...7a60df )
by Philip
02:48
created

Validator::createValidators()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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