Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

FilterInputVariable::filterValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Karma\Configuration;
4
5
trait FilterInputVariable
6
{
7
    private function filterValue($value)
8
    {
9
        if(is_array($value))
10
        {
11
            return array_map(function ($item) {
12
                return $this->filterOneValue($item);
13
            }, $value);
14
        }
15
        
16
        return $this->filterOneValue($value);
17
    }
18
    
19
    private function filterOneValue($value)
20
    {
21
        if(! is_string($value))
22
        {
23
            return $value;
24
        }
25
        
26
        $value = trim($value);
27
    
28
        $knowValues = array(
29
            'true' => true,
30
            'false' => false,
31
            'null' => null
32
        );
33
    
34
        if(array_key_exists(strtolower($value), $knowValues))
35
        {
36
            return $knowValues[strtolower($value)];
37
        }
38
    
39
        if(is_numeric($value))
40
        {
41
            if(stripos($value, '.') !== false && floatval($value) == $value)
42
            {
43
                return floatval($value);
44
            }
45
    
46
            return intval($value);
47
        }
48
    
49
        return $value;
50
    }
51
52
    public function parseList($value)
53
    {
54
        $value = trim($value);
55
    
56
        if(preg_match('~^\[(?P<valueList>[^\[\]]*)\]$~', $value, $matches))
57
        {
58
            $value = array_map('trim', explode(',', $matches['valueList']));
59
            $value = $this->removeEmptyEntries($value);
60
        }
61
    
62
        return $value;
63
    }
64
    
65
    private function removeEmptyEntries(array $list)
66
    {
67
        return array_merge(array_filter($list, function($item) {
68
            return empty($item) === false;
69
        }));
70
    }
71
}