ConfigArrayBuilder::mergeOverByConfigs()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\SymfonyInteropContainer;
4
5
class ConfigArrayBuilder
6
{
7
	private $configs = [];
8
	private $key;
9
10 8
	private function __construct(array $configs, string $key)
11
	{
12 8
		$this->key = $key;
13 8
		$this->mergeOverByConfigs($configs);
14 8
	}
15
16 8
	public function mergeOverByConfigs(array $configs): self
17
	{
18 8
		foreach ($configs as $config) {
19 8
			$this->mergeOverByConfig($config);
20
		}
21
22 8
		return $this;
23
	}
24
25 8
	public function mergeOverByConfig(array $config): self
26
	{
27 8
		$this->configs[] = $config;
28
29 8
		return $this;
30
	}
31
32 8
	public static function withConfigsOnKey(array $configs = [], string $key): self
33
	{
34 8
		return new self($configs, $key);
35
	}
36
37 8
	public function mergeDefaultConfig(array $config): self
38
	{
39 8
		array_unshift($this->configs, $config);
40
41 8
		return $this;
42
	}
43
44 8
	public function build(): array
45
	{
46 8
		$builtConfig = [];
47 8
		foreach ($this->configs as $overridingConfig) {
48 8
			$builtConfig = $this->mergeConfigs($overridingConfig, $builtConfig);
49
		}
50
51 8
		return $builtConfig;
52
	}
53
54 8
	private function mergeConfigs(array $overridingConfig, array $overriddenConfig): array
55
	{
56 8
		return array_replace_recursive($overriddenConfig, $overridingConfig);
57
	}
58
59 8
	public function getKey(): string
60
	{
61 8
		return $this->key;
62
	}
63
64 8
	public function setKey(string $key)
65
	{
66 8
		$this->key = $key;
67 8
	}
68
}
69