Validator::validate_valid_ip_public()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
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 mixed $value
67
     * @param mixed $param
68
     * @return string
69
     */
70
    public function filter_lower($value, $param = null): string
71
    {
72
        return strtolower($value);
73
    }
74
75
    /**
76
     *  A custom filter named "upper".
77
     *
78
     *  The callback function receives two arguments:
79
     *  The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
80
     *
81
     * @param mixed $value
82
     * @param mixed $param
83
     * @return string
84
     */
85
    public function filter_upper($value, $param = null): string
86
    {
87
        return strtoupper($value);
88
    }
89
90
    /**
91
     * Strip whitespaces from the beginning of a string
92
     *
93
     * The callback function receives two arguments:
94
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
95
     *
96
     * @param mixed $value
97
     * @param mixed $param
98
     * @return string
99
     * @link https://fatfreeframework.com/utf-unicode-string-manager#ltrim
100
     */
101
    public function filter_ltrim($value, $param = null): string
102
    {
103
        return \UTF::instance()->ltrim($value);
104
    }
105
106
    /**
107
     * Strip whitespaces from the end of a string
108
     *
109
     * The callback function receives two arguments:
110
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
111
     *
112
     * @param mixed $value
113
     * @param mixed $param
114
     * @return string
115
     * @link https://fatfreeframework.com/utf-unicode-string-manager#rtrim
116
     */
117
    public function filter_rtrim($value, $param = null): string
118
    {
119
        return \UTF::instance()->rtrim($value);
120
    }
121
122
    /**
123
     * Strip whitespaces from the beginning and end of a string
124
     *
125
     * The callback function receives two arguments:
126
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
127
     *
128
     * @param mixed $value
129
     * @param mixed $param
130
     * @return string
131
     * @link https://fatfreeframework.com/utf-unicode-string-manager#trim
132
     */
133
    public function filter_trim($value, $param = null): string
134
    {
135
        return \UTF::instance()->trim($value);
136
    }
137
138
    /**
139
     * Convert code points to Unicode symbols
140
     *
141
     * The callback function receives two arguments:
142
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
143
     *
144
     * @param mixed $value
145
     * @param mixed $param
146
     * @return string
147
     * @link https://fatfreeframework.com/utf-unicode-string-manager#translate
148
     */
149
    public function filter_translate($value, $param = null): string
150
    {
151
        return \UTF::instance()->translate($value);
152
    }
153
154
    /**
155
     * Translate emoji tokens to Unicode font-supported symbols
156
     *
157
     * The callback function receives two arguments:
158
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
159
     *
160
     * @param mixed $value
161
     * @param mixed $param
162
     * @return string
163
     * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify
164
     */
165
    public function filter_emojify($value, $param = null): string
166
    {
167
        return \UTF::instance()->emojify($value);
168
    }
169
170
    /**
171
     * Convert input to a slug
172
     *
173
     * The callback function receives two arguments:
174
     * The value to filter, and any parameters used in the filter rule. It should returned the filtered value.
175
     *
176
     * @param mixed $value
177
     * @param mixed $param
178
     * @return string
179
     * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify
180
     */
181
    public function filter_slug($value, $param = null): string
182
    {
183
        return \Web::instance()->slug($value);
184
    }
185
186
    /**
187
     * Check whether the IP Address is Public
188
     *
189
     * Usage: '<index>' => 'valid_ip_public'
190
     *
191
     * @param string $field
192
     * @param array  $input
193
     * @param null|array $param
194
     *
195
     * @return null|array
196
     */
197
    public function validate_valid_ip_public(string $field, array $input, $param = null)
0 ignored issues
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...
198
    {
199
        if (!isset($input[$field]) || empty($input[$field])) {
200
            return;
201
        }
202
        if (!\Audit::instance()->ispublic($input[$field])) {
203
            return array(
204
                'field' => $field,
205
                'value' => $input[$field],
206
                'rule' => __FUNCTION__,
207
                'param' => $param,
208
            );
209
        }
210
    }
211
212
    /**
213
     * Check whether the IP Address is NOT Public
214
     *
215
     * Usage: '<index>' => 'valid_ip_not_public'
216
     *
217
     * @param string $field
218
     * @param array  $input
219
     * @param null|array $param
220
     * @return null|array
221
     */
222
    public function validate_valid_ip_not_public(string $field, array $input, $param = null)
0 ignored issues
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...
223
    {
224
        if (!isset($input[$field]) || empty($input[$field])) {
225
            return;
226
        }
227
        if (\Audit::instance()->ispublic($input[$field])) {
228
            return array(
229
                'field' => $field,
230
                'value' => $input[$field],
231
                'rule' => __FUNCTION__,
232
                'param' => $param,
233
            );
234
        }
235
    }
236
237
    /**
238
     * Check whether the IP Address is Reserved
239
     *
240
     * Usage: '<index>' => 'valid_ip_reserved'
241
     *
242
     * @param string $field
243
     * @param array  $input
244
     * @param null|array $param
245
     * @return null|array
246
     */
247
    public function validate_valid_ip_reserved(string $field, array $input, $param = null)
0 ignored issues
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...
248
    {
249
        if (!isset($input[$field]) || empty($input[$field])) {
250
            return;
251
        }
252
        if (!\Audit::instance()->isreserved($input[$field])) {
253
            return array(
254
                'field' => $field,
255
                'value' => $input[$field],
256
                'rule' => __FUNCTION__,
257
                'param' => $param,
258
            );
259
        }
260
    }
261
262
    /**
263
     * Check whether the IP Address is Private or Reserved
264
     *
265
     * Usage: '<index>' => 'valid_ip_private'
266
     *
267
     * @param string $field
268
     * @param array  $input
269
     * @param null|array $param
270
     * @return null|array
271
     */
272
    public function validate_valid_ip_private(string $field, array $input, $param = null)
0 ignored issues
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...
273
    {
274
        if (!isset($input[$field]) || empty($input[$field])) {
275
            return;
276
        }
277
        if (!\Audit::instance()->isprivate($input[$field])) {
278
            return array(
279
                'field' => $field,
280
                'value' => $input[$field],
281
                'rule' => __FUNCTION__,
282
                'param' => $param,
283
            );
284
        }
285
    }
286
287
}
288