Completed
Pull Request — master (#7)
by
unknown
14:35
created

DataFilter::filterBoolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php namespace Comodojo\Zip\Foundation\Validation;
2
3
/**
4
 * @package     Comodojo Foundation
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @license     MIT
7
 *
8
 * LICENSE:
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16
 * THE SOFTWARE.
17
 */
18
19
class DataFilter {
20
21
    /**
22
     * Filter an integer.
23
     *
24
     * This method is a shortcut to filter_var using FILTER_VALIDATE_INT
25
     *
26
     * @param int $int Int to filter
27
     * @param int $min Min value (default to ~PHP_INT_MAX)
28
     * @param int $max Max value (default to PHP_INT_MAX)
29
     * @param int $default Default value
30
     * @return int
31
     */
32
    public static function filterInteger($int, $min=~PHP_INT_MAX, $max=PHP_INT_MAX, $default=0) {
33
34
        return filter_var($int, FILTER_VALIDATE_INT, array(
35
            'options' => array(
36
                'default' => $default,
37
                'min_range' => $min,
38
                'max_range' => $max
39
            )
40
        ));
41
42
    }
43
44
    /**
45
     * Filter a TCP/UDP port
46
     *
47
     * @param int $port
48
     * @param array $default
49
     * @return int
50
     */
51
    public static function filterPort($port, $default = 80) {
52
53
        return self::filterInteger($port, 1, 65535, $default);
0 ignored issues
show
Bug introduced by
It seems like $default can also be of type array; however, parameter $default of Comodojo\Zip\Foundation\...Filter::filterInteger() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        return self::filterInteger($port, 1, 65535, /** @scrutinizer ignore-type */ $default);
Loading history...
54
55
    }
56
57
    /**
58
     * filter a bool.
59
     *
60
     * This method is a shortcut to filter_var using FILTER_VALIDATE_BOOLEAN
61
     *
62
     * @param bool $bool
63
     * @param array $default
64
     * @return bool
65
     */
66
    public static function filterBoolean($bool, $default = false) {
67
68
        return filter_var($bool, FILTER_VALIDATE_BOOLEAN, array(
69
            'options' => array(
70
                'default' => $default
71
            )
72
        ));
73
74
    }
75
76
}
77