Completed
Push — master ( dc083b...485c1a )
by Philip
02:02
created

Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 66
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setupValidators() 0 5 1
A validateField() 0 16 4
A gatherFailedFields() 0 12 4
A __construct() 0 3 1
A addValidator() 0 3 1
A validate() 0 16 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\Required;
15
16
class Validator {
17
18
    protected $availableValidators;
19
20
    protected function setupValidators() {
21
        $this->availableValidators = array(
22
            'required' => new Required()
23
        );
24
    }
25
26
    protected function validateField($validators, $value) {
27
        $result = array();
28
        foreach ($validators as $validator) {
29
            $name = $validator;
30
            $parameters = array();
31
            if (is_array($validator)) {
32
                $parameters = $validator;
33
                $name = array_shift($parameters);
34
            }
35
            if (!array_key_exists($name, $this->availableValidators)) {
36
                throw new ValidatorException($name.' not found as available validator.');
37
            }
38
            $result[$name] = $this->availableValidators[$name]->validate($value, $parameters);
39
        }
40
        return $result;
41
    }
42
43
    protected function gatherFailedFields($fields) {
44
        $failedFields = array();
45
        foreach ($fields as $field => $result) {
46
            foreach ($result as $subResult) {
47
                if (!$subResult) {
48
                    $failedFields[] = $field;
49
                    break;
50
                }
51
            }
52
        }
53
        return $failedFields;
54
    }
55
56
    public function __construct() {
57
        $this->setupValidators();
58
    }
59
60
    public function addValidator(ValidatorInterface $validator) {
61
        $this->validators[$validator->getType()] = $validator;
0 ignored issues
show
Bug introduced by
The property validators does not seem to exist. Did you mean availableValidators?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
    }
63
64
    public function validate(array $validators, array $data) {
65
        $fields = array();
66
        foreach ($validators as $field => $fieldValidators) {
67
            if (!array_key_exists($field, $data)) {
68
                continue;
69
            }
70
            $value = isset($data[$field]) ? $data[$field] : null;
71
            $fields[$field] = $this->validateField($fieldValidators, $value);
72
        }
73
        $failed = $this->gatherFailedFields($fields);
74
        return array(
75
            'valid' => count($failed) === 0,
76
            'fields' => $fields,
77
            'failed' => $failed
78
        );
79
    }
80
81
}
82