AddressHeader::getAddresses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser\Header;
9
10
use Psr\Log\LoggerInterface;
11
use ZBateson\MailMimeParser\Header\Consumer\AddressBaseConsumerService;
12
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart;
13
use ZBateson\MailMimeParser\Header\Part\AddressPart;
14
use ZBateson\MailMimeParser\MailMimeParser;
15
16
/**
17
 * A header containing one or more email addresses and/or groups of addresses.
18
 *
19
 * An address is separated by a comma, and each group separated by a semi-colon.
20
 * The AddressHeader provides a complete list of all addresses referenced in a
21
 * header including any addresses in groups, in addition to being able to access
22
 * the groups separately if needed.
23
 *
24
 * For full specifications, see {@link https://www.ietf.org/rfc/rfc2822.txt}
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class AddressHeader extends AbstractHeader
29
{
30
    /**
31
     * @var AddressPart[] array of addresses, included all addresses contained
32
     *      in groups.
33
     */
34
    protected array $addresses = [];
35
36
    /**
37
     * @var AddressGroupPart[] array of address groups (lists).
38
     */
39
    protected array $groups = [];
40
41 109
    public function __construct(
42
        string $name,
43
        string $value,
44
        ?LoggerInterface $logger = null,
45
        ?AddressBaseConsumerService $consumerService = null
46
    ) {
47 109
        $di = MailMimeParser::getGlobalContainer();
48 109
        parent::__construct(
49 109
            $logger ?? $di->get(LoggerInterface::class),
50 109
            $consumerService ?? $di->get(AddressBaseConsumerService::class),
51 109
            $name,
52 109
            $value
53 109
        );
54
    }
55
56
    /**
57
     * Filters $this->allParts into the parts required by $this->parts
58
     * and assignes it.
59
     *
60
     * The AbstractHeader::filterAndAssignToParts method filters out CommentParts.
61
     */
62 109
    protected function filterAndAssignToParts() : void
63
    {
64 109
        parent::filterAndAssignToParts();
65 109
        foreach ($this->parts as $part) {
66 108
            if ($part instanceof AddressPart) {
67 106
                $this->addresses[] = $part;
68 5
            } elseif ($part instanceof AddressGroupPart) {
69 5
                $this->addresses = \array_merge($this->addresses, $part->getAddresses());
70 5
                $this->groups[] = $part;
71
            }
72
        }
73
    }
74
75
    /**
76
     * Returns all address parts in the header including any addresses that are
77
     * in groups (lists).
78
     *
79
     * @return AddressPart[] The addresses.
80
     */
81 1
    public function getAddresses() : array
82
    {
83 1
        return $this->addresses;
84
    }
85
86
    /**
87
     * Returns all group parts (lists) in the header.
88
     *
89
     * @return AddressGroupPart[]
90
     */
91 1
    public function getGroups() : array
92
    {
93 1
        return $this->groups;
94
    }
95
96
    /**
97
     * Returns true if an address exists with the passed email address.
98
     *
99
     * Comparison is done case insensitively.
100
     *
101
     */
102 1
    public function hasAddress(string $email) : bool
103
    {
104 1
        foreach ($this->addresses as $addr) {
105 1
            if (\strcasecmp($addr->getEmail(), $email) === 0) {
106 1
                return true;
107
            }
108
        }
109 1
        return false;
110
    }
111
112
    /**
113
     * Returns the first email address in the header.
114
     *
115
     * @return ?string The email address
116
     */
117 1
    public function getEmail() : ?string
118
    {
119 1
        if (!empty($this->addresses)) {
120 1
            return $this->addresses[0]->getEmail();
121
        }
122
        return null;
123
    }
124
125
    /**
126
     * Returns the name associated with the first email address to complement
127
     * getEmail() if one is set, or null if not.
128
     *
129
     * @return string|null The person name.
130
     */
131 99
    public function getPersonName() : ?string
132
    {
133 99
        if (!empty($this->addresses)) {
134 98
            return $this->addresses[0]->getName();
135
        }
136 1
        return null;
137
    }
138
}
139