Completed
Push — master ( 63d3cc...fb2a6e )
by Peter
06:57
created

ArrayFiller::fill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
crap 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 08.03.18
6
 * Time: 17:12
7
 */
8
9
namespace Maslosoft\Manganel\Helpers;
10
11
12
use function array_key_exists;
13
14
class ArrayFiller
15
{
16
	/**
17
	 * Fill array by path. This will
18
	 * create array keys if not already set.
19
	 *
20
	 * @param $array
21
	 * @param $path
22
	 * @param $value
23
	 */
24 5
	public static function fill($array, $path, $value)
25
	{
26 5
		$parts = explode('.', $path);
27 5
		$key = array_shift($parts);
28 5
		if (empty($path))
29
		{
30 5
			return $value;
31
		}
32 5
		if (!array_key_exists($key, $array))
33
		{
34 5
			$array[$key] = self::fill($array, implode('.', $parts), $value);
35
		}
36 5
		return $array;
37
	}
38
}