Dotor::get()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 37
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 37
rs 5.3846
cc 8
eloc 15
nc 6
nop 2
1
<?php
2
3
namespace Dotor;
4
5
/**
6
 * @author Pierre Klink <[email protected]>
7
 * @license BSD-2-Clause http://opensource.org/licenses/BSD-2-Clause See LICENSE-file in root directory
8
 */
9
class Dotor
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $params = [];
16
17
18
    /**
19
     * @param Loader|array $config
20
     */
21
    public function __construct($config)
22
    {
23
        if (is_array($config)) {
24
            $this->params = $config;
25
        } elseif ($config instanceof Loader) {
26
            $this->params = $config->get();
27
        }
28
    }
29
30
31
    /**
32
     * @param string|int|null $param
33
     * @param mixed $default
34
     * @return mixed
35
     * @throws \InvalidArgumentException
36
     */
37
    public function get($param = null, $default = null)
38
    {
39
        // $param has to be a string or integer
40
        if (!is_string($param) && !(is_int($param)) && $param !== null) {
41
            throw new \InvalidArgumentException('$param has to be a string, integer or null');
42
        }
43
44
        // if param is null get the param-array
45
        if ($param === null) {
46
            return $this->params;
47
        }
48
49
        $source   = $this->params;
50
51
        // split $param
52
        $segments = explode('.', $param);
53
54
        // returned value
55
        $value = '';
56
57
        // iterate splitted $param
58
        foreach ($segments as $index => $segment) {
59
            // if param doesn't exist return default value
60
            if (!isset($source[$segment])) {
61
                return $default;
62
            }
63
64
            // save value
65
            $value = $source[$segment];
66
67
            if ($index < count($segments)-1) {
68
                $source = $value;
69
            }
70
        }
71
72
        return $value;
73
    }
74
75
76
    /**
77
     * Alias for Dotot::getBoolean
78
     *
79
     * @param string $param
80
     * @param boolean $default
81
     * @return boolean
82
     */
83
    public function getBool($param, $default = true)
84
    {
85
        return $this->getBoolean($param, $default);
86
    }
87
88
89
    /**
90
     * @param string $param
91
     * @param boolean $default
92
     * @return boolean
93
     */
94
    public function getBoolean($param, $default = true)
95
    {
96
        $value = $this->get($param, $default);
97
98
        // check if $value is boolean
99
        if (is_bool($value)) {
100
            return $value;
101
        } elseif ($value === '0' || $value === 0) {
102
            return false;
103
        } elseif ($value === '1' || $value === 1) {
104
            return true;
105
        } elseif (is_bool($default)) {
106
            return $default;
107
        } else {
108
            return true;
109
        }
110
    }
111
112
113
    /**
114
     * @param string|int|null $param
115
     * @param mixed $default a scalar value
116
     * @return mixed a scalar value
117
     * @throws \InvalidArgumentException
118
     */
119
    public function getScalar($param = null, $default = '')
120
    {
121
        $value = $this->get($param, $default);
122
123
        // check if $value is scalar
124
        if (!is_scalar($value)) {
125
            // check if $default is scalar
126
            if (!is_scalar($default)) {
127
                throw new \InvalidArgumentException('$default has to be scalar');
128
            }
129
130
            return $default;
131
        } else {
132
            return $value;
133
        }
134
    }
135
136
137
    /**
138
     * @param string|int|null $param
139
     * @param array $default
140
     * @return array
141
     */
142
    public function getArray($param = null, array $default = [])
143
    {
144
        $value = $this->get($param, $default);
145
146
        // check if $value is an array
147
        if (!is_array($value)) {
148
            return $default;
149
        } else {
150
            return $value;
151
        }
152
    }
153
}
154
155