Changeset View
Changeset View
Standalone View
Standalone View
src/parser/PhutilEmailAddress.php
<?php | <?php | ||||
/** | /** | ||||
* Basic email address parser. This parser is very liberal and does not attempt | * Basic email address parser. This parser is very liberal and does not attempt | ||||
* to be fully RFC-compliant, because trying to do so is a crazy mess. However, | * to be fully RFC-compliant, because trying to do so is a crazy mess. However, | ||||
* it should parse all reasonable addresses which are actually in use on the | * it should parse all reasonable addresses which are actually in use on the | ||||
* internet today. | * internet today. | ||||
*/ | */ | ||||
final class PhutilEmailAddress extends Phobject { | final class PhutilEmailAddress extends Phobject { | ||||
private $displayName; | private $displayName; | ||||
private $localPart; | private $localPart; | ||||
private $domainName; | private $domainName; | ||||
public function __construct($email_address = null) { | public function __construct($email_address = null) { | ||||
$email_address = phutil_string_cast($email_address); | |||||
$email_address = trim($email_address); | $email_address = trim($email_address); | ||||
$matches = null; | $matches = null; | ||||
if (preg_match('/^(.*)<(.*?)>$/', $email_address, $matches)) { | if (preg_match('/^(.*)<(.*?)>$/', $email_address, $matches)) { | ||||
$display_name = trim($matches[1], '\'" '); | $display_name = trim($matches[1], '\'" '); | ||||
if (strpos($matches[2], '@') !== false) { | if (strpos($matches[2], '@') !== false) { | ||||
list($local_part, $domain_name) = explode('@', $matches[2], 2); | list($local_part, $domain_name) = explode('@', $matches[2], 2); | ||||
} else { | } else { | ||||
Show All 12 Lines | public function __construct($email_address = null) { | ||||
$this->displayName = $display_name; | $this->displayName = $display_name; | ||||
$this->localPart = $local_part; | $this->localPart = $local_part; | ||||
$this->domainName = $domain_name; | $this->domainName = $domain_name; | ||||
} | } | ||||
public function __toString() { | public function __toString() { | ||||
$address = $this->getAddress(); | $address = $this->getAddress(); | ||||
if (strlen($this->displayName)) { | |||||
if (phutil_nonempty_string($this->displayName)) { | |||||
$display_name = $this->encodeDisplayName($this->displayName); | $display_name = $this->encodeDisplayName($this->displayName); | ||||
return $display_name.' <'.$address.'>'; | return $display_name.' <'.$address.'>'; | ||||
} else { | |||||
return $address; | |||||
} | } | ||||
return $address; | |||||
} | } | ||||
public function setDisplayName($display_name) { | public function setDisplayName($display_name) { | ||||
$this->displayName = $display_name; | $this->displayName = $display_name; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getDisplayName() { | public function getDisplayName() { | ||||
Show All 26 Lines | if (isset($parts[1])) { | ||||
$this->domainName = $parts[1]; | $this->domainName = $parts[1]; | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getAddress() { | public function getAddress() { | ||||
$address = $this->localPart; | $address = $this->localPart; | ||||
if (strlen($this->domainName)) { | if ($this->domainName !== null && strlen($this->domainName)) { | ||||
$address .= '@'.$this->domainName; | $address .= '@'.$this->domainName; | ||||
} | } | ||||
return $address; | return $address; | ||||
} | } | ||||
private function encodeDisplayName($name) { | private function encodeDisplayName($name) { | ||||
// NOTE: This is a reasonable effort based on a cursory reading of | // NOTE: This is a reasonable effort based on a cursory reading of | ||||
// RFC2822, but may be significantly misguided. | // RFC2822, but may be significantly misguided. | ||||
Show All 14 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0