工作流模式的链表模型类

工作流的线性结构正好和数据结构的链表类似,于是就可以用链表来实现这个业务。

<?php
function string_to_array($string,$delimiter=",")
{
    if(is_string($string))
    {
       $string=array_filter(explode($delimiter,$string));
    }
    return $string;
}
function array_to_string($array=array(),$delimiter=",")
{
    if(is_array($array))
    {
        $array=implode($delimiter,$array);
    }
    return $array;
}
class MyQueue
{
    private $total_array_list=array();//总的数组链表
    private $progress_array_list=array();//已经完成的任务链表
    private $current_index=0;//当前链表指针位置
    private $prev_point=0;//指针上一位置
    private $next_point=0;//指针下一位置
    public function __construct($leader_list="",$checked_leader_list="")
    {
        $this->total_array_list=string_to_array($leader_list);
        $this->progress_array_list=string_to_array($checked_leader_list);
        $progress_length=count($this->progress_array_list);//当前完成进度
        $this->current_index=$progress_length;//指针位置
        $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null;
        $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null;
    }

    /*
     * 队列链表指针向前移动
     * **/
    public function forward()
    {
        //链表向前移动
        if(is_null($this->next_point))
        {
            //到了最后一个任务了 不可继续向前
        }
        else
        {
            $this->progress_array_list[]=$this->total_array_list[$this->current_index];
            $this->current_index++;
            $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null;
            $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null;
        }
        return $this;
    }
    /*
     * 队列链表指针向后移动
     * **/
    public function back_off()
    {
        //链表向后移动
        if(is_null($this->prev_point))
        {
            //到了第一个任务了 不可继续向后
        }
        else
        {
            array_pop($this->progress_array_list);
            $this->current_index--;
            $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null;
            $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null;
        }
        return $this;
    }
    /*
     * 拿到链表的信息
     * **/
    public function get_param()
    {
        $leader_list=array_to_string($this->total_array_list);
        $checked_leader_list=array_to_string($this->progress_array_list);
        $prev_leader=$this->prev_point;
        $next_leader=$this->next_point;
        return compact(
            "leader_list",//审核人列表
            "checked_leader_list",//已经通过审核人的列表
            "prev_leader",//上一审核人
            "next_leader",//下一审核人
        );
    }
}

我定义了两个方法来对字符串和数组进行转换和切割,封装代替了 implode explode

然后定义MyQueue 来作为链表操作的类。具体使用的方法如下:

<?php
$obj=new MyQueue("1,2,3,4,5","1,2,3");//生成一个队列链表
$result=$obj->forward()->get_param();//指针向前移动
// $result=$obj->back_off()->get_param();//指针向后移动
var_dump($result);
?>

使用起来是很方便的。小小的骄傲一下,嘿嘿。

原文地址:https://www.cnblogs.com/lizhaoyao/p/6609826.html