Completed
Push — master ( 37ce8a...a9986e )
by Philip
02:25
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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