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