class UniqueLabelInListConstraintValidator

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/Validation/Constraint/UniqueLabelInListConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\UniqueLabelInListConstraintValidator
  2. 11.x core/modules/ckeditor5/src/Plugin/Validation/Constraint/UniqueLabelInListConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\UniqueLabelInListConstraintValidator

Uniquely labeled list item constraint validator.

@internal

Hierarchy

Expanded class hierarchy of UniqueLabelInListConstraintValidator

File

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

Namespace

Drupal\ckeditor5\Plugin\Validation\Constraint
View source
class UniqueLabelInListConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     *
     * @throws \Symfony\Component\Validator\Exception\UnexpectedTypeException
     *   Thrown when the given constraint is not supported by this validator.
     */
    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();
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
UniqueLabelInListConstraintValidator::validate public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.