HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/vendor/shetabit/multipay/src/EventEmitter.php
<?php

namespace Shetabit\Multipay;

class EventEmitter
{
    /**
     * List of listeners.
     *
     * @description a pair of $event => [array of listeners]
     *
     * @var array
     */
    private $listeners = [];

    /**
     * Add new listener fo given event.
     *
     * @param string $event
     * @param callable $listener
     *
     * @return void
     */
    public function addEventListener(string $event, callable $listener)
    {
        if (empty($this->listeners[$event]) || !is_array($this->listeners[$event])) {
            $this->listeners[$event] = [];
        }

        array_push($this->listeners[$event], $listener);
    }

    /**
     * Remove given listener from a specefic event.
     * if we call this method without listener, it will totaly remove the given event and all of its listeners.
     *
     * @param string $event
     * @param callable $listener
     *
     * @return void
     */
    public function removeEventListener(string $event, callable $listener = null)
    {
        if (empty($this->listeners[$event])) {
            return;
        }

        // remove the event and all of its listeners
        if (empty($listener)) {
            unset($this->listeners[$event]);

            return;
        }

        // remove only the given listener if exists
        $listenerIndex = array_search($listener, $this->listeners[$event]);

        if ($listenerIndex !== false) {
            unset($this->listeners[$event][$listenerIndex]);
        }
    }

    /**
     * Run event listeners.
     *
     * @param string $event
     * @param array ...$arguments
     *
     * @return void
     */
    public function dispatch(string $event, ...$arguments)
    {
        $listeners = $this->listeners;

        if (empty($listeners[$event])) {
            return;
        }

        array_walk($listeners[$event], function ($listener) use ($arguments) {
            call_user_func_array($listener, $arguments);
        });
    }

    /**
     * Call events by their name.
     *
     * @param string $name
     * @param array $arguments
     *
     * @return void
     */
    public function __call($name, $arguments)
    {
        $this->dispatch($name, $arguments);
    }
}