-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCNNameValidator.php
More file actions
29 lines (24 loc) · 864 Bytes
/
CNNameValidator.php
File metadata and controls
29 lines (24 loc) · 864 Bytes
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
<?php
namespace Thenbsp\ResourceBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CNNameValidator extends ConstraintValidator
{
const MIN = 2;
const MAX = 4;
public function validate($value, Constraint $constraint)
{
if (!preg_match("/^\p{Han}+$/u", $value)) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('%string%', $value)
->addViolation();
}
$strlen = mb_strlen($value);
if (($strlen < self::MIN) || ($strlen > self::MAX)) {
$this->context->buildViolation($constraint->lengthMessage)
->setParameter('%min%', self::MIN)
->setParameter('%max%', self::MAX)
->addViolation();
}
}
}