forked from symfony/maker-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
248 lines (194 loc) · 8.15 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle;
use Doctrine\Common\Persistence\ManagerRegistry as LegacyManagerRegistry;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @author Javier Eguiluz <[email protected]>
* @author Ryan Weaver <[email protected]>
*
* @internal
*/
final class Validator
{
public static function validateClassName(string $className, string $errorMessage = ''): string
{
// remove potential opening slash so we don't match on it
$pieces = explode('\\', ltrim($className, '\\'));
$shortClassName = Str::getShortClassName($className);
$reservedKeywords = ['__halt_compiler', 'abstract', 'and', 'array',
'as', 'break', 'callable', 'case', 'catch', 'class',
'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval',
'exit', 'extends', 'final', 'finally', 'for', 'foreach', 'function',
'global', 'goto', 'if', 'implements', 'include',
'include_once', 'instanceof', 'insteadof', 'interface', 'isset',
'list', 'namespace', 'new', 'or', 'print', 'private',
'protected', 'public', 'require', 'require_once', 'return',
'static', 'switch', 'throw', 'trait', 'try', 'unset',
'use', 'var', 'while', 'xor', 'yield',
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void',
'iterable', 'object', '__file__', '__line__', '__dir__', '__function__', '__class__',
'__method__', '__namespace__', '__trait__', 'self', 'parent',
];
foreach ($pieces as $piece) {
if (!mb_check_encoding($piece, 'UTF-8')) {
$errorMessage = $errorMessage ?: sprintf('"%s" is not a UTF-8-encoded string.', $piece);
throw new RuntimeCommandException($errorMessage);
}
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) {
$errorMessage = $errorMessage ?: sprintf('"%s" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)', $className);
throw new RuntimeCommandException($errorMessage);
}
if (\in_array(strtolower($shortClassName), $reservedKeywords, true)) {
throw new RuntimeCommandException(sprintf('"%s" is a reserved keyword and thus cannot be used as class name in PHP.', $shortClassName));
}
}
// return original class name
return $className;
}
public static function notBlank(string $value = null): string
{
if (null === $value || '' === $value) {
throw new RuntimeCommandException('This value cannot be blank.');
}
return $value;
}
public static function validateLength($length)
{
if (!$length) {
return $length;
}
$result = filter_var($length, \FILTER_VALIDATE_INT, [
'options' => ['min_range' => 1],
]);
if (false === $result) {
throw new RuntimeCommandException(sprintf('Invalid length "%s".', $length));
}
return $result;
}
public static function validatePrecision($precision)
{
if (!$precision) {
return $precision;
}
$result = filter_var($precision, \FILTER_VALIDATE_INT, [
'options' => ['min_range' => 1, 'max_range' => 65],
]);
if (false === $result) {
throw new RuntimeCommandException(sprintf('Invalid precision "%s".', $precision));
}
return $result;
}
public static function validateScale($scale)
{
if (!$scale) {
return $scale;
}
$result = filter_var($scale, \FILTER_VALIDATE_INT, [
'options' => ['min_range' => 0, 'max_range' => 30],
]);
if (false === $result) {
throw new RuntimeCommandException(sprintf('Invalid scale "%s".', $scale));
}
return $result;
}
public static function validateBoolean($value)
{
if ('yes' == $value) {
return true;
}
if ('no' == $value) {
return false;
}
if (null === $valueAsBool = filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE)) {
throw new RuntimeCommandException(sprintf('Invalid bool value "%s".', $value));
}
return $valueAsBool;
}
public static function validatePropertyName(string $name): string
{
// check for valid PHP variable name
if (!Str::isValidPhpVariableName($name)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP property name.', $name));
}
return $name;
}
public static function validateDoctrineFieldName(string $name, ManagerRegistry|LegacyManagerRegistry $registry): string
{
// check reserved words
if ($registry->getConnection()->getDatabasePlatform()->getReservedKeywordsList()->isKeyword($name)) {
throw new \InvalidArgumentException(sprintf('Name "%s" is a reserved word.', $name));
}
self::validatePropertyName($name);
return $name;
}
public static function validateEmailAddress(?string $email): string
{
if (!filter_var($email, \FILTER_VALIDATE_EMAIL)) {
throw new RuntimeCommandException(sprintf('"%s" is not a valid email address.', $email));
}
return $email;
}
public static function existsOrNull(string $className = null, array $entities = []): ?string
{
if (null !== $className) {
self::validateClassName($className);
if (str_starts_with($className, '\\')) {
self::classExists($className);
} else {
self::entityExists($className, $entities);
}
}
return $className;
}
public static function classExists(string $className, string $errorMessage = ''): string
{
self::notBlank($className);
if (!class_exists($className)) {
$errorMessage = $errorMessage ?: sprintf('Class "%s" doesn\'t exist; please enter an existing full class name.', $className);
throw new RuntimeCommandException($errorMessage);
}
return $className;
}
public static function entityExists(string $className = null, array $entities = []): string
{
self::notBlank($className);
if (empty($entities)) {
throw new RuntimeCommandException('There are no registered entities; please create an entity before using this command.');
}
if (str_starts_with($className, '\\')) {
self::classExists($className, sprintf('Entity "%s" doesn\'t exist; please enter an existing one or create a new one.', $className));
}
if (!\in_array($className, $entities)) {
throw new RuntimeCommandException(sprintf('Entity "%s" doesn\'t exist; please enter an existing one or create a new one.', $className));
}
return $className;
}
public static function classDoesNotExist($className): string
{
self::notBlank($className);
if (class_exists($className)) {
throw new RuntimeCommandException(sprintf('Class "%s" already exists.', $className));
}
return $className;
}
public static function classIsUserInterface($userClassName): string
{
self::classExists($userClassName);
if (!isset(class_implements($userClassName)[UserInterface::class])) {
throw new RuntimeCommandException(sprintf('The class "%s" must implement "%s".', $userClassName, UserInterface::class));
}
return $userClassName;
}
}