ValueParser::parseNumeric()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Codervio\Envmanager\Parser;
4
5
class ValueParser
6
{
7
    public function parse($input, $strict)
8
    {
9
        $input = $this->parseEmpty($input);
10
        $input = $this->parseFloat($input);
11
        $input = $this->parseNumeric($input);
12
        $input = $this->parseBool($input, $strict);
13
14
        return $input;
15
    }
16
17
    public function parseNumeric($input)
18
    {
19
        $var = filter_var($input, FILTER_VALIDATE_INT);
20
21
        if (ctype_digit($input) || is_int($var)) {
22
            return (int)$input;
23
        }
24
25
        return $input;
26
    }
27
28
    public function parseFloat($input)
29
    {
30
        $var = filter_var($input, FILTER_VALIDATE_FLOAT);
31
32
        if (is_float($var)) {
33
            return (float)$var;
34
        }
35
36
        return $input;
37
    }
38
39
    public function parseBool($var, $strict)
40
    {
41
        if ($strict) {
42
            if (preg_match('/^(true|yes|on|1|y)$/i', $var)) {
43
                return true;
44
            } else if (preg_match('/^(false|no|off|0|n)$/i', $var)) {
45
                return false;
46
            }
47
48
        } else {
49
            if (preg_match('/^(true|yes|on)$/i', $var)) {
50
                return true;
51
            } else if (preg_match('/^(false|no|off)$/i', $var)) {
52
                return false;
53
            }
54
55
        }
56
57
        return $var;
58
    }
59
60
    public function parseEmpty($var)
61
    {
62
        if (strlen($var) === 0) {
63
            return null;
64
        }
65
66
        return $var;
67
    }
68
69
    public function isBool($input)
70
    {
71
        $input = strtolower($input);
72
73
        return filter_var($input, FILTER_VALIDATE_BOOLEAN);
74
    }
75
}