1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Configuration; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface; |
6
|
|
|
use PhpUnitGen\Exception\InvalidConfigException; |
7
|
|
|
use Respect\Validation\Validator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ConsoleConfig. |
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 ConsoleConfig extends BaseConfig implements ConsoleConfigInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected function validate($config): void |
24
|
|
|
{ |
25
|
|
|
parent::validate($config); |
26
|
|
|
|
27
|
|
|
$this->validateBooleans($config); |
28
|
|
|
$this->validateIncludeRegex($config); |
29
|
|
|
$this->validateExcludeRegex($config); |
30
|
|
|
$this->validateDirs($config); |
31
|
|
|
$this->validateFiles($config); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Validate all boolean attributes contained in configuration. |
36
|
|
|
* |
37
|
|
|
* @param mixed $config The configuration. |
38
|
|
|
* |
39
|
|
|
* @throws InvalidConfigException If a boolean attribute is invalid. |
40
|
|
|
*/ |
41
|
|
|
private function validateBooleans($config): void |
42
|
|
|
{ |
43
|
|
|
// Check boolean parameters |
44
|
|
|
if (! Validator::key('overwrite', Validator::boolType())->validate($config)) { |
45
|
|
|
throw new InvalidConfigException('"overwrite" parameter must be set as a boolean.'); |
46
|
|
|
} |
47
|
|
|
if (! Validator::key('ignore', Validator::boolType())->validate($config)) { |
48
|
|
|
throw new InvalidConfigException('"ignore" parameter must be set as a boolean.'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validate the include regex. |
54
|
|
|
* |
55
|
|
|
* @param mixed $config The configuration. |
56
|
|
|
* |
57
|
|
|
* @throws InvalidConfigException If a string attribute is invalid. |
58
|
|
|
*/ |
59
|
|
|
private function validateIncludeRegex($config): void |
60
|
|
|
{ |
61
|
|
|
if (! Validator::key('include', Validator::stringType())->validate($config) |
62
|
|
|
&& ! Validator::key('include', Validator::nullType())->validate($config) |
63
|
|
|
) { |
64
|
|
|
throw new InvalidConfigException('"include" parameter must be set as a string or a null value.'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate the exclude regex. |
70
|
|
|
* |
71
|
|
|
* @param mixed $config The configuration. |
72
|
|
|
* |
73
|
|
|
* @throws InvalidConfigException If a string attribute is invalid. |
74
|
|
|
*/ |
75
|
|
|
private function validateExcludeRegex($config): void |
76
|
|
|
{ |
77
|
|
|
if (! Validator::key('exclude', Validator::stringType())->validate($config) |
78
|
|
|
&& ! Validator::key('exclude', Validator::nullType())->validate($config) |
79
|
|
|
) { |
80
|
|
|
throw new InvalidConfigException('"exclude" parameter must be set as a string or a null value.'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Validate directories contained in configuration. |
86
|
|
|
* |
87
|
|
|
* @param mixed $config The configuration. |
88
|
|
|
* |
89
|
|
|
* @throws InvalidConfigException If a directory is invalid (source or target). |
90
|
|
|
*/ |
91
|
|
|
private function validateDirs($config): void |
92
|
|
|
{ |
93
|
|
|
// Check that dirs key exists |
94
|
|
|
if (! Validator::key('dirs', Validator::arrayType())->validate($config)) { |
95
|
|
|
throw new InvalidConfigException('"dirs" parameter is not an array.'); |
96
|
|
|
} |
97
|
|
|
// Validate each dirs |
98
|
|
|
if (! Validator::arrayVal() |
99
|
|
|
->each(Validator::stringType(), Validator::stringType())->validate($config['dirs']) |
100
|
|
|
) { |
101
|
|
|
throw new InvalidConfigException('Some directories in "dirs" parameter are not strings.'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Validate files contained in configuration. |
107
|
|
|
* |
108
|
|
|
* @param mixed $config The configuration. |
109
|
|
|
* |
110
|
|
|
* @throws InvalidConfigException If a file is invalid (source or target). |
111
|
|
|
*/ |
112
|
|
|
private function validateFiles($config): void |
113
|
|
|
{ |
114
|
|
|
// Check that files key exists |
115
|
|
|
if (! Validator::key('files', Validator::arrayType())->validate($config)) { |
116
|
|
|
throw new InvalidConfigException('"files" parameter is not an array.'); |
117
|
|
|
} |
118
|
|
|
// Validate each files |
119
|
|
|
if (! Validator::arrayVal() |
120
|
|
|
->each(Validator::stringType(), Validator::stringType())->validate($config['files']) |
121
|
|
|
) { |
122
|
|
|
throw new InvalidConfigException('Some files in "files" parameter are not strings.'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function hasOverwrite(): bool |
130
|
|
|
{ |
131
|
|
|
return $this->config['overwrite']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function hasIgnore(): bool |
138
|
|
|
{ |
139
|
|
|
return $this->config['ignore']; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getIncludeRegex(): string |
146
|
|
|
{ |
147
|
|
|
return $this->config['include']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getExcludeRegex(): string |
154
|
|
|
{ |
155
|
|
|
return $this->config['exclude']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getDirectories(): array |
162
|
|
|
{ |
163
|
|
|
return $this->config['dirs']; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function addDirectory(string $sourceDirectory, string $targetDirectory): void |
170
|
|
|
{ |
171
|
|
|
if (! Validator::arrayType()->validate($this->config['dirs'])) { |
172
|
|
|
$this->config['dirs'] = []; |
173
|
|
|
} |
174
|
|
|
$this->config['dirs'][$sourceDirectory] = $targetDirectory; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getFiles(): array |
181
|
|
|
{ |
182
|
|
|
return $this->config['files']; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function addFile(string $sourceFile, string $targetFile): void |
189
|
|
|
{ |
190
|
|
|
if (! Validator::arrayType()->validate($this->config['files'])) { |
191
|
|
|
$this->config['files'] = []; |
192
|
|
|
} |
193
|
|
|
$this->config['files'][$sourceFile] = $targetFile; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|