Completed
Push — master ( 98fcab...5f247a )
by Philip
02:01
created

Validator::validateField()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 14
nc 7
nop 2
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 validateField($validators, $value) {
50
        $result = array();
51
        foreach ($validators as $validator) {
52
            $name = $validator;
53
            $parameters = array();
54
            if (is_array($validator)) {
55
                $parameters = $validator;
56
                $name = array_shift($parameters);
57
            }
58
            if (!array_key_exists($name, $this->availableValidators)) {
59
                throw new ValidatorException('"'.$name . '" not found as available validator.');
60
            }
61
            $valid = $this->availableValidators[$name]->validate($value, $parameters);
62
            if (!$valid) {
63
                $result[$name] = false;
64
            }
65
        }
66
        return $result;
67
    }
68
69
    public function __construct() {
70
        $this->setupValidators();
71
    }
72
73
    public function addValidator($name, ValidatorInterface $validator) {
74
        $this->availableValidators[$name] = $validator;
75
    }
76
77
    public function validate(array $validators, array $data) {
78
        $errors = array();
79
        foreach ($validators as $field => $fieldValidators) {
80
            $value = isset($data[$field]) ? $data[$field] : null;
81
            $fieldErrors = $this->validateField($fieldValidators, $value);
82
            if ($fieldErrors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldErrors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83
                $errors[$field] = $fieldErrors;
84
            }
85
        }
86
        return array(
87
            'valid' => count($errors) === 0,
88
            'errors' => $errors
89
        );
90
    }
91
92
}
93