UniqueLabelInListConstraintValidator.php in Drupal 10
File
core/modules/ckeditor5/src/Plugin/Validation/Constraint/UniqueLabelInListConstraintValidator.php
View source
<?php
declare (strict_types=1);
namespace Drupal\ckeditor5\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class UniqueLabelInListConstraintValidator extends ConstraintValidator {
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();
}
}
}
}