Completed
Pull Request — master (#37)
by Chris
02:41
created

AbstractConfiguration::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace Darya\Foundation;
3
4
use ArrayAccess;
5
use Darya\Foundation\Configuration;
6
7
/**
8
 * Darya's abstract application configuration implementation (what a mouthful).
9
 * 
10
 * @author Chris Andrew <[email protected]>
11
 */
12
abstract class AbstractConfiguration implements ArrayAccess, Configuration
13
{
14
	/**
15
	 * The configuration data.
16
	 * 
17
	 * @var array
18
	 */
19
	protected $data = array();
20
	
21
	/**
22
	 * Determine whether an array has a value at the given dot-notated key.
23
	 * 
24
	 * @param array  $array
25
	 * @param string $key
26
	 * @return bool
27
	 */
28
	protected static function arrayHas(array $array, $key)
29
	{
30
		if (empty($array) || $key === null) {
31
			return false;
32
		}
33
		
34
		if (array_key_exists($key, $array)) {
35
			return true;
36
		}
37
		
38
		$parts = explode('.', $key);
39
		
40 View Code Duplication
		foreach ($parts as $part) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
			if (is_array($array) && array_key_exists($part, $array)) {
42
				$array = $array[$part];
43
			} else {
44
				return false;
45
			}
46
		}
47
		
48
		return true;
49
	}
50
	
51
	/**
52
	 * Retrieve a value from the given array using dot notation.
53
	 * 
54
	 * @param array  $array
55
	 * @param string $key
56
	 * @param mixed  $default [optional]
57
	 * @return mixed
58
	 */
59
	protected static function arrayGet(array $array, $key, $default = null)
60
	{
61
		if (empty($array) || $key === null) {
62
			return $default;
63
		}
64
		
65
		if (array_key_exists($key, $array)) {
66
			return $array[$key];
67
		}
68
		
69
		$parts = explode('.', $key);
70
		
71 View Code Duplication
		foreach ($parts as $part) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
			if (is_array($array) && array_key_exists($part, $array)) {
73
				$array = $array[$part];
74
			} else {
75
				return $default;
76
			}
77
		}
78
		
79
		return $array;
80
	}
81
	
82
	/**
83
	 * Set a value on the given array using dot notation.
84
	 * 
85
	 * @param array  $array
86
	 * @param string $key
87
	 * @param mixed  $value
88
	 */
89
	protected static function arraySet(array &$array, $key, $value)
90
	{
91
		if ($key === null) {
92
			return;
93
		}
94
		
95
		$parts = explode('.', $key);
96
		
97
		while (count($parts) > 1) {
98
			$part = array_shift($parts);
99
			
100
			if (!isset($array[$part]) || !is_array($array[$part])) {
101
				$array[$part] = array();
102
			}
103
			
104
			$array = &$array[$part];
105
		}
106
		
107
		$array[array_shift($parts)] = $value;
108
	}
109
	
110
	/**
111
	 * Determine whether a configuration value exists for the given key.
112
	 * 
113
	 * @param string $key
114
	 * @return bool
115
	 */
116
	public function has($key)
117
	{
118
		return static::arrayHas($this->data, $key);
119
	}
120
	
121
	/**
122
	 * Retrieve a configuration value.
123
	 * 
124
	 * @param string $key
125
	 * @param mixed  $default [optional]
126
	 * @return mixed
127
	 */
128
	public function get($key, $default = null)
129
	{
130
		return static::arrayGet($this->data, $key, $default);
131
	}
132
	
133
	/**
134
	 * Set a configuration value.
135
	 * 
136
	 * @param string $key
137
	 * @param mixed  $value
138
	 */
139
	public function set($key, $value)
140
	{
141
		static::arraySet($this->data, $key, $value);
142
	}
143
	
144
	/**
145
	 * Retrieve all of the configuration values.
146
	 * 
147
	 * @return array
148
	 */
149
	public function all()
150
	{
151
		return $this->data;
152
	}
153
	
154
	/**
155
	 * Determine whether a configuration value exists for the given offset.
156
	 * 
157
	 * @param mixed $offset
158
	 * @return bool
159
	 */
160
	public function offsetExists($offset)
161
	{
162
		return $this->has($offset);
163
	}
164
	
165
	/**
166
	 * Retrieve the configuration value at the given offset.
167
	 * 
168
	 * @param mixed $offset
169
	 * @return mixed
170
	 */
171
	public function offsetGet($offset)
172
	{
173
		return $this->get($offset);
174
	}
175
	
176
	/**
177
	 * Set a configuration value to the given offset.
178
	 * 
179
	 * @param mixed $offset
180
	 * @param mixed $value
181
	 */
182
	public function offsetSet($offset, $value)
183
	{
184
		$this->set($offset, $value);
185
	}
186
	
187
	/**
188
	 * Clear the given offset and its value.
189
	 * 
190
	 * @param mixed $offset
191
	 */
192
	public function offsetUnset($offset)
193
	{
194
		$this->set($offset, null);
195
	}
196
}
197