Completed
Pull Request — master (#74)
by Marcel
13:40
created

Configuration::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
namespace PHPSemVerChecker\Configuration;
4
5
use Noodlehaus\Config;
6
use PHPSemVerChecker\SemanticVersioning\Level;
7
8
class Configuration
9
{
10
	/**
11
	 * @var array
12
	 */
13
	protected $mapping = [];
14
	/**
15
	 * @var \Noodlehaus\Config
16
	 */
17
	protected $config;
18
19
	/**
20
	 * @param string|array $file
21
	 * @return \PHPSemVerChecker\Configuration\Configuration
22
	 */
23
	public static function fromFile($file)
24
	{
25
		$configuration = new Configuration();
26
		$config = new Config($file);
27
28
		$configuration->extractMapping($config->get('level.mapping', []));
29
30
		return $configuration;
31
	}
32
33
	/**
34
	 * @return \PHPSemVerChecker\Configuration\Configuration
35
	 */
36
	public static function defaults()
37
	{
38
		return self::fromFile(['?php-semver-checker.yml.dist', '?php-semver-checker.yml']);
39
	}
40
41
	/**
42
	 * @param array $mapping
43
	 */
44
	protected function extractMapping(array $mapping)
45
	{
46
		foreach ($mapping as &$level) {
47
			$level = Level::fromString($level);
48
		}
49
		$this->mapping = $mapping;
50
	}
51
52
	/**
53
	 * @return array
54
	 */
55
	public function getLevelMapping()
56
	{
57
		return $this->mapping;
58
	}
59
60
	/**
61
	 * @see \Noodlehaus\Config::get
62
	 * @param string $key
63
	 * @param mixed|null $default
64
	 * @return array|mixed|null
65
	 */
66
	public function get($key, $default = null)
67
	{
68
		return $this->config->get($key, $default);
69
	}
70
71
	/**
72
	 * @see \Noodlehaus\Config::set
73
	 * @param string $key
74
	 * @param mixed $value
75
	 */
76
	public function set($key, $value)
77
	{
78
		$this->config->set($key, $value);
79
	}
80
81
	/**
82
	 * Merge this configuration with an associative array.
83
	 *
84
	 * Note that dot notation is used for keys.
85
	 *
86
	 * @param array $data
87
	 */
88
	public function merge($data)
89
	{
90
		foreach ($data as $key => $value) {
91
			$this->set($key, $value);
92
		}
93
	}
94
}
95