|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConfigInterface; |
|
6
|
|
|
use PhpUnitGen\Exception\InvalidConfigException; |
|
7
|
|
|
use Respect\Validation\Validator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class BaseConfig. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
13
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
15
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
16
|
|
|
* @since Class available since Release 2.0.0. |
|
17
|
|
|
*/ |
|
18
|
|
|
class BaseConfig implements ConfigInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const DEFAULT_CONFIG = [ |
|
21
|
|
|
'interface' => false, |
|
22
|
|
|
'auto' => true, |
|
23
|
|
|
'phpdoc' => [] |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array $config The configuration as an array. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $config; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ArrayConfig constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed $config The config array to use. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($config = BaseConfig::DEFAULT_CONFIG) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->validate($config); |
|
39
|
|
|
|
|
40
|
|
|
$this->config = $config; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Validate a configuration to know if it can be used to construct an instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $config The configuration to validate. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws InvalidConfigException If the $config is invalid. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function validate($config): void |
|
51
|
|
|
{ |
|
52
|
|
|
// Check that $config is an array |
|
53
|
|
|
if (! Validator::arrayType()->validate($config)) { |
|
54
|
|
|
throw new InvalidConfigException('The config must be an array.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Check boolean parameters |
|
58
|
|
|
if (! Validator::key('interface', Validator::boolType())->validate($config)) { |
|
59
|
|
|
throw new InvalidConfigException('"interface" parameter must be set as a boolean.'); |
|
60
|
|
|
} |
|
61
|
|
|
if (! Validator::key('auto', Validator::boolType())->validate($config)) { |
|
62
|
|
|
throw new InvalidConfigException('"auto" parameter must be set as a boolean.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->validatePhpdoc($config); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Validate the phpdoc key in the config array. |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $config The configuration. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidConfigException If the phpdoc is invalid (source or target). |
|
74
|
|
|
*/ |
|
75
|
|
|
private function validatePhpdoc($config): void |
|
76
|
|
|
{ |
|
77
|
|
|
// Check that dirs key exists |
|
78
|
|
|
if (! Validator::key('phpdoc', Validator::arrayType())->validate($config)) { |
|
79
|
|
|
throw new InvalidConfigException('"phpdoc" parameter is not an array.'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function hasInterfaceParsing(): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->config['interface']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function hasAuto(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->config['auto']; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getPhpDoc(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->config['phpdoc']; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|