Completed
Push — master ( 5034f5...9d8248 )
by James Ekow Abaka
01:13
created

Validator::getInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
/**
31
 * Base validator class for validating data in associative arrays.
32
 * Validator class allows general validation rules to be defined that could be used to validate data in arbitrary
33
 * associative arrays. Rules are defined in an array where different validation types point the the fields in a given
34
 * associative array that will be validated. ntentan\utils ships with a set of default validations for lenght (or size),
35
 * regular expressions, numbers and presence (fields that are required).
36
 */
37
class Validator
38
{
39
40
    /**
41
     * An array which represnts the validation rules.
42
     * 
43
     * @var array
44
     */
45
    private $rules = [];
46
47
    /**
48
     * An array which holds the validation errors found after the last
49
     * validation was run.
50
     * 
51
     * @var array
52
     */
53
    private $invalidFields = [];
54
55
    /**
56
     * An array of loaded validations for this validator.
57
     * 
58
     * @var array 
59
     */
60
    private $validations = [];
61
62
    /**
63
     * A register of validations that could be used by this validator.
64
     * 
65
     * @var array
66
     */
67
    private $validationRegister = [
68
        'required' => '\ntentan\utils\validator\validations\RequiredValidation',
69
        'length' => '\ntentan\utils\validator\validations\LengthValidation',
70
        'numeric' => '\ntentan\utils\validator\validations\NumericValidation',
71
        'regexp' => '\ntentan\utils\validator\validations\RegexpValidation',
72
    ];
73
    private $validationData = [];
74
75
    /**
76
     * Returns a new instance of the Validator.
77
     * 
78
     * @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...
79
     */
80 8
    public static function getInstance(): self
81
    {
82 8
        return new self();
83
    }
84
85
    /**
86
     * Get an instance of a validation class.
87
     * 
88
     * @param string $name
89
     * @return validator\Validation
90
     * @throws exceptions\ValidatorException
91
     */
92 8
    private function getValidation(string $name): validator\Validation
93
    {
94 8
        if (!isset($this->validations[$name])) {
95 8
            if (isset($this->validationRegister[$name])) {
96 7
                $class = $this->validationRegister[$name];
97
            } else {
98 1
                throw new exceptions\ValidatorException("Validator [$name] not found");
99
            }
100 7
            $this->validations[$name] = new $class(isset($this->validationData[$name]) ? $this->validationData[$name] : null);
101
        }
102 7
        return $this->validations[$name];
103
    }
104
105
    /**
106
     * Register a validation type.
107
     * 
108
     * @param string $name The name of the validation to be used in validation descriptions.
109
     * @param string $class The name of the validation class to load.
110
     * @param mixed $data Any extra validation data that would be necessary for the validation.
111
     */
112 1
    protected function registerValidation(string $name, string $class, $data = null) : void
113
    {
114 1
        $this->validationRegister[$name] = $class;
115 1
        $this->validationData[$name] = $data;
116 1
    }
117
118
    /**
119
     * Set the validation rules.
120
     * 
121
     * @param array $rules
122
     */
123 8
    public function setRules(array $rules) : void
124
    {
125 8
        $this->rules = $rules;
126 8
    }
127
128
    /**
129
     * Returns an associative array of all the errors that occurred the last time the validator was run.
130
     * 
131
     * @return array
132
     */
133 7
    public function getInvalidFields(): array
134
    {
135 7
        return $this->invalidFields;
136
    }
137
138
    /**
139
     * Build a uniform field info array for various types of validations.
140
     * 
141
     * @param mixed $key
142
     * @param mixed $value
143
     * @return array
144
     */
145 8
    private function getFieldInfo($key, $value): array
146
    {
147 8
        $name = null;
148 8
        $options = [];
149 8
        if (is_numeric($key) && is_string($value)) {
150 3
            $name = $value;
151 5
        } else if (is_numeric($key) && is_array($value)) {
152 1
            $name = array_shift($value);
153 1
            $options = $value;
154 4
        } else if (is_string($key)) {
155 4
            $name = $key;
156 4
            $options = $value;
157
        }
158 8
        return ['name' => $name, 'options' => $options];
159
    }
160
161
    /**
162
     * Validate data according to validation rules that have been set into
163
     * this validator.
164
     * @param array $data The data to be validated
165
     */
166 8
    public function validate(array $data)
167
    {
168 8
        $passed = true;
169 8
        $this->invalidFields = [];
170 8
        foreach ($this->rules as $validation => $fields) {
171 8
            foreach ($fields as $key => $value) {
172 8
                $field = $this->getFieldInfo($key, $value);
173 8
                $validationInstance = $this->getValidation($validation);
174 7
                $passed &= $validationInstance->run($field, $data);
175 7
                $this->invalidFields = array_merge_recursive(
176 7
                        $this->invalidFields, $validationInstance->getMessages()
177
                );
178
            }
179
        }
180 7
        return $passed;
181
    }
182
183
}
184