|
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
|
|
|
namespace ZBateson\MailMimeParser\Message\Part; |
|
8
|
|
|
|
|
9
|
|
|
use Psr\Http\Message\StreamInterface; |
|
10
|
|
|
use ZBateson\MailMimeParser\Message\PartFilterFactory; |
|
11
|
|
|
use ZBateson\MailMimeParser\Message\PartFilter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* MimePart methods related to child parts. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Zaahid Bateson |
|
17
|
|
|
*/ |
|
18
|
|
|
trait MimePartChildrenTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \ZBateson\MailMimeParser\Message\PartFilterFactory factory object |
|
22
|
|
|
* responsible for create PartFilters |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $partFilterFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \ZBateson\MailMimeParser\Message\Part\MessagePart[] array of child |
|
28
|
|
|
* parts |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $children = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param PartFilterFactory $partFilterFactory |
|
34
|
|
|
* @param PartBuilder $partBuilder |
|
35
|
|
|
* @param StreamInterface $stream |
|
36
|
|
|
*/ |
|
37
|
24 |
|
public function __construct( |
|
38
|
|
|
PartFilterFactory $partFilterFactory, |
|
39
|
|
|
PartBuilder $partBuilder, |
|
40
|
|
|
StreamInterface $stream |
|
41
|
|
|
) { |
|
42
|
24 |
|
$this->partFilterFactory = $partFilterFactory; |
|
43
|
24 |
|
$pbChildren = $partBuilder->getChildren(); |
|
44
|
24 |
|
if (!empty($pbChildren)) { |
|
45
|
7 |
|
$this->children = array_map(function ($child) use ($stream) { |
|
46
|
7 |
|
$childPart = $child->createMessagePart($stream); |
|
47
|
7 |
|
$childPart->parent = $this; |
|
48
|
7 |
|
return $childPart; |
|
49
|
7 |
|
}, $pbChildren); |
|
50
|
|
|
} |
|
51
|
24 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns all parts, including the current object, and all children below |
|
55
|
|
|
* it (including children of children, etc...) |
|
56
|
|
|
* |
|
57
|
|
|
* @return MessagePart[] |
|
58
|
|
|
*/ |
|
59
|
6 |
|
protected function getAllNonFilteredParts() |
|
60
|
|
|
{ |
|
61
|
6 |
|
$parts = [ $this ]; |
|
62
|
6 |
|
foreach ($this->children as $part) { |
|
63
|
6 |
|
if ($part instanceof MimePart) { |
|
64
|
6 |
|
$parts = array_merge( |
|
65
|
6 |
|
$parts, |
|
66
|
6 |
|
$part->getAllNonFilteredParts() |
|
67
|
|
|
); |
|
68
|
|
|
} else { |
|
69
|
6 |
|
array_push($parts, $part); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
6 |
|
return $parts; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the part at the given 0-based index, or null if none is set. |
|
77
|
|
|
* |
|
78
|
|
|
* Note that the first part returned is the current part itself. This is |
|
79
|
|
|
* often desirable for queries with a PartFilter, e.g. looking for a |
|
80
|
|
|
* MessagePart with a specific Content-Type that may be satisfied by the |
|
81
|
|
|
* current part. |
|
82
|
|
|
* |
|
83
|
|
|
* @param int $index |
|
84
|
|
|
* @param PartFilter $filter |
|
85
|
|
|
* @return MessagePart |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getPart($index, PartFilter $filter = null) |
|
88
|
|
|
{ |
|
89
|
2 |
|
$parts = $this->getAllParts($filter); |
|
90
|
2 |
|
if (!isset($parts[$index])) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
2 |
|
return $parts[$index]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the current part, all child parts, and child parts of all |
|
98
|
|
|
* children optionally filtering them with the provided PartFilter. |
|
99
|
|
|
* |
|
100
|
|
|
* The first part returned is always the current MimePart. This is often |
|
101
|
|
|
* desirable as it may be a valid MimePart for the provided PartFilter. |
|
102
|
|
|
* |
|
103
|
|
|
* @param PartFilter $filter an optional filter |
|
104
|
|
|
* @return MessagePart[] |
|
105
|
|
|
*/ |
|
106
|
6 |
|
public function getAllParts(PartFilter $filter = null) |
|
107
|
|
|
{ |
|
108
|
6 |
|
$parts = $this->getAllNonFilteredParts(); |
|
109
|
6 |
|
if (!empty($filter)) { |
|
110
|
4 |
|
return array_values(array_filter( |
|
111
|
4 |
|
$parts, |
|
112
|
4 |
|
[ $filter, 'filter' ] |
|
113
|
|
|
)); |
|
114
|
|
|
} |
|
115
|
4 |
|
return $parts; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the total number of parts in this and all children. |
|
120
|
|
|
* |
|
121
|
|
|
* Note that the current part is considered, so the minimum getPartCount is |
|
122
|
|
|
* 1 without a filter. |
|
123
|
|
|
* |
|
124
|
|
|
* @param PartFilter $filter |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function getPartCount(PartFilter $filter = null) |
|
128
|
|
|
{ |
|
129
|
2 |
|
return count($this->getAllParts($filter)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the direct child at the given 0-based index, or null if none is |
|
134
|
|
|
* set. |
|
135
|
|
|
* |
|
136
|
|
|
* @param int $index |
|
137
|
|
|
* @param PartFilter $filter |
|
138
|
|
|
* @return MessagePart |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function getChild($index, PartFilter $filter = null) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$parts = $this->getChildParts($filter); |
|
143
|
2 |
|
if (!isset($parts[$index])) { |
|
144
|
|
|
return null; |
|
145
|
|
|
} |
|
146
|
2 |
|
return $parts[$index]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns all direct child parts. |
|
151
|
|
|
* |
|
152
|
|
|
* If a PartFilter is provided, the PartFilter is applied before returning. |
|
153
|
|
|
* |
|
154
|
|
|
* @param PartFilter $filter |
|
155
|
|
|
* @return MessagePart[] |
|
156
|
|
|
*/ |
|
157
|
3 |
|
public function getChildParts(PartFilter $filter = null) |
|
158
|
|
|
{ |
|
159
|
3 |
|
if ($filter !== null) { |
|
160
|
1 |
|
return array_values(array_filter($this->children, [ $filter, 'filter' ])); |
|
161
|
|
|
} |
|
162
|
2 |
|
return $this->children; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns the number of direct children under this part. |
|
167
|
|
|
* |
|
168
|
|
|
* @param PartFilter $filter |
|
169
|
|
|
* @return int |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function getChildCount(PartFilter $filter = null) |
|
172
|
|
|
{ |
|
173
|
1 |
|
return count($this->getChildParts($filter)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns the part associated with the passed mime type if it exists. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $mimeType |
|
180
|
|
|
* @return MessagePart or null |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function getPartByMimeType($mimeType, $index = 0) |
|
183
|
|
|
{ |
|
184
|
1 |
|
$partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType); |
|
185
|
1 |
|
return $this->getPart($index, $partFilter); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Returns an array of all parts associated with the passed mime type if any |
|
190
|
|
|
* exist or null otherwise. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $mimeType |
|
193
|
|
|
* @return MessagePart[] or null |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function getAllPartsByMimeType($mimeType) |
|
196
|
|
|
{ |
|
197
|
1 |
|
$partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType); |
|
198
|
1 |
|
return $this->getAllParts($partFilter); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns the number of parts matching the passed $mimeType |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $mimeType |
|
205
|
|
|
* @return int |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function getCountOfPartsByMimeType($mimeType) |
|
208
|
|
|
{ |
|
209
|
1 |
|
$partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType); |
|
210
|
1 |
|
return $this->getPartCount($partFilter); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|