Completed
Push — master ( 32467f...42bb8a )
by James Ekow Abaka
01:22
created

Validator::getValidation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
3
/*
4
 * Ntentan Framework
5
 * Copyright (c) 2008-2017 James Ekow Abaka Ainooson
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 *
26
 */
27
28
namespace ntentan\utils;
29
/**
30
 * Base validator class for validating data in associative arrays.
31
 * Validator class allows general validation rules to be defined that could be used to validate data in arbitrary
32
 * associative arrays. Rules are defined in an array where different validation types point the the fields in a given
33
 * associative array that will be validated. ntentan\utils ships with a set of default validations for lenght (or size),
34
 * regular expressions, numbers and presence (fields that are required).
35
 */
36
class Validator
37
{
38
39
    /**
40
     * An array which represnts the validation rules.
41
     * 
42
     * @var array
43
     */
44
    private $rules = [];
45
46
    /**
47
     * An array which holds the validation errors found after the last
48
     * validation was run.
49
     * 
50
     * @var array
51
     */
52
    private $invalidFields = [];
53
54
    /**
55
     * An array of loaded validations for this validator.
56
     * 
57
     * @var array 
58
     */
59
    private $validations = [];
60
61
    /**
62
     * A register of validations that could be used by this validator.
63
     * 
64
     * @var array
65
     */
66
    private $validationRegister = [
67
        'required' => '\ntentan\utils\validator\validations\RequiredValidation',
68
        'length' => '\ntentan\utils\validator\validations\LengthValidation',
69
        'numeric' => '\ntentan\utils\validator\validations\NumericValidation',
70
        'regexp' => '\ntentan\utils\validator\validations\RegexpValidation',
71
    ];
72
    private $validationData = [];
73
74
    /**
75
     * Get an instance of a validation class.
76
     * 
77
     * @param string $name
78
     * @return validator\Validation
79
     * @throws exceptions\ValidatorException
80
     */
81 16
    private function getValidation(string $name): validator\Validation
82
    {
83 16
        if (!isset($this->validations[$name])) {
84 16
            if (isset($this->validationRegister[$name])) {
85 14
                $class = $this->validationRegister[$name];
86
            } else {
87 2
                throw new exceptions\ValidatorException("Validator [$name] not found");
88
            }
89
90 14
            $params = isset($this->validationData[$name]) ? $this->validationData[$name] : null;
91 14
            $this->validations[$name] = new $class($params);
92
        }
93 14
        return $this->validations[$name];
94
    }
95
96
    /**
97
     * Register a validation type.
98
     * 
99
     * @param string $name The name of the validation to be used in validation descriptions.
100
     * @param string $class The name of the validation class to load.
101
     * @param mixed $data Any extra validation data that would be necessary for the validation.
102
     */
103 2
    protected function registerValidation(string $name, string $class, $data = null)
104
    {
105 2
        $this->validationRegister[$name] = $class;
106 2
        $this->validationData[$name] = $data;
107 2
    }
108
109
    /**
110
     * Set the validation rules.
111
     * 
112
     * @param array $rules
113
     */
114 16
    public function setRules(array $rules)
115
    {
116 16
        $this->rules = $rules;
117 16
    }
118
119
    /**
120
     * Returns an associative array of all the errors that occurred the last time the validator was run.
121
     * 
122
     * @return array
123
     */
124 14
    public function getInvalidFields(): array
125
    {
126 14
        return $this->invalidFields;
127
    }
128
129
    /**
130
     * Build a uniform field info array for various types of validations.
131
     * 
132
     * @param mixed $key
133
     * @param mixed $value
134
     * @return array
135
     */
136 16
    private function getFieldInfo($key, $value): array
137
    {
138 16
        $name = null;
139 16
        $options = [];
140 16
        if (is_numeric($key) && is_string($value)) {
141 6
            $name = $value;
142 10
        } else if (is_numeric($key) && is_array($value)) {
143 2
            $name = array_shift($value);
144 2
            $options = $value;
145 8
        } else if (is_string($key)) {
146 8
            $name = $key;
147 8
            $options = $value;
148
        }
149 16
        return ['name' => $name, 'options' => $options];
150
    }
151
    
152 16
    public function getRules() : array
153
    {
154 16
        return $this->rules;
155
    }
156
157
    /**
158
     * Validate data according to validation rules that have been set into
159
     * this validator.
160
     * 
161
     * @param array $data The data to be validated
162
     * @return bool
163
     */
164 16
    public function validate(array $data) : bool
165
    {
166 16
        $passed = true;
167 16
        $this->invalidFields = [];
168 16
        $rules = $this->getRules();
169 16
        foreach ($rules as $validation => $fields) {
170 16
            foreach ($fields as $key => $value) {
171 16
                $field = $this->getFieldInfo($key, $value);
172 16
                $validationInstance = $this->getValidation($validation);
173 14
                $validationStatus = $validationInstance->run($field, $data);
174 14
                $passed = $passed && $validationStatus;
175 14
                $this->invalidFields = array_merge_recursive(
176 14
                        $this->invalidFields, $validationInstance->getMessages()
177
                );
178
            }
179
        }
180 14
        return $passed;
181
    }
182
183
}
184