You are here

SocialUserNameConstraintValidator.php in Open Social 10.3.x

File

modules/social_features/social_user/src/Plugin/Validation/Constraint/SocialUserNameConstraintValidator.php
View source
<?php

namespace Drupal\social_user\Plugin\Validation\Constraint;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Validates the UserName constraint no email address allowed in the username.
 */
class SocialUserNameConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
  protected $typedDataManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(TypedDataManagerInterface $typed_data_manager) {
    $this->typedDataManager = $typed_data_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('typed_data_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    if (is_null($items)) {
      return;
    }
    if ($nameField = $items
      ->first()) {
      $name = $nameField
        ->__get('value');
      $definition = DataDefinition::create('string')
        ->setConstraints([
        'Email' => [],
      ]);
      $typed_data = $this->typedDataManager
        ->create($definition, $name);
      $violations = $typed_data
        ->validate();
      if (count($violations) == 0) {
        $this->context
          ->addViolation($constraint->usernameIsEmailMessage);
      }
    }
  }

}

Classes

Namesort descending Description
SocialUserNameConstraintValidator Validates the UserName constraint no email address allowed in the username.