phpmentoring/webapp

View on GitHub
src/Mentoring/Account/Validator/Constraints/TagConstraintValidator.php

Summary

Maintainability
A
25 mins
Test Coverage
<?php

namespace Mentoring\Account\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Mentoring\Taxonomy\Term;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

class TagConstraintValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        if (!is_array($value)) {
            throw new InvalidArgumentException('Array with terms expected but not found in TagConstraintValidator');
        }
        foreach ($value as $term) {
            if (!$term instanceof Term) {
                throw new InvalidArgumentException('Term expected but not found in TagConstraintValidator');
            }
            if (strlen($term->getName()) <= 0) {
                $this->context->addViolation(
                    $constraint->message
                );
            }
        }
    }
}