When using the new symfony/mailer component, especially when you are migrating from SwiftMailer, it might be tedious to reorganize email addresses formatting. This library intends to help developpers with formatting them in the expected way.
$ composer require pandawan-technology/email-transformer
In order to make sure you only have one address, you can use the transformUnique
method as shown below:
<?php
use PandawanTechnology\EmailTransformer\EmailAddressTransformer;
class MyService
{
/**
* @var EmailAddressTransformer
*/
private $emailAddressTransformer;
public function __construct(EmailAddressTransformer $emailAddressTransformer)
{
$this->emailAddressTransformer = $emailAddressTransformer;
}
public function __invoke($address)
{
$address = $this->emailAddressTransformer->transformUnique($address);
// Will output an (Named)Address instance depending on the provided input
}
}
In order to format a collection of email addresses (or an unknown quantity), the transform
method is available as shown below:
<?php
use PandawanTechnology\EmailTransformer\EmailAddressTransformer;
class MyService
{
/**
* @var EmailAddressTransformer
*/
private $emailAddressTransformer;
public function __construct(EmailAddressTransformer $emailAddressTransformer)
{
$this->emailAddressTransformer = $emailAddressTransformer
}
public function __invoke($addresses)
{
$addresses = $this->emailAddressTransformer->transform($addresses);
// Will output an (Named)Address array instance(s) depending on the provided input
}
}
Those methods accept string(s), array of strings, (Named)Address instances, array of (Named)Address instances and mixed arrays. To get a better overview of supported inputs, you could find them in the EmailAddressTransformerTest.php
test class file.