|
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
|
|
|
/** |
|
21
|
|
|
* @var mixed[] DEFAULT_CONFIG The default base configuration. |
|
22
|
|
|
*/ |
|
23
|
|
|
protected const DEFAULT_CONFIG = [ |
|
24
|
|
|
'interface' => false, |
|
25
|
|
|
'private' => true, |
|
26
|
|
|
'auto' => false, |
|
27
|
|
|
'phpdoc' => [] |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array $config The configuration as an array. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ArrayConfig constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $config The config array to use. |
|
39
|
|
|
* |
|
40
|
|
|
* @throws InvalidConfigException If the $config is invalid. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($config = BaseConfig::DEFAULT_CONFIG) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->validate($config); |
|
45
|
|
|
|
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Validate a configuration to know if it can be used to construct an instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $config The configuration to validate. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws InvalidConfigException If the $config is invalid. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function validate($config): void |
|
57
|
|
|
{ |
|
58
|
|
|
// Check that $config is an array |
|
59
|
|
|
if (! Validator::arrayType()->validate($config)) { |
|
60
|
|
|
throw new InvalidConfigException('The config must be an array.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Check boolean parameters |
|
64
|
|
|
if (! Validator::key('interface', Validator::boolType())->validate($config)) { |
|
65
|
|
|
throw new InvalidConfigException('"interface" parameter must be set as a boolean.'); |
|
66
|
|
|
} |
|
67
|
|
|
if (! Validator::key('private', Validator::boolType())->validate($config)) { |
|
68
|
|
|
throw new InvalidConfigException('"private" parameter must be set as a boolean.'); |
|
69
|
|
|
} |
|
70
|
|
|
if (! Validator::key('auto', Validator::boolType())->validate($config)) { |
|
71
|
|
|
throw new InvalidConfigException('"auto" parameter must be set as a boolean.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->validatePhpdoc($config); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Validate the phpdoc key in the config array. |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $config The configuration. |
|
81
|
|
|
* |
|
82
|
|
|
* @throws InvalidConfigException If the phpdoc is invalid (source or target). |
|
83
|
|
|
*/ |
|
84
|
|
|
private function validatePhpdoc($config): void |
|
85
|
|
|
{ |
|
86
|
|
|
// Check that dirs key exists |
|
87
|
|
|
if (! Validator::key('phpdoc', Validator::arrayType())->validate($config)) { |
|
88
|
|
|
throw new InvalidConfigException('"phpdoc" parameter is not an array.'); |
|
89
|
|
|
} |
|
90
|
|
|
// Validate each phpdoc |
|
91
|
|
|
if (! Validator::arrayVal() |
|
92
|
|
|
->each(Validator::stringType(), Validator::stringType())->validate($config['phpdoc']) |
|
93
|
|
|
) { |
|
94
|
|
|
throw new InvalidConfigException('Some annotation in "phpdoc" parameter are not strings.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function hasInterfaceParsing(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->config['interface']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function hasPrivateParsing(): bool |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->config['private']; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function hasAuto(): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->config['auto']; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getTemplatesPath(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return realpath(__DIR__ . '/../../template'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getPhpDoc(): array |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->config['phpdoc']; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|