mediawiki-extensions-Echo/includes/Mapper/AbstractMapper.php
Andre Klapper 4da4dad97b Use explicit nullable type on parameter arguments (for PHP 8.4)
Implicitly marking parameter $... as nullable is deprecated in PHP
8.4. The explicit nullable type must be used instead.

Bug: T376276
Change-Id: I251cb37401c37242f493816b6f70ab61a64a4c32
2024-10-26 15:05:13 +02:00

74 lines
1.6 KiB
PHP

<?php
namespace MediaWiki\Extension\Notifications\Mapper;
use InvalidArgumentException;
use MediaWiki\Extension\Notifications\DbFactory;
/**
* Abstract mapper for model
*/
abstract class AbstractMapper {
/**
* Echo database factory
* @var DbFactory
*/
protected $dbFactory;
/**
* Event listeners for method like insert/delete
* @var array[]
*/
protected $listeners;
public function __construct( ?DbFactory $dbFactory = null ) {
$this->dbFactory = $dbFactory ?? DbFactory::newFromDefault();
}
/**
* Attach a listener
*
* @param string $method Method name
* @param string $key Identification of the callable
* @param callable $callable
*/
public function attachListener( $method, $key, $callable ) {
if ( !method_exists( $this, $method ) ) {
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
}
if ( !isset( $this->listeners[$method] ) ) {
$this->listeners[$method] = [];
}
$this->listeners[$method][$key] = $callable;
}
/**
* Detach a listener
*
* @param string $method Method name
* @param string $key identification of the callable
*/
public function detachListener( $method, $key ) {
if ( isset( $this->listeners[$method] ) ) {
unset( $this->listeners[$method][$key] );
}
}
/**
* Get the listener for a method
*
* @param string $method
* @return callable[]
*/
public function getMethodListeners( $method ) {
if ( !method_exists( $this, $method ) ) {
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
}
return $this->listeners[$method] ?? [];
}
}