Validator::getValidation()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
rs 10
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 and used in validating data stored in arbitrary
32
 * associative arrays. Rules are defined in an array where different validation types point to the fields in a given
33
 * associative array that will be validated. ntentan\utils ships with a set of default validations for length (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
73
    /**
74
     * Custom validation data to be passed on to a validation when instantiated.
75
     *
76
     * @var mixed
77
     */
78
    private $validationData;
79
80
    /**
81
     * Get an instance of a validation class.
82
     * 
83
     * @param string $name
84
     * @return validator\Validation
85
     * @throws exceptions\ValidatorNotFoundException
86
     */
87 16
    private function getValidation(string $name): validator\Validation
88
    {
89 16
        if (!isset($this->validations[$name])) {
90 16
            if (isset($this->validationRegister[$name])) {
91 14
                $class = $this->validationRegister[$name];
92
            } else {
93 2
                throw new exceptions\ValidatorNotFoundException("Validator [$name] not found");
94
            }
95
96 14
            $params = isset($this->validationData[$name]) ? $this->validationData[$name] : null;
97 14
            $this->validations[$name] = new $class($params);
98
        }
99 14
        return $this->validations[$name];
100
    }
101
102
    /**
103
     * Register a validation type.
104
     * 
105
     * @param string $name The name of the validation to be used in validation descriptions.
106
     * @param string $class The name of the validation class to load.
107
     * @param mixed $data Any extra validation data that would be necessary for the validation.
108
     */
109 2
    protected function registerValidation(string $name, string $class, $data = null)
110
    {
111 2
        $this->validationRegister[$name] = $class;
112 2
        $this->validationData[$name] = $data;
113
    }
114
115
    /**
116
     * Set the validation rules.
117
     * 
118
     * @param array $rules
119
     */
120 16
    public function setRules(array $rules)
121
    {
122 16
        $this->rules = $rules;
123
    }
124
125
    /**
126
     * Returns an associative array of all the errors that occurred the last time the validator was run.
127
     * 
128
     * @return array
129
     */
130 14
    public function getInvalidFields(): array
131
    {
132 14
        return $this->invalidFields;
133
    }
134
135
    /**
136
     * Build a uniform field info array for various types of validations.
137
     * 
138
     * @param mixed $key
139
     * @param mixed $value
140
     * @return array
141
     */
142 16
    private function getFieldInfo($key, $value): array
143
    {
144 16
        $name = null;
145 16
        $options = [];
146 16
        if (is_numeric($key) && is_string($value)) {
147 6
            $name = $value;
148 10
        } else if (is_numeric($key) && is_array($value)) {
149 2
            $name = array_shift($value);
150 2
            $options = $value;
151 8
        } else if (is_string($key)) {
152 8
            $name = $key;
153 8
            $options = $value;
154
        }
155 16
        return ['name' => $name, 'options' => $options];
156
    }
157
158
    /**
159
     * Return the validation rules in this validator.
160
     *
161
     * @return array
162
     */
163 16
    public function getRules() : array
164
    {
165 16
        return $this->rules;
166
    }
167
168
    /**
169
     * Validate data according to validation rules that have been set into
170
     * this validator.
171
     *
172
     * @param array $data The data to be validated
173
     * @return bool
174
     * @throws exceptions\ValidatorNotFoundException
175
     */
176 16
    public function validate(array $data) : bool
177
    {
178 16
        $passed = true;
179 16
        $this->invalidFields = [];
180 16
        $rules = $this->getRules();
181 16
        foreach ($rules as $validation => $fields) {
182 16
            foreach ($fields as $key => $value) {
183 16
                $field = $this->getFieldInfo($key, $value);
184 16
                $validationInstance = $this->getValidation($validation);
185 14
                $validationStatus = $validationInstance->run($field, $data);
186 14
                $passed = $passed && $validationStatus;
187 14
                $this->invalidFields = array_merge_recursive(
188 14
                        $this->invalidFields, $validationInstance->getMessages()
189 14
                );
190
            }
191
        }
192 14
        return $passed;
193
    }
194
195
}
196