Passed
Push — master ( e4897b...79b76a )
by smiley
01:48
created

DotArray::arraySet()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 3
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait DotArray
4
 *
5
 * @filesource   DotArray.php
6
 * @created      13.11.2017
7
 * @package      chillerlan\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits;
14
15
/**
16
 * @link https://github.com/laravel/framework/blob/5.4/src/Illuminate/Support/Arr.php
17
 */
18
trait DotArray{
19
20
	/**
21
	 *  Checks if $key isset in $array using dot notation and returns it on success.
22
	 *
23
	 * @param array  $array   the array to search in
24
	 * @param string $key     the key to search
25
	 * @param mixed  $default [optional] a default value in case the key isn't being found
26
	 *
27
	 * @return mixed returns $array[$key], $default otherwise.
28
	 */
29
	protected function arrayGet(array $array, string $key, $default = null){
30
31
		if(isset($array[$key])){
32
			return $array[$key];
33
		}
34
35
		foreach(explode('.', $key) as $segment){
36
37
			if(!is_array($array) || !array_key_exists($segment, $array)){
38
				return $default;
39
			}
40
41
			$array = $array[$segment];
42
		}
43
44
		return $array;
45
	}
46
47
	/**
48
	 * Checks if $key exists in $array using dot notation and returns it on success
49
	 *
50
	 * @param array  $array the array to search in
51
	 * @param string $key   the key to search
52
	 *
53
	 * @return bool
54
	 */
55
	protected function arrayIn(array $array, string $key):bool{
56
57
		if(empty($array)){
58
			return false;
59
		}
60
61
		if(array_key_exists($key, $array)){
62
			return true;
63
		}
64
65
		foreach(explode('.', $key) as $segment){
66
67
			if(!is_array($array) || !array_key_exists($segment, $array)){
68
				return false;
69
			}
70
71
			$array = $array[$segment];
72
		}
73
74
		return true;
75
	}
76
77
	/**
78
	 * Sets $key in $array using dot notation
79
	 *
80
	 * If no key is given to the method, the entire array will be replaced.
81
	 *
82
	 * @param  array  $array
83
	 * @param  string $key
84
	 * @param  mixed  $value
85
	 *
86
	 * @return array
87
	 */
88
	protected function arraySet(array &$array, string $key, $value){
89
90
		if(is_null($key)){
91
			return $array = $value;
92
		}
93
94
		$keys = explode('.', $key);
95
96
		while(count($keys) > 1){
97
			$key = array_shift($keys);
98
99
			// If the key doesn't exist at this depth, we will just create an empty array
100
			// to hold the next value, allowing us to create the arrays to hold final
101
			// values at the correct depth. Then we'll keep digging into the array.
102
			if(!isset($array[$key]) || !is_array($array[$key])){
103
				$array[$key] = [];
104
			}
105
106
			$array = &$array[$key];
107
		}
108
109
		$array[array_shift($keys)] = $value;
110
111
		return $array;
112
	}
113
114
}
115