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