2014-08-14 18:46:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract mapper for model
|
|
|
|
*/
|
|
|
|
abstract class EchoAbstractMapper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo database factory
|
|
|
|
* @var MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected $dbFactory;
|
|
|
|
|
2014-08-07 19:10:37 +00:00
|
|
|
/**
|
|
|
|
* Event listeners for method like insert/delete
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[]
|
2014-08-07 19:10:37 +00:00
|
|
|
*/
|
|
|
|
protected $listeners;
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
/**
|
2015-11-12 22:10:52 +00:00
|
|
|
* @param MWEchoDbFactory|null $dbFactory
|
2014-08-14 18:46:26 +00:00
|
|
|
*/
|
|
|
|
public function __construct( MWEchoDbFactory $dbFactory = null ) {
|
|
|
|
if ( $dbFactory === null ) {
|
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
|
|
|
}
|
|
|
|
$this->dbFactory = $dbFactory;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:10:37 +00:00
|
|
|
/**
|
|
|
|
* Attach a listener
|
|
|
|
*
|
|
|
|
* @param string $method Method name
|
|
|
|
* @param string $key Identification of the callable
|
|
|
|
* @param callable $callable
|
2015-11-12 22:10:52 +00:00
|
|
|
* @throws MWException
|
2014-08-07 19:10:37 +00:00
|
|
|
*/
|
|
|
|
public function attachListener( $method, $key, $callable ) {
|
|
|
|
if ( !method_exists( $this, $method ) ) {
|
|
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
|
|
}
|
|
|
|
if ( !isset( $this->listeners[$method] ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->listeners[$method] = [];
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param string $method
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return callable[]
|
2015-11-12 22:10:52 +00:00
|
|
|
* @throws MWException
|
2014-08-07 19:10:37 +00:00
|
|
|
*/
|
|
|
|
public function getMethodListeners( $method ) {
|
|
|
|
if ( !method_exists( $this, $method ) ) {
|
|
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
|
|
}
|
|
|
|
if ( isset( $this->listeners[$method] ) ) {
|
|
|
|
return $this->listeners[$method];
|
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
}
|