You are here

public function UniqueLabelInListConstraintValidator::validate in Drupal 10

Throws

\Symfony\Component\Validator\Exception\UnexpectedTypeException Thrown when the given constraint is not supported by this validator.

File

core/modules/ckeditor5/src/Plugin/Validation/Constraint/UniqueLabelInListConstraintValidator.php, line 24

Class

UniqueLabelInListConstraintValidator
Uniquely labeled list item constraint validator.

Namespace

Drupal\ckeditor5\Plugin\Validation\Constraint

Code

public function validate($list, Constraint $constraint) {
  if (!$constraint instanceof UniqueLabelInListConstraint) {
    throw new UnexpectedTypeException($constraint, UniqueLabelInListConstraint::class);
  }
  $labels = array_column($list, $constraint->labelKey);
  $label_frequencies = array_count_values($labels);
  foreach ($label_frequencies as $label => $frequency) {
    if ($frequency > 1) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('%label', $label)
        ->addViolation();
    }
  }
}