|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
|
6
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface; |
|
7
|
|
|
use PhpUnitGen\Exception\FileNotFoundException; |
|
8
|
|
|
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface; |
|
9
|
|
|
use Respect\Validation\Validator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class FileValidator. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
15
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
17
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
18
|
|
|
* @since Class available since Release 2.0.0. |
|
19
|
|
|
*/ |
|
20
|
|
|
class FileValidator implements FileValidatorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConsoleConfigInterface $config The configuration to use. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FilesystemInterface $fileSystem The file system to use. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fileSystem; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* FileValidator constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ConsoleConfigInterface $config The config to use. |
|
36
|
|
|
* @param FilesystemInterface $filesystem The file system to use. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ConsoleConfigInterface $config, FilesystemInterface $filesystem) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->config = $config; |
|
41
|
|
|
$this->fileSystem = $filesystem; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function validate(string $path): bool |
|
48
|
|
|
{ |
|
49
|
|
|
if (! $this->validatePath($path)) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
if (! $this->validateIncludeRegex($path) |
|
53
|
|
|
|| ! $this->validateExcludeRegex($path) |
|
54
|
|
|
) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Validate file has a valid path. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $path The file path. |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool True if the path exists and is a file. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws FileNotFoundException If the path corresponds to a dir. |
|
69
|
|
|
*/ |
|
70
|
|
|
private function validatePath(string $path): bool |
|
71
|
|
|
{ |
|
72
|
|
|
if (! $this->fileSystem->has($path)) { |
|
73
|
|
|
throw new FileNotFoundException(sprintf('The source file "%s" does not exist.', $path)); |
|
74
|
|
|
} |
|
75
|
|
|
if ($this->fileSystem->get($path)->getType() === 'dir') { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Validate file has the include regex if this regex is set. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $path The file path. |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool True if it pass this validation. |
|
87
|
|
|
*/ |
|
88
|
|
|
private function validateIncludeRegex(string $path): bool |
|
89
|
|
|
{ |
|
90
|
|
|
$includeRegex = $this->config->getIncludeRegex(); |
|
91
|
|
|
return $includeRegex === null || Validator::regex($includeRegex)->validate($path) === true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Validate file has not the exclude regex if this regex is set. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path The file path. |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool True if it pass this validation. |
|
100
|
|
|
*/ |
|
101
|
|
|
private function validateExcludeRegex(string $path): bool |
|
102
|
|
|
{ |
|
103
|
|
|
$excludeRegex = $this->config->getExcludeRegex(); |
|
104
|
|
|
return $excludeRegex === null || Validator::regex($excludeRegex)->validate($path) !== true; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|