88use greeny \MailLibrary \Attachment ;
99use greeny \MailLibrary \Drivers \ImapDriver ;
1010use greeny \MailLibrary \Mailbox ;
11+ use greeny \MailLibrary \MimePart ;
1112
1213class ImapStructure implements IStructure {
1314 const TYPE_TEXT = 0 ;
@@ -37,6 +38,9 @@ class ImapStructure implements IStructure {
3738 self ::TYPE_OTHER => 'other ' ,
3839 );
3940
41+ /** @var MimePart[] */
42+ private $ mimeParts = [];
43+
4044 /** @var \greeny\MailLibrary\Drivers\ImapDriver */
4145 protected $ driver ;
4246
@@ -64,6 +68,9 @@ class ImapStructure implements IStructure {
6468 /** @var Mailbox */
6569 protected $ mailbox ;
6670
71+ /** @var array */
72+ private $ rawStructure = [];
73+
6774 /**
6875 * @param ImapDriver $driver
6976 * @param object $structure
@@ -72,6 +79,7 @@ class ImapStructure implements IStructure {
7279 */
7380 public function __construct (ImapDriver $ driver , $ structure , $ mailId , Mailbox $ mailbox )
7481 {
82+ $ this ->rawStructure = $ structure ;
7583 $ this ->driver = $ driver ;
7684 $ this ->id = $ mailId ;
7785 $ this ->mailbox = $ mailbox ;
@@ -84,6 +92,15 @@ public function __construct(ImapDriver $driver, $structure, $mailId, Mailbox $ma
8492 }
8593 }
8694
95+ /**
96+ * @return array
97+ * @internal use only with caution, format can change without warning
98+ */
99+ public function getRawStructure ()
100+ {
101+ return $ this ->rawStructure ;
102+ }
103+
87104 /**
88105 * @return string
89106 */
@@ -100,9 +117,9 @@ public function getHtmlBody()
100117 if ($ this ->htmlBody === NULL ) {
101118 $ this ->driver ->switchMailbox ($ this ->mailbox ->getName ());
102119 return $ this ->htmlBody = $ this ->driver ->getBody ($ this ->id , $ this ->htmlBodyIds );
103- } else {
104- return $ this ->htmlBody ;
105120 }
121+
122+ return $ this ->htmlBody ;
106123 }
107124
108125 /**
@@ -113,9 +130,9 @@ public function getTextBody()
113130 if ($ this ->textBody === NULL ) {
114131 $ this ->driver ->switchMailbox ($ this ->mailbox ->getName ());
115132 return $ this ->textBody = $ this ->driver ->getBody ($ this ->id , $ this ->textBodyIds );
116- } else {
117- return $ this ->textBody ;
118133 }
134+
135+ return $ this ->textBody ;
119136 }
120137
121138 /**
@@ -133,6 +150,21 @@ public function getAttachments()
133150 return $ this ->attachments ;
134151 }
135152
153+ /**
154+ * @return MimePart[]
155+ */
156+ public function getMimeParts ()
157+ {
158+ $ this ->driver ->switchMailbox ($ this ->mailbox ->getName ());
159+ return $ this ->mimeParts ;
160+ }
161+
162+ /** @deprecated use getMimeParts() instead */
163+ public function getParts () {
164+ \trigger_error (\E_USER_DEPRECATED , 'use getMimeParts() instead ' );
165+ return $ this ->getMimeParts ();
166+ }
167+
136168 protected function addStructurePart ($ structure , $ partId )
137169 {
138170 $ type = $ structure ->type ;
@@ -151,13 +183,27 @@ protected function addStructurePart($structure, $partId)
151183 }
152184 }
153185
186+ /** @noinspection NestedTernaryOperatorInspection Yep, will fix this when we switch to PHP7 level; see ?? operator */
187+ $ this ->mimeParts [] = new MimePart (
188+ $ this ->driver , // for lazy loading
189+ $ this ->id ,
190+ $ partId ,
191+ self ::$ typeTable [$ type ]. '/ ' . $ subtype ,
192+ (empty ($ parameters ['filename ' ]) ? $ parameters ['filename ' ] : (
193+ empty ($ parameters ['name ' ]) ? $ parameters ['name ' ] : ''
194+ )),
195+ $ encoding
196+ );
197+
198+
154199 if (isset ($ parameters ['filename ' ]) || isset ($ parameters ['name ' ])) {
155200 $ this ->attachmentsIds [] = array (
156201 'id ' => $ partId ,
157202 'encoding ' => $ encoding ,
158203 'name ' => isset ($ parameters ['filename ' ]) ? $ parameters ['filename ' ] : $ parameters ['name ' ],
159204 'type ' => self ::$ typeTable [$ type ]. '/ ' . $ subtype ,
160205 );
206+
161207 } else if ($ type === self ::TYPE_TEXT ) {
162208 if ($ subtype === 'HTML ' ) {
163209 $ this ->htmlBodyIds [] = array ('id ' => $ partId , 'encoding ' => $ encoding );
0 commit comments