1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mail package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mail; |
11
|
|
|
|
12
|
|
|
use Slick\Template\TemplateEngineInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Simple text e-mail message body |
16
|
|
|
* |
17
|
|
|
* @package Slick\Mail |
18
|
|
|
* @author Filipe Silva <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class MessageBody implements MessageBodyInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var MessageInterface |
24
|
|
|
*/ |
25
|
|
|
protected $message; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $template; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $data; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* To load content from twig templates |
39
|
|
|
*/ |
40
|
|
|
use ContentFromTemplateMethods; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $bodyText; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $length; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var TemplateEngineInterface |
54
|
|
|
*/ |
55
|
|
|
protected $templateEngine; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates an e-mail message body content |
59
|
|
|
* |
60
|
|
|
* @param string $template |
61
|
|
|
* @param array $data |
62
|
|
|
*/ |
63
|
10 |
|
public function __construct($template, $data = []) |
64
|
|
|
{ |
65
|
10 |
|
$this->template = $template; |
66
|
10 |
|
$this->data = $data; |
67
|
10 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Attaches the E-Mail massage that will carry this body |
71
|
|
|
* |
72
|
|
|
* @param MessageInterface $message |
73
|
|
|
* |
74
|
|
|
* @return MessageBodyInterface |
75
|
|
|
*/ |
76
|
6 |
|
public function setMailMessage(MessageInterface $message) |
77
|
|
|
{ |
78
|
6 |
|
$this->message = $message; |
79
|
6 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the string-serialized message body text |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
8 |
|
public function getBodyText() |
88
|
|
|
{ |
89
|
8 |
|
if (null == $this->bodyText) { |
90
|
8 |
|
$text = $this->getTemplateEngine() |
91
|
8 |
|
->parse($this->template) |
92
|
8 |
|
->process($this->data); |
93
|
8 |
|
$this->bodyText = trim($text); |
94
|
4 |
|
} |
95
|
8 |
|
return $this->bodyText; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the string representation of this message body |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
2 |
|
public function __toString() |
104
|
|
|
{ |
105
|
2 |
|
return $this->getBodyText(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Gets message length |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
2 |
|
public function getLength() |
114
|
|
|
{ |
115
|
2 |
|
if (null == $this->length) { |
116
|
2 |
|
$this->length = strlen($this); |
117
|
1 |
|
} |
118
|
2 |
|
return $this->length; |
119
|
|
|
} |
120
|
|
|
} |