summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/user/src/UserNameValidator.php
blob: 03a6cbcd4fdf569fbb55d3d7fb1ff52bdb203811 (plain) (blame)
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
<?php

declare(strict_types=1);

namespace Drupal\user;

use Drupal\Core\Validation\BasicRecursiveValidatorFactory;
use Drupal\Core\Validation\ConstraintManager;
use Symfony\Component\Validator\ConstraintViolationListInterface;

/**
 * Provides a username validator.
 *
 * This validator re-uses the UserName constraint plugin but does not require a
 * User entity.
 */
class UserNameValidator {

  public function __construct(
    protected readonly BasicRecursiveValidatorFactory $validatorFactory,
    protected readonly ConstraintManager $constraintManager,
  ) {}

  /**
   * Validates a user name.
   *
   * @return \Symfony\Component\Validator\ConstraintViolationListInterface
   *   The list of constraint violations.
   */
  public function validateName(string $name): ConstraintViolationListInterface {
    $validator = $this->validatorFactory->createValidator();
    $constraint = $this->constraintManager->create('UserName', []);
    return $validator->validate($name, $constraint);
  }

}