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 $path |
21
|
|
|
*/ |
22
|
5 |
|
public function __construct($path) |
23
|
|
|
{ |
24
|
5 |
|
$this->config = new Config($path); |
25
|
5 |
|
$this->extractMapping($this->get('level.mapping', [])); |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string|array $file |
30
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
31
|
|
|
*/ |
32
|
|
|
public static function fromFile($file) |
33
|
|
|
{ |
34
|
|
|
return new Configuration($file); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
39
|
|
|
*/ |
40
|
|
|
public static function defaults() |
41
|
|
|
{ |
42
|
|
|
return self::fromFile(['?php-semver-checker.yml.dist', '?php-semver-checker.yml']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $mapping |
47
|
|
|
*/ |
48
|
5 |
|
protected function extractMapping(array $mapping) |
49
|
|
|
{ |
50
|
5 |
|
foreach ($mapping as &$level) { |
51
|
4 |
|
$level = Level::fromString($level); |
52
|
5 |
|
} |
53
|
5 |
|
$this->mapping = $mapping; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
1 |
|
public function getLevelMapping() |
60
|
|
|
{ |
61
|
1 |
|
return $this->mapping; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see \Noodlehaus\Config::get |
66
|
|
|
* @param string $key |
67
|
|
|
* @param mixed|null $default |
68
|
|
|
* @return array|mixed|null |
69
|
|
|
*/ |
70
|
5 |
|
public function get($key, $default = null) |
71
|
|
|
{ |
72
|
5 |
|
return $this->config->get($key, $default); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see \Noodlehaus\Config::set |
77
|
|
|
* @param string $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
*/ |
80
|
2 |
|
public function set($key, $value) |
81
|
|
|
{ |
82
|
2 |
|
$this->config->set($key, $value); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Merge this configuration with an associative array. |
87
|
|
|
* |
88
|
|
|
* Note that dot notation is used for keys. |
89
|
|
|
* |
90
|
|
|
* @param array $data |
91
|
|
|
*/ |
92
|
1 |
|
public function merge($data) |
93
|
|
|
{ |
94
|
1 |
|
foreach ($data as $key => $value) { |
95
|
1 |
|
$this->set($key, $value); |
96
|
1 |
|
} |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|