Completed
Push — master ( fcc20a...6d54a7 )
by Philip
02:05
created

Validator::setupValidators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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