Request.php 1.8 KB
<?php

namespace Lib\Imap\Request;


use Lib\Imap\Imap;

/**
 * 命令组装 请求 返回处理
 * @author:dc
 * @time 2024/9/18 10:12
 * Class Request
 * @package Lib\Imap\Request
 */
abstract class  Request {


    protected Imap $imap;


    /**
     * 命令执行结果
     * @var array
     */
    protected array $result = [];

    /**
     * 执行结果状态 YES OK NO BAD
     * @var string
     */
    protected string $status = '';

    /**
     * 这条命令执行的tag
     * @var string
     */
    protected string $tag = '';

    /**
     * 最后的消息
     * @var string
     */
    protected string $message = '';


    public function __construct(Imap $imap)
    {
        $this->imap = $imap;
    }

    /**
     * 执行命令
     * @param string $cmd 命令
     * @param mixed ...$params 命令参数
     * @author:dc
     * @time 2024/9/13 17:36
     */
    protected final function cmd(string $cmd,...$params){
        $this->result = $this->imap->client->request($params?sprintf($cmd,...$params):$cmd);
        // 删除最后一行的状态
        $end = array_pop($this->result);
        // 状态处理
        list($this->tag,$this->status,$this->message) = explode(' ',$end.'     ',3);
        $this->message = trim($this->message);

    }


    abstract public function exec():static ;


    /**
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }


    /**
     * @return string
     */
    public function getStatus(): string
    {
        return $this->status;
    }


    /**
     * @return string
     */
    public function getTag(): string
    {
        return $this->tag;
    }


    /**
     * 是否成功
     * @return bool
     * @author:dc
     * @time 2024/9/14 9:10
     */
    public function isOk(){
        return $this->status == 'OK';
    }

}