MessageItem.php 5.1 KB
<?php

namespace Lib\Imap\Parse;

use Lib\Imap\Request\Msg;

/**
 * 邮件
 * @author:dc
 * @time 2024/9/18 11:30
 * @property Body $body
 * @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) 复制当前邮件到指定目录
 * 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;


    /**
     * @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 {
        return in_array('seen',$this->flags);
    }

    /**
     * 星标
     * @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 {
        if(isset($this->body)){
            return $this->body;
        }

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

        return $this->body;
    }


    public function __get(string $name)
    {
        if($name=='body'){
            return $this->getBody();
        }else{
            $this->get($name);
        }
    }



    public function __call(string $name, array $arguments)
    {
        switch ($name){
            case 'seen':{return $this->msg->uid($this->uid)->seen();} // 已读
            case 'unseen':{return $this->msg->uid($this->uid)->unseen();} // 未读
            case 'deleted':{return $this->msg->uid($this->uid)->deleted();}//删除
            case 'undeleted':{return $this->msg->uid($this->uid)->undeleted();}//取消删除
            case 'flagged':{return $this->msg->uid($this->uid)->flagged();}//星标
            case 'unflagged':{return $this->msg->uid($this->uid)->unflagged();}//取消星标
            case 'recent':{return $this->msg->uid($this->uid)->recent();}//最近
            case 'unrecent':{return $this->msg->uid($this->uid)->unrecent();}// 取消最近
            case 'answered':{return $this->msg->uid($this->uid)->answered();} // 已回复
            case 'unanswered':{return $this->msg->uid($this->uid)->unanswered();}// 取消已回复
            case 'draft':{return $this->msg->uid($this->uid)->draft();}// 草稿
            case 'undraft':{return $this->msg->uid($this->uid)->undraft();}//取消草稿
            case 'move':{return $this->msg->uid($this->uid)->move(...$arguments);}//移动当前邮件到指定目录
            case 'copy':{return $this->msg->uid($this->uid)->copy(...$arguments);}//移动当前邮件到指定目录
            default:{
                throw new \Exception('未定义函数 '.$name);
            }
        }
    }

}