Config::get()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 2
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebe\Pvc;
6
7
class Config
8
{
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    /**
15
     * Config constructor.
16
     * @param array $data
17
     */
18
    public function __construct(array $data = [])
19
    {
20
        $this->data = $data;
21
    }
22
23
    /**
24
     * Get value by using dot notation for nested arrays.
25
     *
26
     * @example $value = $this->get('foo.bar.baz');
27
     *
28
     * @param string $name
29
     * @param mixed $default
30
     * @return mixed
31
     */
32
    public function get(string $name, $default = null)
33
    {
34
        $path = explode('.', $name);
35
        $current = $this->data;
36
        foreach ($path as $field) {
37
            if (isset($current) && isset($current[$field])) {
38
                $current = $current[$field];
39
            } elseif (is_array($current) && isset($current[$field])) {
40
                $current = $current[$field];
41
            } else {
42
                return $default;
43
            }
44
        }
45
        return $current;
46
    }
47
}
48