Filter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 60
ccs 0
cts 10
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterVariable() 0 4 1
A __callStatic() 0 11 2
1
<?php
2
3
namespace Bavix\Slice;
4
5
use Bavix\Exceptions\Invalid;
6
7
/**
8
 * Class Filter
9
 *
10
 * @package Bavix\Slice
11
 *
12
 * @method static int getInt(mixed $data, $offset)
13
 * @method static float getFloat(mixed $data, $offset)
14
 * @method static bool getBool(mixed $data, $offset)
15
 * @method static string getEmail(mixed $data, $offset)
16
 * @method static string getIP(mixed $data, $offset)
17
 * @method static string getURL(mixed $data, $offset)
18
 */
19
class Filter
20
{
21
22
    /**
23
     * @var array
24
     */
25
    protected static $filters = [
26
        'getInt'   => FILTER_VALIDATE_INT,
27
        'getFloat' => FILTER_VALIDATE_FLOAT,
28
        'getBool'  => FILTER_VALIDATE_BOOLEAN,
29
        'getEmail' => FILTER_VALIDATE_EMAIL,
30
        'getIP'    => FILTER_VALIDATE_IP,
31
        'getURL'   => FILTER_VALIDATE_URL
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    protected static $defaults = [
38
        'getInt'   => 0,
39
        'getFloat' => .0,
40
        'getBool'  => false,
41
        'getEmail' => '',
42
        'getIP'    => '',
43
        'getURL'   => '',
44
    ];
45
46
    /**
47
     * @param mixed $data
48
     * @param mixed $type
49
     * @param mixed $default
50
     *
51
     * @return mixed
52
     */
53
    protected static function filterVariable($data, $type, $default)
54
    {
55
        return \filter_var($data, $type, [
56
            'options' => ['default' => $default]
57
        ]);
58
    }
59
60
    /**
61
     * @param $name
62
     * @param $arguments
63
     *
64
     * @return mixed
65
     *
66
     * @throws \BadFunctionCallException
67
     */
68
    public static function __callStatic($name, $arguments)
69
    {
70
71
        if (empty(static::$filters[$name])) {
72
            throw new Invalid('Filter `' . $name . '` not found');
73
        }
74
75
        return static::filterVariable(
76
            $arguments[0],
77
            static::$filters[$name],
78
            static::$defaults[$name]
79
        );
80
    }
81
}
82