1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Schema\Validators; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Regex; |
14
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
15
|
|
|
use Symfony\Component\Validator\Validation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @Annotation |
19
|
|
|
*/ |
20
|
|
|
class PropertyNameValidator |
21
|
|
|
{ |
22
|
|
|
private int $minLength = 1; |
23
|
|
|
|
24
|
|
|
private int $maxLength = 64; |
25
|
|
|
|
26
|
513 |
|
public function validate(string $name): ConstraintViolationListInterface |
27
|
|
|
{ |
28
|
513 |
|
$validator = Validation::createValidator(); |
29
|
|
|
|
30
|
513 |
|
return $validator->validate($name, [ |
31
|
513 |
|
new Length([ |
32
|
513 |
|
'min' => $this->minLength, |
33
|
513 |
|
'max' => $this->maxLength, |
34
|
|
|
'allowEmptyString' => false, |
35
|
513 |
|
'minMessage' => 'must be least {{ limit }} characters long.', |
36
|
513 |
|
'maxMessage' => 'must be max {{ limit }} characters.', |
37
|
|
|
]), |
38
|
513 |
|
new Regex([ |
39
|
513 |
|
'pattern' => '/^(?=[^_0-9].*)\w*[^_\W]$/', |
40
|
|
|
'message' => 'is not valid. Only accept letters, numbers and underscore (except at beginning nor end).', |
41
|
|
|
]), |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|