Completed
Push — master ( 424983...a57ff3 )
by Greg
01:22
created

Config::setDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Consolidation\Config;
3
4
use Dflydev\DotAccessData\Data;
5
6
class Config implements ConfigInterface
7
{
8
    /**
9
     * @var Data
10
     */
11
    protected $config;
12
13
    /**
14
     * @var array
15
     */
16
    protected $defaults;
17
18
    /**
19
     * Create a new configuration object, and initialize it with
20
     * the provided nested array containing configuration data.
21
     */
22
    public function __construct(array $data = null)
23
    {
24
        $this->config = new Data($data);
25
        $this->defaults = [];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function has($key)
32
    {
33
        return ($this->config->has($key));
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function get($key, $defaultFallback = null)
40
    {
41
        if ($this->has($key)) {
42
            return $this->config->get($key);
43
        }
44
        return $this->getDefault($key, $defaultFallback);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function set($key, $value)
51
    {
52
        $this->config->set($key, $value);
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function import($data)
60
    {
61
        $this->config = new Data($data);
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function combine($data)
69
    {
70
        if (!empty($data)) {
71
            $this->config->import($data, true);
72
        }
73
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function export()
80
    {
81
        return $this->config->export();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function hasDefault($key)
88
    {
89
        return isset($this->defaults[$key]);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getDefault($key, $defaultFallback = null)
96
    {
97
        return $this->hasDefault($key) ? $this->defaults[$key] : $defaultFallback;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function setDefault($key, $value)
104
    {
105
        $this->defaults[$key] = $value;
106
        return $this;
107
    }
108
}
109