DotArray   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

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