Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
12 / 12
Event
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
12 / 12
 fire
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
4 / 4
 on
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 validateEvent
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
<?php declare(strict_types=1);
namespace Aviat\Kilo;
class Event {
    use Traits\ConstList;
    // ------------------------------------------------------------------------
    // Valid Events
    // ------------------------------------------------------------------------
    public const INPUT_KEY = 'INPUT_KEY';
    public const PAGE_CHANGE = 'PAGE_CHANGE';
    public const MOVE_CURSOR = 'MOVE_CURSOR';
    public const QUIT_ATTEMPT = 'QUIT_ATTEMPT';
    // Mapping of events to handlers
    private static $subscribeMap = [];
    public static function fire(string $eventName, $value): void
    {
        static::validateEvent($eventName);
        if (array_key_exists($eventName, static::$subscribeMap))
        {
            foreach (static::$subscribeMap[$eventName] as $fn)
            {
                $fn($value);
            }
        }
    }
    public static function on(string $eventName, callable $fn): void
    {
        static::validateEvent($eventName);
        if ( ! array_key_exists($eventName, static::$subscribeMap))
        {
            static::$subscribeMap[$eventName] = [];
        }
        if ( ! in_array($fn, static::$subscribeMap[$eventName], TRUE))
        {
            static::$subscribeMap[$eventName][] = $fn;
        }
    }
    private static function validateEvent(string $eventName): void
    {
        $validEvents = self::getConstList();
        if ( ! array_key_exists($eventName, $validEvents))
        {
            throw new \InvalidArgumentException("Invalid event '{$eventName}'. Event const must exist in Aviat\\Kilo\\Event.");
        }
    }
}