Completed
Push — dev-master ( d3c67d...bb4d80 )
by Vijay
02:34
created

Validator::validate_valid_ip_public()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 9.2
cc 4
eloc 9
nc 3
nop 3
1
<?php
2
3
namespace FFMVC\Helpers;
4
5
/**
6
 * Validation Helper Class
7
 *
8
 * Add to composer.json:
9
 *    "wixel/gump": "dev-master"
10
 *
11
 * @package helpers
12
 * @author Vijay Mahrra <[email protected]>
13
 * @copyright (c) Copyright 2016 Vijay Mahrra
14
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
15
 * @url https://github.com/Wixel/GUMP
16
 */
17
class Validator extends \GUMP
18
{
19
    /**
20
     * Function to create and return previously created instance
21
     * Renamed from get_instance() to follow $f3 design pattern
22
     * as calling $this->get_instance() will ignore this class
23
     * and get a GUMP instance instead if this method did not exist
24
     *
25
     * @return Validator
26
     */
27
    public static function instance()
28
    {
29
        if (self::$instance === null) {
30
            self::$instance = new self();
31
        }
32
33
        return self::$instance;
34
    }
35
36
    /**
37
     * Perform data filtering against the provided ruleset.
38
     *
39
     * @param mixed $input
40
     * @param array optinal $ruleset ot use class ruleset
41
     * @return bool|array
42
     */
43
    public function filter(array $input, array $ruleset = [])
44
    {
45
        return empty($ruleset) ? parent::filter($input, $this->filter_rules) : parent::filter($input, $ruleset);
46
    }
47
48
    /**
49
     * Perform data validation against the provided ruleset.
50
     *
51
     * @param array $input
52
     * @param array optinal $ruleset ot use class ruleset
53
     * @return bool|array
54
     */
55
    public function validate(array $input, array $ruleset = [])
56
    {
57
        return empty($ruleset) ? parent::validate($input, $this->validation_rules) : parent::validate($input, $ruleset);
58
    }
59
60
    /**
61
     *  A custom filter named "lower".
62
     *
63
     *  The callback function receives two arguments:
64
     *  The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
65
     *
66
     * @param $value
67
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
68
     *
69
     * @return string
70
     */
71
    public function filter_lower(string $value, array $param = null): string
72
    {
73
        return strtolower($value);
74
    }
75
76
    /**
77
     *  A custom filter named "upper".
78
     *
79
     *  The callback function receives two arguments:
80
     *  The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
81
     *
82
     * @param $value
83
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
84
     *
85
     * @return string
86
     */
87
    public function filter_upper(string $value, array $param = null): string
88
    {
89
        return strtoupper($value);
90
    }
91
92
    /**
93
     * Strip whitespaces from the beginning of a string
94
     *
95
     * The callback function receives two arguments:
96
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
97
     *
98
     * @param $value
99
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
100
     *
101
     * @return string
102
     * @link https://fatfreeframework.com/utf-unicode-string-manager#ltrim
103
     */
104
    public function filter_ltrim(string $value, array $param = null): string
105
    {
106
        return \UTF::instance()->ltrim($value);
107
    }
108
109
    /**
110
     * Strip whitespaces from the end of a string
111
     *
112
     * The callback function receives two arguments:
113
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
114
     *
115
     * @param $value
116
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
117
     *
118
     * @return string
119
     * @link https://fatfreeframework.com/utf-unicode-string-manager#rtrim
120
     */
121
    public function filter_rtrim(string $value, array $param = null): string
122
    {
123
        return \UTF::instance()->rtrim($value);
124
    }
125
126
    /**
127
     * Strip whitespaces from the beginning and end of a string
128
     *
129
     * The callback function receives two arguments:
130
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
131
     *
132
     * @param $value
133
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
134
     *
135
     * @return string
136
     * @link https://fatfreeframework.com/utf-unicode-string-manager#trim
137
     */
138
    public function filter_trim(string $value, array $param = null): string
139
    {
140
        return \UTF::instance()->trim($value);
141
    }
142
143
    /**
144
     * Convert code points to Unicode symbols
145
     *
146
     * The callback function receives two arguments:
147
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
148
     *
149
     * @param $value
150
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
151
     *
152
     * @return string
153
     * @link https://fatfreeframework.com/utf-unicode-string-manager#translate
154
     */
155
    public function filter_translate(string $value, array $param = null): string
156
    {
157
        return \UTF::instance()->translate($value);
158
    }
159
160
    /**
161
     * Translate emoji tokens to Unicode font-supported symbols
162
     *
163
     * The callback function receives two arguments:
164
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
165
     *
166
     * @param $value
167
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
168
     *
169
     * @return string
170
     * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify
171
     */
172
    public function filter_emojify(string $value, array $param = null): string
173
    {
174
        return \UTF::instance()->emojify($value);
175
    }
176
177
    /**
178
     * Convert input to a slug
179
     *
180
     * The callback function receives two arguments:
181
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
182
     *
183
     * @param $value
184
     * @param array $param
0 ignored issues
show
Documentation introduced by
Should the type for parameter $param not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
185
     *
186
     * @return string
187
     * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify
188
     */
189
    public function filter_slug(string $value, array $param = null): string
190
    {
191
        return \Web::instance()->slug($value);
192
    }
193
194
    /**
195
     * Check whether the IP Address is Public
196
     *
197
     * Usage: '<index>' => 'valid_ip_public'
198
     *
199
     * @param string $field
200
     * @param array  $input
201
     * @param null   $param
202
     *
203
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
204
     */
205 View Code Duplication
    public function validate_valid_ip_public(string $field, array $input, $param = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207
        if (!isset($input[$field]) || empty($input[$field])) {
208
            return;
209
        }
210
        if (!\Audit::instance()->ispublic($input[$field])) {
211
            return array(
212
                'field' => $field,
213
                'value' => $input[$field],
214
                'rule' => __FUNCTION__,
215
                'param' => $param,
216
            );
217
        }
218
    }
219
220
    /**
221
     * Check whether the IP Address is NOT Public
222
     *
223
     * Usage: '<index>' => 'valid_ip_not_public'
224
     *
225
     * @param string $field
226
     * @param array  $input
227
     * @param null   $param
228
     *
229
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
230
     */
231 View Code Duplication
    public function validate_valid_ip_not_public(string $field, array $input, $param = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        if (!isset($input[$field]) || empty($input[$field])) {
234
            return;
235
        }
236
        if (\Audit::instance()->ispublic($input[$field])) {
237
            return array(
238
                'field' => $field,
239
                'value' => $input[$field],
240
                'rule' => __FUNCTION__,
241
                'param' => $param,
242
            );
243
        }
244
    }
245
246
    /**
247
     * Check whether the IP Address is Reserved
248
     *
249
     * Usage: '<index>' => 'valid_ip_reserved'
250
     *
251
     * @param string $field
252
     * @param array  $input
253
     * @param null   $param
254
     *
255
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
256
     */
257 View Code Duplication
    public function validate_valid_ip_reserved(string $field, array $input, $param = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
    {
259
        if (!isset($input[$field]) || empty($input[$field])) {
260
            return;
261
        }
262
        if (!\Audit::instance()->isreserved($input[$field])) {
263
            return array(
264
                'field' => $field,
265
                'value' => $input[$field],
266
                'rule' => __FUNCTION__,
267
                'param' => $param,
268
            );
269
        }
270
    }
271
272
    /**
273
     * Check whether the IP Address is Private or Reserved
274
     *
275
     * Usage: '<index>' => 'valid_ip_private'
276
     *
277
     * @param string $field
278
     * @param array  $input
279
     * @param null   $param
280
     *
281
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
282
     */
283 View Code Duplication
    public function validate_valid_ip_private(string $field, array $input, $param = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
284
    {
285
        if (!isset($input[$field]) || empty($input[$field])) {
286
            return;
287
        }
288
        if (!\Audit::instance()->isprivate($input[$field])) {
289
            return array(
290
                'field' => $field,
291
                'value' => $input[$field],
292
                'rule' => __FUNCTION__,
293
                'param' => $param,
294
            );
295
        }
296
    }
297
298
299
}
300