1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hautzi\SystemMailBundle\SystemMailer\MailDefinition; |
4
|
|
|
|
5
|
|
|
use Hautzi\SystemMailBundle\SystemMailer\ParsedMessage; |
6
|
|
|
use Hautzi\SystemMailBundle\SystemMailer\XML\XsdValidator; |
7
|
|
|
|
8
|
|
|
class MailDefinitionParserXml implements ParserInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var XsdValidator |
12
|
|
|
*/ |
13
|
|
|
private $validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $xsdFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param XsdValidator $validator |
22
|
|
|
* @param $xsdFile |
23
|
|
|
*/ |
24
|
|
|
public function __construct(XsdValidator $validator, $xsdFile) |
25
|
|
|
{ |
26
|
|
|
$this->validator = $validator; |
27
|
|
|
$this->xsdFile = $xsdFile; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Parse XML Mail Definition string |
32
|
|
|
* |
33
|
|
|
* which looks something like this |
34
|
|
|
* <code> |
35
|
|
|
* <email> |
36
|
|
|
* <to name="yourname">[email protected]</email> |
37
|
|
|
* <subject locale="de">Your subject</subject> |
38
|
|
|
* <messageText locale="de">Your Message</messageText> |
39
|
|
|
* </email> |
40
|
|
|
* </code> |
41
|
|
|
* |
42
|
|
|
* @param string $xml |
43
|
|
|
* @param string $locale |
44
|
|
|
* |
45
|
|
|
* @return ParsedMessage |
46
|
|
|
* |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
public function parseMailDefinition($xml, $locale = null) |
50
|
|
|
{ |
51
|
|
|
$this->validator->validate($xml, $this->xsdFile); |
52
|
|
|
|
53
|
|
|
$parsed = simplexml_load_string($xml); |
54
|
|
|
|
55
|
|
|
$message = new ParsedMessage(); |
56
|
|
|
|
57
|
|
|
$from = $parsed->from; |
|
|
|
|
58
|
|
|
if ($from->count() > 0) { |
59
|
|
|
$message->setFrom((string)$from, (string)$from->attributes()['name']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$replyTo = $parsed->replyTo; |
|
|
|
|
63
|
|
|
if ($replyTo->count() > 0) { |
64
|
|
|
$message->setReplyTo((string)$replyTo); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($parsed->to as $to) { |
68
|
|
|
$message->addTo((string)$to, (string)$to->attributes()['name']); |
69
|
|
|
} |
70
|
|
|
foreach ($parsed->cc as $cc) { |
71
|
|
|
$message->addCc((string)$cc, (string)$cc->attributes()['name']); |
72
|
|
|
} |
73
|
|
|
foreach ($parsed->bcc as $bcc) { |
74
|
|
|
$message->addBcc((string)$bcc, (string)$bcc->attributes()['name']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$message->setSubject($this->getLocalizedTagContent($parsed->subject, $locale)); |
78
|
|
|
|
79
|
|
|
$message->setMessageHtml($this->getLocalizedTagContent($parsed->messageHtml, $locale)); |
80
|
|
|
$message->setMessageText($this->getLocalizedTagContent($parsed->messageText, $locale)); |
81
|
|
|
|
82
|
|
|
return $message; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \SimpleXMLElement $element |
87
|
|
|
* @param string $locale |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
* |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
protected function getLocalizedTagContent(\SimpleXMLElement $element, $locale = null) |
94
|
|
|
{ |
95
|
|
|
if (0 === $element->count()) { |
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (null === $locale) { |
|
|
|
|
100
|
|
|
return $this->multilineRemoveIndent((string)$element, 'true' === (string)$element->attributes()['removeIndent']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
foreach ($element as $node) { |
104
|
|
View Code Duplication |
if ($locale == (string)$node->attributes()['locale']) { |
|
|
|
|
105
|
|
|
return $this->multilineRemoveIndent((string)$node, 'true' === (string)$node->attributes()['removeIndent']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$message = sprintf('Locale "%s" not available in node "%s".', $locale, $element->getName()); |
110
|
|
|
throw new \Exception($message); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $string |
115
|
|
|
* @param bool $removeThatShit |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
protected function multilineRemoveIndent($string, $removeThatShit = false) |
120
|
|
|
{ |
121
|
|
|
if (!$removeThatShit) { |
122
|
|
|
return $string; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return trim(preg_replace('@^ *@m', '', $string)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.