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; |
8
|
|
|
|
9
|
|
|
use ArrayAccess; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use RecursiveIterator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Container of IMessagePart items for a parent IMultiPart. |
15
|
|
|
* |
16
|
|
|
* @author Zaahid Bateson |
17
|
|
|
*/ |
18
|
|
|
class PartChildrenContainer implements RecursiveIterator, ArrayAccess |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var IMessagePart[] array of child parts of the IMultiPart object that is |
22
|
|
|
* holding this container. |
23
|
|
|
*/ |
24
|
|
|
protected $children; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int current key position within $children for iteration. |
28
|
|
|
*/ |
29
|
|
|
protected $position = 0; |
30
|
|
|
|
31
|
107 |
|
public function __construct(array $children = []) |
32
|
|
|
{ |
33
|
107 |
|
$this->children = $children; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns true if the current element is an IMultiPart and doesn't return |
38
|
|
|
* null for {@see IMultiPart::getChildIterator()}. Note that the iterator |
39
|
|
|
* may still be empty. |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
#[\ReturnTypeWillChange] |
44
|
70 |
|
public function hasChildren() |
45
|
|
|
{ |
46
|
70 |
|
return ($this->current() instanceof IMultiPart |
47
|
70 |
|
&& $this->current()->getChildIterator() !== null); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* If the current element points to an IMultiPart, its child iterator is |
52
|
|
|
* returned by calling {@see IMultiPart::getChildIterator()}. |
53
|
|
|
* |
54
|
|
|
* @return RecursiveIterator|null the iterator |
55
|
|
|
*/ |
56
|
|
|
#[\ReturnTypeWillChange] |
57
|
69 |
|
public function getChildren() |
58
|
|
|
{ |
59
|
69 |
|
if ($this->current() instanceof IMultiPart) { |
60
|
69 |
|
return $this->current()->getChildIterator(); |
61
|
|
|
} |
62
|
1 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
#[\ReturnTypeWillChange] |
66
|
76 |
|
public function current() |
67
|
|
|
{ |
68
|
76 |
|
return $this->offsetGet($this->position); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
#[\ReturnTypeWillChange] |
72
|
70 |
|
public function key() |
73
|
|
|
{ |
74
|
70 |
|
return $this->position; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
#[\ReturnTypeWillChange] |
78
|
76 |
|
public function next() |
79
|
|
|
{ |
80
|
76 |
|
++$this->position; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
#[\ReturnTypeWillChange] |
84
|
102 |
|
public function rewind() |
85
|
|
|
{ |
86
|
102 |
|
$this->position = 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
#[\ReturnTypeWillChange] |
90
|
102 |
|
public function valid() |
91
|
|
|
{ |
92
|
102 |
|
return $this->offsetExists($this->position); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds the passed IMessagePart to the container in the passed position. |
97
|
|
|
* |
98
|
|
|
* If position is not passed or null, the part is added to the end, as the |
99
|
|
|
* last child in the container. |
100
|
|
|
* |
101
|
|
|
* @param IMessagePart $part The part to add |
102
|
|
|
* @param int $position An optional index position (0-based) to add the |
103
|
|
|
* child at. |
104
|
|
|
*/ |
105
|
75 |
|
public function add(IMessagePart $part, $position = null) |
106
|
|
|
{ |
107
|
75 |
|
$index = ($position === null) ? count($this->children) : $position; |
108
|
75 |
|
array_splice( |
109
|
75 |
|
$this->children, |
110
|
|
|
$index, |
111
|
|
|
0, |
112
|
|
|
[ $part ] |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Removes the passed part, and returns the integer position it occupied. |
118
|
|
|
* |
119
|
|
|
* @param IMessagePart $part The part to remove. |
120
|
|
|
* @return int the 0-based position it previously occupied. |
121
|
|
|
*/ |
122
|
|
|
#[\ReturnTypeWillChange] |
123
|
14 |
|
public function remove(IMessagePart $part) |
124
|
|
|
{ |
125
|
14 |
|
foreach ($this->children as $key => $child) { |
126
|
14 |
|
if ($child === $part) { |
127
|
14 |
|
$this->offsetUnset($key); |
128
|
14 |
|
return $key; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
#[\ReturnTypeWillChange] |
135
|
104 |
|
public function offsetExists($offset) |
136
|
|
|
{ |
137
|
104 |
|
return isset($this->children[$offset]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
#[\ReturnTypeWillChange] |
141
|
77 |
|
public function offsetGet($offset) |
142
|
|
|
{ |
143
|
77 |
|
return $this->offsetExists($offset) ? $this->children[$offset] : null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
#[\ReturnTypeWillChange] |
147
|
1 |
|
public function offsetSet($offset, $value) |
148
|
|
|
{ |
149
|
1 |
|
if (!$value instanceof IMessagePart) { |
150
|
|
|
throw new InvalidArgumentException( |
151
|
|
|
get_class($value) . ' is not a ZBateson\MailMimeParser\Message\IMessagePart' |
152
|
|
|
); |
153
|
|
|
} |
154
|
1 |
|
$index = ($offset === null) ? count($this->children) : $offset; |
155
|
1 |
|
$this->children[$index] = $value; |
156
|
1 |
|
if ($index < $this->position) { |
157
|
|
|
++$this->position; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
#[\ReturnTypeWillChange] |
162
|
14 |
|
public function offsetUnset($offset) |
163
|
|
|
{ |
164
|
14 |
|
array_splice($this->children, $offset, 1); |
165
|
14 |
|
if ($this->position >= $offset) { |
166
|
14 |
|
--$this->position; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|