-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailAddressService.php
More file actions
62 lines (55 loc) · 1.75 KB
/
Copy pathEmailAddressService.php
File metadata and controls
62 lines (55 loc) · 1.75 KB
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
<?php
namespace Fab\Formule\Service;
/*
* This file is part of the Fab/Formule project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Fab\Formule\Exception\InvalidEmailFormatException;
use TYPO3\CMS\Core\SingletonInterface;
/**
* EmailAddressService
*/
class EmailAddressService implements SingletonInterface
{
/**
* @param string $listOfEmails
* @return array
*/
public function parse(string $listOfEmails): array
{
$emails = array();
if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $listOfEmails, $matches, PREG_SET_ORDER) > 0) {
foreach ($matches as $match) {
if (!empty($match[2])) {
$emails[trim($match[2], '<>')] = $match[1];
} else {
$emails[$match[1]] = $match[1];
}
}
}
return $emails;
}
/**
* Validate emails to be used in the SwiftMailer framework
*
* @throws InvalidEmailFormatException
* @param $emails
* @return boolean
*/
public function validate($emails): bool
{
foreach ($emails as $email => $name) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = sprintf('Email provided is not valid, given value "%s"', $email);
throw new InvalidEmailFormatException($message, 1350297165);
}
if (strlen($name) <= 0) {
$message = sprintf('Name should not be empty, given value "%s"', $name);
throw new InvalidEmailFormatException($message, 1350297170);
}
}
return true;
}
}