Completed
Push — master ( 666b3e...855c41 )
by James Ekow Abaka
01:15
created

Validator::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
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
     * A DI container for initializing the validations.
76
     *
77
     * @var Container
78
     */
79
    private $container;
80
81
    /**
82
     * Returns a new instance of the Validator.
83
     * 
84
     * @return \ntentan\utils\Validator
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86 16
    public static function getInstance(): self
87
    {
88 16
        return new self();
89
    }
90
91
    /**
92
     * Get an instance of a validation class.
93
     * 
94
     * @param string $name
95
     * @return validator\Validation
96
     * @throws exceptions\ValidatorException
97
     */
98 16
    private function getValidation(string $name): validator\Validation
99
    {
100 16
        if (!isset($this->validations[$name])) {
101 16
            if (isset($this->validationRegister[$name])) {
102 14
                $class = $this->validationRegister[$name];
103
            } else {
104 2
                throw new exceptions\ValidatorException("Validator [$name] not found");
105
            }
106
107 14
            $params = isset($this->validationData[$name]) ? $this->validationData[$name] : null;
108 14
            if($this->container) {
109
                $this->validations[$name] = $this->container->resolve($class, ['params' => $params]);
110
            } else {
111 14
                $this->validations[$name] = new $class($params);
112
            }
113
        }
114 14
        return $this->validations[$name];
115
    }
116
117
    /**
118
     * Register a validation type.
119
     * 
120
     * @param string $name The name of the validation to be used in validation descriptions.
121
     * @param string $class The name of the validation class to load.
122
     * @param mixed $data Any extra validation data that would be necessary for the validation.
123
     */
124 2
    protected function registerValidation(string $name, string $class, $data = null) : void
125
    {
126 2
        $this->validationRegister[$name] = $class;
127 2
        $this->validationData[$name] = $data;
128 2
    }
129
130
    /**
131
     * Set the validation rules.
132
     * 
133
     * @param array $rules
134
     */
135 16
    public function setRules(array $rules) : void
136
    {
137 16
        $this->rules = $rules;
138 16
    }
139
140
    /**
141
     * Returns an associative array of all the errors that occurred the last time the validator was run.
142
     * 
143
     * @return array
144
     */
145 14
    public function getInvalidFields(): array
146
    {
147 14
        return $this->invalidFields;
148
    }
149
150
    /**
151
     * Build a uniform field info array for various types of validations.
152
     * 
153
     * @param mixed $key
154
     * @param mixed $value
155
     * @return array
156
     */
157 16
    private function getFieldInfo($key, $value): array
158
    {
159 16
        $name = null;
160 16
        $options = [];
161 16
        if (is_numeric($key) && is_string($value)) {
162 6
            $name = $value;
163 10
        } else if (is_numeric($key) && is_array($value)) {
164 2
            $name = array_shift($value);
165 2
            $options = $value;
166 8
        } else if (is_string($key)) {
167 8
            $name = $key;
168 8
            $options = $value;
169
        }
170 16
        return ['name' => $name, 'options' => $options];
171
    }
172
    
173 16
    public function getRules() : array
174
    {
175 16
        return $this->rules;
176
    }
177
178
    /**
179
     * Validate data according to validation rules that have been set into
180
     * this validator.
181
     * 
182
     * @param array $data The data to be validated
183
     * @return bool
184
     */
185 16
    public function validate(array $data) : bool
186
    {
187 16
        $passed = true;
188 16
        $this->invalidFields = [];
189 16
        $rules = $this->getRules();
190 16
        foreach ($rules as $validation => $fields) {
191 16
            foreach ($fields as $key => $value) {
192 16
                $field = $this->getFieldInfo($key, $value);
193 16
                $validationInstance = $this->getValidation($validation);
194 14
                $validationStatus = $validationInstance->run($field, $data);
195 14
                $passed = $passed && $validationStatus;
196 14
                $this->invalidFields = array_merge_recursive(
197 14
                        $this->invalidFields, $validationInstance->getMessages()
198
                );
199
            }
200
        }
201 14
        return $passed;
202
    }
203
204
}
205