Completed
Push — master ( b59a3b...c22cec )
by Greg
01:26
created

Config::setDefaults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Consolidation\Config;
4
5
use Dflydev\DotAccessData\Data;
6
7
class Config implements ConfigInterface
8
{
9
10
    /**
11
     * @var Data
12
     */
13
    protected $config;
14
15
    /**
16
     * TODO: make this private in 2.0 to prevent being saved as an array
17
     *   Making private now breaks backward compatibility
18
     *
19
     * @var Data
20
     */
21
    protected $defaults;
22
23
    /**
24
     * Create a new configuration object, and initialize it with
25
     * the provided nested array containing configuration data.
26
     *
27
     * @param array $data - Config data to store
28
     */
29
    public function __construct(array $data = null)
30
    {
31
        $this->config = new Data($data);
32
        $this->setDefaults(new Data());
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function has($key)
39
    {
40
        return ($this->config->has($key));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function get($key, $defaultFallback = null)
47
    {
48
        if ($this->has($key)) {
49
            return $this->config->get($key);
50
        }
51
        return $this->getDefault($key, $defaultFallback);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function set($key, $value)
58
    {
59
        $this->config->set($key, $value);
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function import($data)
67
    {
68
        return $this->replace($data);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function replace($data)
75
    {
76
        $this->config = new Data($data);
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function combine($data)
84
    {
85
        if (!empty($data)) {
86
            $this->config->import($data, true);
87
        }
88
        return $this;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function export()
95
    {
96
        return $this->config->export();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function hasDefault($key)
103
    {
104
        return $this->getDefaults()->has($key);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getDefault($key, $defaultFallback = null)
111
    {
112
        return $this->hasDefault($key) ? $this->getDefaults()->get($key) : $defaultFallback;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function setDefault($key, $value)
119
    {
120
        $this->getDefaults()->set($key, $value);
121
        return $this;
122
    }
123
124
    /**
125
     * Return the class $defaults property and ensure it's a Data object
126
     * TODO: remove Data object validation in 2.0
127
     *
128
     * @return Data
129
     */
130
    protected function getDefaults()
131
    {
132
        // Ensure $this->defaults is a Data object (not an array)
133
        if (!$this->defaults instanceof Data) {
134
            $this->setDefaults($this->defaults);
135
        }
136
        return $this->defaults;
137
    }
138
139
    /**
140
     * Sets the $defaults class parameter
141
     * TODO: remove support for array in 2.0 as this would currently break backward compatibility
142
     *
143
     * @param Data|array $defaults
144
     *
145
     * @throws \Exception
146
     */
147
    protected function setDefaults($defaults)
148
    {
149
        if (is_array($defaults)) {
150
            $this->defaults = new Data($defaults);
151
        } elseif ($defaults instanceof Data) {
152
            $this->defaults = $defaults;
153
        } else {
154
            throw new \Exception("Unknown type provided for \$defaults");
155
        }
156
    }
157
}
158