Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
GetFieldValueFixTrait | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
getFieldValue | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | |
3 | /** |
4 | * Trait that provides an improved version of the getFieldValue method. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) The National Library of Finland 2023. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Mailer |
25 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Mailer; |
31 | |
32 | use Laminas\Mail\Header\HeaderInterface; |
33 | use Laminas\Mail\Header\HeaderValue; |
34 | use Laminas\Mail\Header\HeaderWrap; |
35 | use Laminas\Mail\Headers; |
36 | |
37 | use function sprintf; |
38 | |
39 | /** |
40 | * Trait that provides an improved version of the getFieldValue method. |
41 | * |
42 | * @category VuFind |
43 | * @package Mailer |
44 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org/wiki/development Wiki |
47 | */ |
48 | trait GetFieldValueFixTrait |
49 | { |
50 | /** |
51 | * Retrieve header value |
52 | * |
53 | * Overrides the original implementation to always enclose name in quotes. |
54 | * |
55 | * @param bool $format Return the value in Mime::Encoded (HeaderInterface::FORMAT_ENCODED) or |
56 | * in Raw format (HeaderInterface::FORMAT_RAW). Using a constant from HeaderInterface is |
57 | * recommended instead of a raw boolean value. |
58 | * |
59 | * @return string |
60 | */ |
61 | public function getFieldValue($format = HeaderInterface::FORMAT_RAW) |
62 | { |
63 | $emails = []; |
64 | $encoding = $this->getEncoding(); |
65 | |
66 | foreach ($this->getAddressList() as $address) { |
67 | $email = $address->getEmail(); |
68 | $name = $address->getName(); |
69 | |
70 | if ( |
71 | $format === HeaderInterface::FORMAT_ENCODED |
72 | && 'ASCII' !== $encoding |
73 | ) { |
74 | if (! empty($name)) { |
75 | $name = HeaderWrap::mimeEncodeValue($name, $encoding); |
76 | } |
77 | |
78 | if (preg_match('/^(.+)@([^@]+)$/', $email, $matches)) { |
79 | $localPart = $matches[1]; |
80 | $hostname = $this->idnToAscii($matches[2]); |
81 | $email = sprintf('%s@%s', $localPart, $hostname); |
82 | } |
83 | } |
84 | |
85 | if (empty($name)) { |
86 | $emails[] = $email; |
87 | } else { |
88 | $emails[] = sprintf('"%s" <%s>', $name, $email); |
89 | } |
90 | } |
91 | |
92 | // Ensure the values are valid before sending them. |
93 | if ($format !== HeaderInterface::FORMAT_RAW) { |
94 | foreach ($emails as $email) { |
95 | HeaderValue::assertValid($email); |
96 | } |
97 | } |
98 | |
99 | return implode(',' . Headers::FOLDING, $emails); |
100 | } |
101 | } |