mediawiki-extensions-Echo/includes/formatters/EchoEventFormatter.php
Kunal Mehta 74b50cad30 Move some boilerplate code into EchoEventFormatter
The conversion of EchoEvent into a EchoEventPresentationModel is now
done by EchoFlyoutFormatter, instead of having each subclass do it. It
also does the canRender() check so subclasses don't need to worry about
it.

The subclasses no longer have access to the underlying EchoEvent object,
so the timestamp is exposed in EchoEventPresentationModel.

Change-Id: I7f0a650373eebac7aa2231b1795b51a6d031ad67
2015-10-29 21:22:54 +00:00

39 lines
1,011 B
PHP

<?php
/**
* Abstract class that each "formatter" should implement.
*
* A formatter is an output type, example formatters would be:
* * Special:Notifications
* * HTML email
* * plaintext email
*
* The formatter does not maintain any state except for the
* arguments passed in the constructor (user and language)
*/
abstract class EchoEventFormatter {
public function __construct( User $user, Language $language ) {
$this->user = $user;
$this->language = $language;
}
/**
* @param EchoEvent $event
* @return string|bool Output format depends on implementation, false if it cannot be formatted
*/
final public function format( EchoEvent $event ) {
$model = EchoEventPresentationModel::factory( $event, $this->language, $this->user );
if ( !$model->canRender() ) {
return false;
}
return $this->formatModel( $model );
}
/**
* @param EchoEventPresentationModel $model
* @return string
*/
abstract protected function formatModel( EchoEventPresentationModel $model );
}