Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Mime;
13
 
14
use Egulias\EmailValidator\EmailValidator;
15
use Egulias\EmailValidator\Validation\MessageIDValidation;
16
use Egulias\EmailValidator\Validation\RFCValidation;
17
use Symfony\Component\Mime\Encoder\IdnAddressEncoder;
18
use Symfony\Component\Mime\Exception\InvalidArgumentException;
19
use Symfony\Component\Mime\Exception\LogicException;
20
use Symfony\Component\Mime\Exception\RfcComplianceException;
21
 
22
/**
23
 * @author Fabien Potencier <fabien@symfony.com>
24
 */
25
final class Address
26
{
27
    /**
28
     * A regex that matches a structure like 'Name <email@address.com>'.
29
     * It matches anything between the first < and last > as email address.
30
     * This allows to use a single string to construct an Address, which can be convenient to use in
31
     * config, and allows to have more readable config.
32
     * This does not try to cover all edge cases for address.
33
     */
34
    private const FROM_STRING_PATTERN = '~(?<displayName>[^<]*)<(?<addrSpec>.*)>[^>]*~';
35
 
36
    private static EmailValidator $validator;
37
    private static IdnAddressEncoder $encoder;
38
 
39
    private string $address;
40
    private string $name;
41
 
42
    public function __construct(string $address, string $name = '')
43
    {
44
        if (!class_exists(EmailValidator::class)) {
45
            throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s"; try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
46
        }
47
 
48
        self::$validator ??= new EmailValidator();
49
 
50
        $this->address = trim($address);
51
        $this->name = trim(str_replace(["\n", "\r"], '', $name));
52
 
53
        if (!self::$validator->isValid($this->address, class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation())) {
54
            throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
55
        }
56
    }
57
 
58
    public function getAddress(): string
59
    {
60
        return $this->address;
61
    }
62
 
63
    public function getName(): string
64
    {
65
        return $this->name;
66
    }
67
 
68
    public function getEncodedAddress(): string
69
    {
70
        self::$encoder ??= new IdnAddressEncoder();
71
 
72
        return self::$encoder->encodeString($this->address);
73
    }
74
 
75
    public function toString(): string
76
    {
77
        return ($n = $this->getEncodedName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
78
    }
79
 
80
    public function getEncodedName(): string
81
    {
82
        if ('' === $this->getName()) {
83
            return '';
84
        }
85
 
86
        return sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName()));
87
    }
88
 
89
    public static function create(self|string $address): self
90
    {
91
        if ($address instanceof self) {
92
            return $address;
93
        }
94
 
95
        if (!str_contains($address, '<')) {
96
            return new self($address);
97
        }
98
 
99
        if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) {
100
            throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $address, self::class));
101
        }
102
 
103
        return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
104
    }
105
 
106
    /**
107
     * @param array<Address|string> $addresses
108
     *
109
     * @return Address[]
110
     */
111
    public static function createArray(array $addresses): array
112
    {
113
        $addrs = [];
114
        foreach ($addresses as $address) {
115
            $addrs[] = self::create($address);
116
        }
117
 
118
        return $addrs;
119
    }
120
}