-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigurableTrait.php
220 lines (187 loc) · 6.04 KB
/
ConfigurableTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* The Tale Config Utility Trait.
*
* Contains a trait that eases up configuration array handling
* for all tale framework components
*
* LICENSE:
* The code of this file is distributed under the MIT license.
* If you didn't receive a copy of the license text, you can
* read it here http://licenses.talesoft.io/2015/MIT.txt
*
* @category Utility
* @package tale-config
* @author Torben Koehn <tk@talesoft.io>
* @author Talesoft <info@talesoft.io>
* @copyright Copyright (c) 2015 Talesoft (http://talesoft.io)
* @license http://licenses.talesoft.io/2015/MIT.txt MIT License
* @version 1.0
* @link http://config.tale.talesoft.io/docs/files/ConfigurableTrait.html
* @since File available since Release 1.0
*/
namespace Tale;
use Tale\Config\FormatInterface;
/**
* Provides some utility methods to work with configuration arrays
*
* @package Tale\Jade\Util
*/
trait ConfigurableTrait
{
/**
* The options array.
*
* Keys are option names, values are option values
* @var array
*/
protected $options = [];
/**
* Sets the options initially providing default- and optional user options.
*
* @param array $defaults the default options
* @param array|null $userOptions the optional options passed by the user
* @param bool $recursive
*/
public function defineOptions(array $defaults, array $userOptions = null, $recursive = false)
{
$this->options = $defaults;
if ($userOptions)
$this->mergeOptions($userOptions, $recursive);
}
/**
* Returns the option array.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Merges the current options with another option array.
*
* The second parameter makes this recursive.
* The functions used are array_replace and array_replace_recursive
*
* Passing the third parameter reverses the merge, so you don't overwrite
* passed options with existing one, but rather set them only if they
* don't exist yet (defaulting)
*
* @param array $options the options to merge with
* @param bool $recursive should we merge recursively or not
* @param bool $reverse should values be prepended rather than appended
*
* @return $this
*/
public function mergeOptions(array $options, $recursive = false, $reverse = false)
{
$merge = 'array_replace';
if ($recursive)
$merge .= '_recursive';
$this->options = $reverse
? $merge($options, $this->options)
: $merge($this->options, $options);
return $this;
}
/**
* Returns a single option by its name.
*
* You can pass an optional default value (Default: null)
*
* @param string $name the name of the option to return
* @param mixed $defaultValue the default value if the option is not set (Default: null)
*
* @return mixed
*/
public function getOption($name, $defaultValue = null)
{
if (strstr($name, '.'))
return Config::get($name, $this->options, $defaultValue);
return isset($this->options[$name]) ? $this->options[$name] : $defaultValue;
}
/**
* Replaces with all options passed, if they are not set yet.
*
* This is an alias to ->setOptions with the third parameter set
* to true.
*
* @param array $defaults the array of default options
* @param bool|false $recursive should we merge recursively or not
*
* @return $this
*/
public function setOptions(array $defaults, $recursive = false)
{
return $this->mergeOptions($defaults, $recursive, true);
}
/**
* Replaces with all options passed, if they are not set yet.
*
* This is an alias to ->setOptions with the third parameter set
* to true.
*
* @param array $defaults the array of default options
* @param bool|false $recursive should we merge recursively or not
*
* @return $this
*/
public function setDefaults(array $defaults, $recursive = false)
{
return $this->mergeOptions($defaults, $recursive, true);
}
/**
* Sets a single option to the passed value.
*
* @param string $name the name of the option
* @param mixed $value the value of the option
*
* @return $this
*/
public function setOption($name, $value)
{
$this->options[$name] = $value;
return $this;
}
/**
* Forwards an option to an option array.
*
* e.g.
* options = [
* 'target' => [
* 'targetName' => null
* ],
*
* 'someOption' => 'someValue'
* ]
*
* options->forwardOption('someOption', 'target', 'targetName')
* will set ['target']['targetName'] to 'someValue'
*
* Notice that the third parameter can be omitted, it will
* be set to the same name as the first parameter then.
*
* @param string $name the name of the option to forward
* @param string $target the name of the option array to forward to
* @param string $targetName the name of the target option name inside the target array
*/
public function forwardOption($name, $target, $targetName = null)
{
$targetName = $targetName ? $targetName : $name;
if (isset($this->options[$name]))
$this->options[$target][$targetName] = $this->options[$name];
}
public function loadOptions($path, $optional = false, $recursive = false)
{
return $this->mergeOptions(Config::load($path, $optional), $recursive);
}
public function loadDefaults($path, $optional = false, $recursive = false)
{
return $this->setDefaults(Config::load($path, $optional), $recursive);
}
public function interpolateOptions(array &$source = null, $defaultValue = null)
{
Config::interpolateArray($this->options, $source, $defaultValue);
return $this;
}
}