php-dto/email-address

Immutable email object with validation and helpful methods

0.1.1 2022-08-04 01:24 UTC

This package is auto-updated.

Last update: 2024-11-04 06:17:57 UTC


README

Installation

composer require php-dto/email-address

Usage

<?php
use \PhpDto\EmailAddress\EmailAddress;
use \PhpDto\EmailAddress\Exception\InvalidEmailAddressException;

$email = new EmailAddress(' mail@example.com '); //spaces will be trimmed

echo $email->get();              //will print 'mail@example.com'
echo (string) $email;            //will print 'mail@example.com'

echo $email->getUsername();      //will print 'mail'
echo $email->getHostname();      //will print 'example.com'

echo json_encode([$email]);      //will print '["mail@example.com"]'


new EmailAddress('example.com'); //will throw InvalidEmailAddressException (validate by FILTER_VALIDATE_EMAIL)