Soullivaneuh/IsoCodesValidator

View on GitHub
src/Constraints/IpValidator.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace SLLH\IsoCodesValidator\Constraints;

use IsoCodes;
use SLLH\IsoCodesValidator\AbstractIsoCodesConstraintValidator;
use Symfony\Component\Validator\Constraint;

/**
 * @author Sullivan Senechal <soullivaneuh@gmail.com>
 */
final class IpValidator extends AbstractIsoCodesConstraintValidator
{
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint)
    {
        parent::validate($value, $constraint);

        if ($value && !IsoCodes\IP::validate($value) && !IsoCodes\IP::validateIPV6($value)) {
            $this->createViolation($constraint->message);
        }
    }
}