<?php

declare(strict_types=1);

namespace app\common\arw\adjfut\src\Traits;


/**
 * 事件类
 */
trait Event
{
    /**
     * 事件
     *
     * @var array
     * @date 2022-12-28
     * @example
     * @author arw
     * @since 1.0.0
     */
    private $event = [];
    /**
     * 事件
     *
     * @param string $type
     * @param callable $cb
     * @return self
     * @date 2022-12-28
     * @example
     * @author arw
     * @since 1.0.0
     */
    private function event(string $type, callable $cb): self
    {
        $this->event[strtoupper($type)] = $cb;
        return $this;
    }

    /**
     * 事件触发器
     *
     * @param string $type
     * @param array $args
     * @param boolean $withThis
     * @return mixed
     * @date 2022-12-28
     * @example
     * @author arw
     * @since 1.0.0
     */
    private function trigger(string $type, array $args = [], bool $withThis = true)
    {
        $type = strtoupper($type);
        if (isset($this->event[$type])) {
            if ($withThis) {
                array_unshift($args, $this);
            }
            return call_user_func_array($this->event[$type], $args);
        }
    }
}
