PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / vendor / zbateson / mail-mime-parser / src / Header / HeaderFactory.php

HeaderFactory.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at vendor/zbateson/mail-mime-parser/src/Header/HeaderFactory.php

177 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
11 use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
12
13 /**
14 * Constructs various IHeader types depending on the type of header passed.
15 *
16 * If the passed header resolves to a specific defined header type, it is parsed
17 * as such. Otherwise, a GenericHeader is instantiated and returned. Headers
18 * are mapped as follows:
19 *
20 * - {@see AddressHeader}: From, To, Cc, Bcc, Sender, Reply-To, Resent-From,
21 * Resent-To, Resent-Cc, Resent-Bcc, Resent-Reply-To, Return-Path,
22 * Delivered-To
23 * - {@see DateHeader}: Date, Resent-Date, Delivery-Date, Expires, Expiry-Date,
24 * Reply-By
25 * - {@see ParameterHeader}: Content-Type, Content-Disposition, Received-SPF,
26 * Authentication-Results, DKIM-Signature, Autocrypt
27 * - {@see SubjectHeader}: Subject
28 * - {@see IdHeader}: Message-ID, Content-ID, In-Reply-To, References
29 * - {@see ReceivedHeader}: Received
30 *
31 * @author Zaahid Bateson
32 */
33 class HeaderFactory
34 {
35 /**
36 * @var ConsumerService the passed ConsumerService providing
37 * AbstractConsumer singletons.
38 */
39 protected $consumerService;
40
41 /**
42 * @var MimeLiteralPartFactory for mime decoding.
43 */
44 protected $mimeLiteralPartFactory;
45
46 /**
47 * @var string[][] maps IHeader types to headers.
48 */
49 protected $types = [
50 \ZBateson\MailMimeParser\Header\AddressHeader::class => [
51 'from',
52 'to',
53 'cc',
54 'bcc',
55 'sender',
56 'replyto',
57 'resentfrom',
58 'resentto',
59 'resentcc',
60 'resentbcc',
61 'resentreplyto',
62 'returnpath',
63 'deliveredto',
64 ],
65 \ZBateson\MailMimeParser\Header\DateHeader::class => [
66 'date',
67 'resentdate',
68 'deliverydate',
69 'expires',
70 'expirydate',
71 'replyby',
72 ],
73 \ZBateson\MailMimeParser\Header\ParameterHeader::class => [
74 'contenttype',
75 'contentdisposition',
76 'receivedspf',
77 'authenticationresults',
78 'dkimsignature',
79 'autocrypt',
80 ],
81 \ZBateson\MailMimeParser\Header\SubjectHeader::class => [
82 'subject',
83 ],
84 \ZBateson\MailMimeParser\Header\IdHeader::class => [
85 'messageid',
86 'contentid',
87 'inreplyto',
88 'references'
89 ],
90 \ZBateson\MailMimeParser\Header\ReceivedHeader::class => [
91 'received'
92 ]
93 ];
94
95 /**
96 * @var string Defines the generic IHeader type to use for headers that
97 * aren't mapped in $types
98 */
99 protected $genericType = \ZBateson\MailMimeParser\Header\GenericHeader::class;
100
101 /**
102 * Instantiates member variables with the passed objects.
103 *
104 */
105 public function __construct(ConsumerService $consumerService, MimeLiteralPartFactory $mimeLiteralPartFactory)
106 {
107 $this->consumerService = $consumerService;
108 $this->mimeLiteralPartFactory = $mimeLiteralPartFactory;
109 }
110
111 /**
112 * Returns the string in lower-case, and with non-alphanumeric characters
113 * stripped out.
114 *
115 * @param string $header The header name
116 * @return string The normalized header name
117 */
118 public function getNormalizedHeaderName(string $header) : string
119 {
120 return \preg_replace('/[^a-z0-9]/', '', \strtolower($header));
121 }
122
123 /**
124 * Returns the name of an IHeader class for the passed header name.
125 *
126 * @param string $name The header name.
127 * @return string The Fully Qualified class name.
128 */
129 private function getClassFor(string $name) : string
130 {
131 $test = $this->getNormalizedHeaderName($name);
132 foreach ($this->types as $class => $matchers) {
133 foreach ($matchers as $matcher) {
134 if ($test === $matcher) {
135 return $class;
136 }
137 }
138 }
139 return $this->genericType;
140 }
141
142 /**
143 * Creates an IHeader instance for the passed header name and value, and
144 * returns it.
145 *
146 * @param string $name The header name.
147 * @param string $value The header value.
148 * @return IHeader The created header object.
149 */
150 public function newInstance(string $name, string $value)
151 {
152 $class = $this->getClassFor($name);
153 return $this->newInstanceOf($name, $value, $class);
154 }
155
156 /**
157 * Creates an IHeader instance for the passed header name and value, and
158 * returns it.
159 *
160 * @param string $name The header name.
161 * @param string $value The header value.
162 * @return IHeader The created header object.
163 */
164 public function newInstanceOf(string $name, string $value, string $iHeaderClass) : IHeader
165 {
166 if (\is_a($iHeaderClass, 'ZBateson\MailMimeParser\Header\MimeEncodedHeader', true)) {
167 return new $iHeaderClass(
168 $this->mimeLiteralPartFactory,
169 $this->consumerService,
170 $name,
171 $value
172 );
173 }
174 return new $iHeaderClass($this->consumerService, $name, $value);
175 }
176 }
177