DataArray.php 1.6 KB
<?php

namespace Lib\Imap;

/**
 * 数据
 * @author:dc
 * @time 2024/9/21 9:27
 * Class DataArray
 * @package Lib\Imap
 */
class DataArray {

    /**
     * 存储的数据
     * @var array
     */
    private array $attribute = [];

    /**
     * @param string $name
     * @param mixed $val
     * @param bool $append
     * @author:dc
     * @time 2024/9/21 9:33
     */
    public function set(string $name, mixed $val, bool $append = false): void
    {
        $name = strtolower($name);
        if($append && is_string($val) && $this->get($name)!==null ){
            $this->attribute[$name] .= $val;
        }else{
            $this->attribute[$name] = $val;
        }
    }


    /**
     * @param string $name
     * @return mixed
     * @author:dc
     * @time 2024/9/21 9:34
     */
    public function get(string $name): mixed
    {
        if(isset($this->attribute[$name])){
            return $this->attribute[$name];
        }

        foreach ($this->attribute as $key=>$value){
            // 不区分大小写对比
            if(strcasecmp($key,$name)===0){
                return $value;
            }
        }

        return '';
    }

    /**
     * 不区分大小写比较字符串
     * @param string $name
     * @param mixed $value
     * @return bool
     * @author:dc
     * @time 2024/9/21 9:57
     */
    public function eq(string $name,mixed $value):bool {
        return strcasecmp($this->get($name),$value) === 0;
    }


    public function __get(string $name): mixed
    {
        return $this->get($name);
    }


    public function __set(string $name, $value): void
    {
        $this->set($name,$value);
    }



}