Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#6268-addresslist-fr…
Browse files Browse the repository at this point in the history
…om-string' into develop

Close zendframework/zendframework#6268
  • Loading branch information
Ocramius committed Jul 27, 2014
7 parents e366253 + 26177b0 + 2beabf9 + de855cb + d26cf74 + 82571c3 + 3d84996 commit d2a67fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
34 changes: 34 additions & 0 deletions src/AddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ public function addMany(array $addresses)
return $this;
}

/**
* Add an address to the list from any valid string format, such as
* - "ZF Dev" <[email protected]>
* - [email protected]
*
* @param string $address
* @throws Exception\InvalidArgumentException
* @return AddressList
*/
public function addFromString($address)
{
if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) {
throw new Exception\InvalidArgumentException('Invalid address format');
}

$name = null;
if (isset($matches['name'])) {
$name = trim($matches['name']);
}
if (empty($name)) {
$name = null;
}

if (isset($matches['namedEmail'])) {
$email = $matches['namedEmail'];
}
if (isset($matches['email'])) {
$email = $matches['email'];
}
$email = trim($email);

return $this->add($email, $name);
}

/**
* Merge another address list into this one
*
Expand Down
24 changes: 1 addition & 23 deletions src/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,7 @@ function (&$value) {

$addressList = $header->getAddressList();
foreach ($values as $address) {
// split values into name/email
if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) {
// Should we raise an exception here?
continue;
}
$name = null;
if (isset($matches['name'])) {
$name = trim($matches['name']);
}
if (empty($name)) {
$name = null;
}

if (isset($matches['namedEmail'])) {
$email = $matches['namedEmail'];
}
if (isset($matches['email'])) {
$email = $matches['email'];
}
$email = trim($email); // we may have leading whitespace

// populate address list
$addressList->add($email, $name);
$addressList->addFromString($address);
}
return $header;
}
Expand Down

0 comments on commit d2a67fc

Please sign in to comment.