TalismanFR /
psb-api
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace talismanfr\psbbank\shared; |
||
| 5 | |||
| 6 | |||
| 7 | use InvalidArgumentException; |
||
| 8 | |||
| 9 | class EmailValue |
||
| 10 | { |
||
| 11 | private $userName; |
||
| 12 | private $domain; |
||
| 13 | |||
| 14 | public function __construct(string $emailAddress) |
||
| 15 | { |
||
| 16 | $addressParts = explode('@', $emailAddress); |
||
| 17 | |||
| 18 | if (!is_array($addressParts) || count($addressParts) !== 2) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 19 | throw new InvalidArgumentException('Given email address could not be parsed'); |
||
| 20 | } |
||
| 21 | |||
| 22 | $this->userName = $addressParts[0]; |
||
| 23 | $this->domain = $addressParts[1]; |
||
| 24 | |||
| 25 | if (trim($this->domain) === '') { |
||
| 26 | throw new InvalidArgumentException('Email domain cannot be empty'); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | public function getUserName(): string |
||
| 31 | { |
||
| 32 | return $this->userName; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function getDomain(): string |
||
| 36 | { |
||
| 37 | return $this->domain; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getNormalizedDomain(): string |
||
| 41 | { |
||
| 42 | return (string)idn_to_ascii($this->domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getFullAddress(): string |
||
| 46 | { |
||
| 47 | return $this->userName . '@' . $this->domain; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getNormalizedAddress(): string |
||
| 51 | { |
||
| 52 | return $this->userName . '@' . $this->getNormalizedDomain(); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function __toString(): string |
||
| 56 | { |
||
| 57 | return $this->getFullAddress(); |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | } |