rosavox/lib/alveolata/observer/functions.php
2025-05-23 07:33:29 +00:00

73 lines
897 B
PHP

<?php
namespace alveolata\observer;
/**
*/
class struct {
/**
* @var array {map<string,any>}
*/
public $listeners;
/**
* @array {map<string,any>}
*/
public function __construct(
array $listeners
)
{
$this->listeners = $listeners;
}
}
/**
* @return struct
*/
function make(
) : struct
{
return (
new struct([])
);
}
/**
* @param struct $subject
* @param \Closure $procedure {function<any,void>}
*/
function register(
struct $subject,
\Closure $procedure
) : string
{
$count = count($subject->listeners);
$id = sprintf('listener_%d', $count);
$subject->listeners[$id] = $procedure;
return $id;
}
/**
* @param struct $subject
* @param mixed $data {any}
*/
function notify(
struct $subject,
$data
)/* : void*/
{
$result = [];
foreach ($subject->listeners as $id => $procedure) {
$result[$id] = $procedure($data);
}
return $result;
}
?>