1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail; |
4
|
|
|
|
5
|
|
|
use Omnimail\Utils\Token; |
6
|
|
|
|
7
|
|
|
class Attachment implements AttachmentInterface |
8
|
|
|
{ |
9
|
|
|
protected $path; |
10
|
|
|
protected $name; |
11
|
|
|
protected $content; |
12
|
|
|
protected $mimeType; |
13
|
|
|
protected $contentId; |
14
|
|
|
protected $disposition; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string|null $path |
18
|
|
|
* @return AttachmentInterface |
19
|
|
|
*/ |
20
|
1 |
|
public function setPath($path = null) |
21
|
|
|
{ |
22
|
1 |
|
$this->path = $path; |
23
|
1 |
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string|null |
28
|
|
|
*/ |
29
|
1 |
|
public function getPath() |
30
|
|
|
{ |
31
|
1 |
|
return $this->path; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string|null $content |
36
|
|
|
* @return AttachmentInterface |
37
|
|
|
*/ |
38
|
1 |
|
public function setContent($content) |
39
|
|
|
{ |
40
|
1 |
|
$this->content = $content; |
41
|
1 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
1 |
|
public function getContent() |
48
|
|
|
{ |
49
|
1 |
|
return $this->content; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|null $name |
54
|
|
|
* @return AttachmentInterface |
55
|
|
|
*/ |
56
|
1 |
|
public function setName($name) |
57
|
|
|
{ |
58
|
1 |
|
$this->name = $name; |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
1 |
|
public function getName() |
66
|
|
|
{ |
67
|
1 |
|
$name = $this->name; |
68
|
1 |
|
if (!$name && $this->path) { |
|
|
|
|
69
|
|
|
return basename($this->path); |
70
|
|
|
} |
71
|
1 |
|
return $name; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|null $mimeType |
76
|
|
|
* @return AttachmentInterface |
77
|
|
|
*/ |
78
|
1 |
|
public function setMimeType($mimeType) |
79
|
|
|
{ |
80
|
1 |
|
$this->mimeType = $mimeType; |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
87
|
1 |
|
public function getMimeType() |
88
|
|
|
{ |
89
|
1 |
|
return $this->mimeType; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return AttachmentInterface |
94
|
|
|
*/ |
95
|
|
|
public function generateContentId() |
96
|
|
|
{ |
97
|
|
|
if ($this->path) { |
|
|
|
|
98
|
|
|
$this->setContentId(Token::generate(4) . '_' . basename($this->path)); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
$this->setContentId(Token::generate(8)); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string|null $contentId |
107
|
|
|
* @return AttachmentInterface |
108
|
|
|
*/ |
109
|
1 |
|
public function setContentId($contentId) |
110
|
|
|
{ |
111
|
1 |
|
if (null === $this->disposition) { |
112
|
1 |
|
$this->setDisposition(); |
113
|
1 |
|
} |
114
|
1 |
|
$this->contentId = $contentId; |
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string|null |
120
|
|
|
*/ |
121
|
1 |
|
public function getContentId() |
122
|
|
|
{ |
123
|
1 |
|
return $this->contentId; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string|null $disposition |
128
|
|
|
* @return AttachmentInterface |
129
|
|
|
*/ |
130
|
1 |
|
public function setDisposition($disposition = AttachmentInterface::DISPOSITION_INLINE) |
131
|
|
|
{ |
132
|
1 |
|
$this->disposition = $disposition; |
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string|null |
138
|
|
|
*/ |
139
|
1 |
|
public function getDisposition() |
140
|
|
|
{ |
141
|
1 |
|
return $this->disposition; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
1 |
|
public function toArray() |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
1 |
|
'path' => $this->getPath(), |
151
|
1 |
|
'content' => $this->getContent(), |
152
|
1 |
|
'name' => $this->getName(), |
153
|
1 |
|
'mimeType' => $this->getMimeType(), |
154
|
1 |
|
'contentId' => $this->getContentId(), |
155
|
1 |
|
'disposition' => $this->getDisposition() |
156
|
1 |
|
]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: