Skip to content

Commit c89d9b1

Browse files
Release v1.7.0
1 parent 4e31ecd commit c89d9b1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace BaseFrame\Exception\Domain;
4+
5+
use BaseFrame\Exception\DomainException;
6+
7+
/**
8+
* Неверный адрес электронной почты
9+
*/
10+
class InvalidMail extends DomainException {
11+
12+
}

src/System/Mail.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace BaseFrame\System;
4+
5+
use BaseFrame\Exception\Domain\InvalidMail;
6+
7+
/**
8+
* Класс для работы с электронными почтовыми адресами
9+
*/
10+
class Mail {
11+
12+
private string $_mail; // значение почты
13+
14+
/**
15+
* @param string $mail
16+
*
17+
* @throws InvalidMail
18+
*/
19+
public function __construct(string $mail) {
20+
21+
$this->_mail = $this->_validate($mail);
22+
}
23+
24+
/**
25+
* Валидируем почту
26+
*
27+
* @param string $mail
28+
*
29+
* @return string
30+
* @throws InvalidMail
31+
*/
32+
private function _validate(string $mail):string {
33+
34+
// нормализуем почту
35+
$mail = $this->_normalize($mail);
36+
37+
if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
38+
throw new InvalidMail("invalid mail");
39+
}
40+
41+
return $mail;
42+
}
43+
44+
/**
45+
* Приводим почту в нормальный вид
46+
*
47+
* @param string $mail
48+
*
49+
* @return string
50+
*/
51+
private function _normalize(string $mail):string {
52+
53+
return strtolower(trim($mail));
54+
}
55+
56+
/**
57+
* Вернуть нормализованную почту
58+
*
59+
* @return string
60+
*/
61+
public function mail():string {
62+
63+
return $this->_mail;
64+
}
65+
66+
/**
67+
* Вернуть часть с доменом из почты
68+
*
69+
* @return string
70+
*/
71+
public function getDomain():string {
72+
73+
return substr($this->_mail, strpos($this->_mail, "@") + 1);
74+
}
75+
}

0 commit comments

Comments
 (0)