ValueParser   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseFloat() 0 9 2
A parseBool() 0 19 6
A isBool() 0 5 1
A parseEmpty() 0 7 2
A parse() 0 8 1
A parseNumeric() 0 9 3
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
}