EmailAddress

Value Object representing an email address

MIT License

Stars
7

EmailAddress

Why? start with why

According to Martin Fowler, a Value Object is:

A small simple object, like money or a date range, whose equality isn't based on identity

Kacper Gunia explains further:

The most important thing is that these objects reflect the language you talk to other developers - when you say Location everyone knows what it means.

Second thing is that VO can validate values passed and forbid to construct such object with incorrect data.

Third benefit is fact that you can rely on type - you know that if such VO was passed as an argument it will be always in valid state and you don't need to worry about that.

Also VO can contain some specialised methods that only make sense in context of this value and can be attached to this object (no need to create weird Util classes).

This package originated from a client project that handled email addresses for their users, but needed to store the local-part and domain in separate fields to the full address.

What?

This package contains a value object class that represents an email address.

Note that this is a dumb value object class. It performs no validation of email addresses. Use https://github.com/egulias/EmailValidator or similar to perform validation.

How?

Install

Add a dependency of gamajo/email-address to your project's composer.json file if you use Composer to manage the dependencies of your project.

composer require gamajo/email-address

Here is a minimal example of a composer.json file that just defines a dependency:

{
    "require": {
        "gamajo/email-address": "0.1.*"
    }
}

Usage Examples

Creating an EmailAddress object and accessing its parts

use Gamajo\EmailAddress\EmailAddress;

// Create EmailAddress object
$emailAddress = new EmailAddress('[email protected]');

// Access the EmailAddress object's local-part and domain
echo $emailAddress->getLocalPart() . PHP_EOL;
echo $emailAddress->getDomain() . PHP_EOL;
echo $emailAddress . PHP_EOL;

The code above produces the output shown below:

me
example.com
[email protected]

Changes

See the change log.

Contributing

See the contributing document.

Support

See the support document.

Licensing

The code in this project is licensed under MIT license.

Credits

Built by Gary Jones. Copyright 2015 Gamajo.