审查视图

lib/Imap/Parse/MessageItem.php 5.0 KB
邓超 authored
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace Lib\Imap\Parse;

use Lib\Imap\Request\Msg;

/**
 * 邮件
 * @author:dc
 * @time 2024/9/18 11:30
 * @property Body $body
m  
邓超 authored
12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * @method bool seen() 标记为 已读
 * @method bool unseen() 标记为 未读
 * @method bool deleted() 标记为 删除
 * @method bool undeleted() 标记为 取消删除
 * @method bool flagged() 标记为 星标
 * @method bool unflagged() 标记为 取消星标
 * @method bool recent() 标记为 最近
 * @method bool unrecent() 标记为 取消最近
 * @method bool answered() 标记为 已回复
 * @method bool unanswered() 标记为 取消已回复
 * @method bool draft() 标记为 草稿
 * @method bool undraft() 标记为 取消草稿
 * @method bool move(string $folder,bool $utf7 = true) 移动当前邮件到指定目录
 * @method bool copy(string $folder,bool $utf7 = true) 复制当前邮件到指定目录
邓超 authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * Class MessageItem
 * @package Lib\Imap\Parse
 */
class MessageItem {

    /**
     * 邮件的唯一id 文件夹下面唯一
     * @var int
     */
    public int $uid = 0;

    /**
     * 邮件编号
     * @var int
     */
    public int $msgno = 0;

    /**
     * 这个是邮件收到的时间
     * @var string
     */
    public string $date = '';

    /**
     * 邮件大小
     * @var int
     */
    public int $size = 0;

    /**
     * 标记
     * @var array
     */
    public array $flags = [];

    /**
     * 头部信息 主题 发件 收件 发送时间等消息
     * @var Header
     */
    public Header $header;
x  
邓超 authored
67 68 69 70 71 72
    /**
     * 邮件的mime结构体
     * @var string
     */
    public string $bodystructure;
邓超 authored
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    /**
     * @var Msg
     */
    protected Msg $msg;

    /**
     * MessageItem constructor.
     * @param int $msgno
     */
    public function __construct(int $msgno, Msg $msg)
    {

        $this->msg = $msg;

        $this->msgno = $msgno;
    }

    /**
     * 是否已被标记为删除了
     * @return bool
     * @author:dc
     * @time 2024/9/18 11:52
     */
    public function isDeleted():bool {
        return in_array('deleted',$this->flags);
    }

    /**
     * 已读
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isSeen():bool {
        return in_array('seen',$this->flags);
    }

    /**
     * 草稿
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isDraft():bool {
x  
邓超 authored
118
        return in_array('draft',$this->flags);
邓超 authored
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    }

    /**
     * 星标
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:53
     */
    public function isFlagged():bool {
        return in_array('flagged',$this->flags);
    }

    /**
     * 已回复
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:52
     */
    public function isAnswered():bool {
        return in_array('answered',$this->flags);
    }

    /**
     * 最近
     * @return bool
     * @author:dc
     * @time 2024/9/18 17:54
     */
    public function isRecent():bool {
        return in_array('recent',$this->flags);
    }

    /**
     * 获取邮件体 字段 内容
     * @param string $name
     * @return mixed
     * @author:dc
     * @time 2024/9/20 15:13
     */
    public function get(string $name){
        return $this->header->get($name);
    }

    /**
     * 获取body体
     * @return Body
     * @author:dc
     * @time 2024/9/20 15:14
     */
    public function getBody():Body {
m  
邓超 authored
169 170 171 172 173 174 175
        if(isset($this->body)){
            return $this->body;
        }

        $this->body = $this->msg->getBody($this);

        return $this->body;
邓超 authored
176 177
    }
邓超 authored
178 179 180 181 182 183 184 185 186 187
    /**
     * 所属文件夹
     * @return string
     * @author:dc
     * @time 2024/9/26 11:46
     */
    public function getFolderName():string {
        return $this->msg->folder->getName();
    }
邓超 authored
188 189 190 191 192 193 194 195 196 197

    public function __get(string $name)
    {
        if($name=='body'){
            return $this->getBody();
        }else{
            $this->get($name);
        }
    }
x  
邓超 authored
198 199 200 201 202 203 204 205 206
    /**
     * @return bool 是否有附件
     * @author:dc
     * @time 2024/9/29 13:58
     */
    public function isAttachment():bool {
        return stripos($this->bodystructure, '"attachment"') !== false;
    }
m  
邓超 authored
207 208 209 210 211


    public function __call(string $name, array $arguments)
    {
        switch ($name){
m  
邓超 authored
212 213 214 215 216 217 218 219 220 221 222 223 224 225
            case 'seen':{} // 已读
            case 'unseen':{} // 未读
            case 'deleted':{}//删除
            case 'undeleted':{}//取消删除
            case 'flagged':{}//星标
            case 'unflagged':{}//取消星标
            case 'recent':{}//最近
            case 'unrecent':{}// 取消最近
            case 'answered':{} // 已回复
            case 'unanswered':{}// 取消已回复
            case 'draft':{}// 草稿
            case 'undraft':{}//取消草稿
            case 'move':{}//移动当前邮件到指定目录
            case 'copy':{return $this->msg->uid($this->uid)->{$name}(...$arguments);}//移动当前邮件到指定目录
m  
邓超 authored
226 227 228 229 230 231
            default:{
                throw new \Exception('未定义函数 '.$name);
            }
        }
    }
邓超 authored
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}