EmailValidator example

private static IdnAddressEncoder $encoder;

    private string $address;
    private string $name;

    public function __construct(string $address, string $name = '')
    {
        if (!class_exists(EmailValidator::class)) {
            throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s". Try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
        }

        self::$validator ??= new EmailValidator();

        $this->address = trim($address);
        $this->name = trim(str_replace(["\n", "\r"], '', $name));

        if (!self::$validator->isValid($this->address, class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation())) {
            throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
        }
    }

    public function getAddress(): string
    {
        
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/** * @group dns-sensitive */
class EmailValidatorTest extends ConstraintValidatorTestCase
{
    use ExpectDeprecationTrait;

    protected function createValidator(): EmailValidator
    {
        return new EmailValidator(Email::VALIDATION_MODE_HTML5);
    }

    public function testUnknownDefaultModeTriggerException()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The "defaultMode" parameter value is not valid.');
        new EmailValidator('Unknown Mode');
    }

    public function testNullIsValid()
    {
        
private static IdnAddressEncoder $encoder;

    private string $address;
    private string $name;

    public function __construct(string $address, string $name = '')
    {
        if (!class_exists(EmailValidator::class)) {
            throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s". Try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
        }

        self::$validator ??= new EmailValidator();

        $this->address = trim($address);
        $this->name = trim(str_replace(["\n", "\r"], '', $name));

        if (!self::$validator->isValid($this->address, class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation())) {
            throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
        }
    }

    public function getAddress(): string
    {
        

class EmailValidatorTest extends TestCase {

  /** * @covers ::isValid */
  public function testIsValid() {
    // Note that \Drupal\Component\Utility\EmailValidator wraps     // \Egulias\EmailValidator\EmailValidator so we don't do anything more than     // test that the wrapping works since the dependency has its own test     // coverage.     $validator = new EmailValidator();
    $this->assertTrue($validator->isValid('example@example.com'));
    $this->assertFalse($validator->isValid('example@example.com@'));
    $this->assertFalse($validator->isValid('example@example .com'));
  }

  /** * @covers ::isValid */
  public function testIsValidException() {
    $validator = new EmailValidator();
    $this->expectException(\BadMethodCallException::class);
    
Home | Imprint | This part of the site doesn't use cookies.