Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Support/Masks/MessageMask.php

raw symbols SCIP
1 <?php
2 /*
3 * File: MessageMask.php
4 * Category: Mask
5 * Author: M.Goldenbaum
6 * Created: 14.03.19 20:49
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Support\Masks;
14
15 use Webklex\PHPIMAP\Attachment;
16 use Webklex\PHPIMAP\Message;
17
18 /**
19 * Class MessageMask
20 *
21 * @package Webklex\PHPIMAP\Support\Masks
22 * @mixin Message
23 */
24 class MessageMask extends Mask {
25
26 /** @var Message $parent */
27 protected mixed $parent;
28
29 /**
30 * Get the message html body
31 *
32 * @return null
33 */
34 public function getHtmlBody(){
35 $bodies = $this->parent->getBodies();
36 if (!isset($bodies['html'])) {
37 return null;
38 }
39
40 if(is_object($bodies['html']) && property_exists($bodies['html'], 'content')) {
41 return $bodies['html']->content;
42 }
43 return $bodies['html'];
44 }
45
46 /**
47 * Get the Message html body filtered by an optional callback
48 * @param callable|null $callback
49 *
50 * @return string|null
51 */
52 public function getCustomHTMLBody(?callable $callback = null): ?string {
53 $body = $this->getHtmlBody();
54 if($body === null) return null;
55
56 if ($callback !== null) {
57 $aAttachment = $this->parent->getAttachments();
58 $aAttachment->each(function($oAttachment) use(&$body, $callback) {
59 /** @var Attachment $oAttachment */
60 if(is_callable($callback)) {
61 $body = $callback($body, $oAttachment);
62 }elseif(is_string($callback)) {
63 call_user_func($callback, [$body, $oAttachment]);
64 }
65 });
66 }
67
68 return $body;
69 }
70
71 /**
72 * Get the Message html body with embedded base64 images
73 * the resulting $body.
74 *
75 * @return string|null
76 */
77 public function getHTMLBodyWithEmbeddedBase64Images(): ?string {
78 return $this->getCustomHTMLBody(function($body, $oAttachment){
79 /** @var Attachment $oAttachment */
80 if ($oAttachment->id) {
81 $body = str_replace('cid:'.$oAttachment->id, 'data:'.$oAttachment->getContentType().';base64, '.base64_encode($oAttachment->getContent()), $body);
82 }
83
84 return $body;
85 });
86 }
87 }