This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMailAddress.php
102 lines (84 loc) · 2.17 KB
/
PMailAddress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Poundation;
include_once 'PString.php';
class PMailAddress extends PObject implements \JsonSerializable {
private $user;
private $host;
function __construct($email = '') {
$components = PString::createFromString($email)->components('@');
switch ($components->count()) {
case 2:
$this->setUser($components[0]);
$this->setHost($components[1]);
break;
}
}
static function createFromString($mail) {
if (self::verifyAddress($mail)) {
$mail = new PMailAddress($mail);
return $mail;
} else {
return NULL;
}
}
/**
* Sets the user of the mail address (the part to the @).
* @param string $user
*/
public function setUser($user) {
$this->user = $user;
}
/**
* Returns the user of the mail address.
* @return string
*/
public function getUser() {
return $this->user;
}
/**
* Sets the host of the mail address (the part starting at the @).
* @param $host
*/
public function setHost($host) {
$this->host = $host;
}
/**
* Returns the host of the address.
* @return string
*/
public function getHost() {
return $this->host;
}
/**
* Verifies the mail address and returns true if it is valid.
* @return boolean
*/
public function verify() {
return self::verifyAddress($this->mailAddressFromComponents($this->getUser(), $this->getHost()));
}
/**
* Verifies a mail address and returns true if it is valid.
* @param string $mailAddress
* @return boolean
*/
static function verifyAddress($mailAddress) {
return (filter_var($mailAddress, FILTER_VALIDATE_EMAIL));
}
private function mailAddressFromComponents($user,$host) {
return $user . '@' . $host;
}
public function __toString() {
return $this->mailAddressFromComponents($this->getUser(), $this->getHost());
}
/**
* (PHP 5 >= 5.4.0)
* Serializes the object to a value that can be serialized natively by json_encode().
* @link http://docs.php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed Returns data which can be serialized by json_encode(), which is a value of any type other than a resource.
*/
function jsonSerialize()
{
return $this->__toString();
}
}
?>