9e4776bdcaf80fcc97baf1b25e8a5efd4f191116
[yaffs-website] / web / core / modules / user / src / Plugin / Validation / Constraint / UserNameConstraintValidator.php
1 <?php
2
3 namespace Drupal\user\Plugin\Validation\Constraint;
4
5 use Drupal\Component\Utility\Unicode;
6 use Symfony\Component\Validator\Constraint;
7 use Symfony\Component\Validator\ConstraintValidator;
8
9 /**
10  * Validates the UserName constraint.
11  */
12 class UserNameConstraintValidator extends ConstraintValidator {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function validate($items, Constraint $constraint) {
18     if (!isset($items) || !$items->value) {
19       $this->context->addViolation($constraint->emptyMessage);
20       return;
21     }
22     $name = $items->first()->value;
23     if (substr($name, 0, 1) == ' ') {
24       $this->context->addViolation($constraint->spaceBeginMessage);
25     }
26     if (substr($name, -1) == ' ') {
27       $this->context->addViolation($constraint->spaceEndMessage);
28     }
29     if (strpos($name, '  ') !== FALSE) {
30       $this->context->addViolation($constraint->multipleSpacesMessage);
31     }
32     if (preg_match('/[^\x{80}-\x{F7} a-z0-9@+_.\'-]/i', $name)
33       || preg_match(
34         // Non-printable ISO-8859-1 + NBSP
35         '/[\x{80}-\x{A0}' .
36         // Soft-hyphen
37         '\x{AD}' .
38         // Various space characters
39         '\x{2000}-\x{200F}' .
40         // Bidirectional text overrides
41         '\x{2028}-\x{202F}' .
42         // Various text hinting characters
43         '\x{205F}-\x{206F}' .
44         // Byte order mark
45         '\x{FEFF}' .
46         // Full-width latin
47         '\x{FF01}-\x{FF60}' .
48         // Replacement characters
49         '\x{FFF9}-\x{FFFD}' .
50         // NULL byte and control characters
51         '\x{0}-\x{1F}]/u',
52         $name)
53     ) {
54       $this->context->addViolation($constraint->illegalMessage);
55     }
56     if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
57       $this->context->addViolation($constraint->tooLongMessage, ['%name' => $name, '%max' => USERNAME_MAX_LENGTH]);
58     }
59   }
60
61 }