| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\EditPage; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Message\Message; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class IntroMessageList { |
| 14 |
|
| 15 |
public array $list = []; |
| 16 |
|
| 17 |
private int $frames; |
| 18 |
|
| 19 |
private array $skip; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( int $frames, array $skip = [] ) { |
| 28 |
if ( !in_array( $frames, [ IntroMessageBuilder::MORE_FRAMES, IntroMessageBuilder::LESS_FRAMES ], true ) ) { |
| 29 |
throw new InvalidArgumentException( "Expected MORE_FRAMES or LESS_FRAMES" ); |
| 30 |
} |
| 31 |
$this->frames = $frames; |
| 32 |
$this->skip = array_fill_keys( $skip, true ); |
| 33 |
} |
| 34 |
|
| 35 |
private function wrap( string $html, string $wrap ): string { |
| 36 |
if ( $this->frames === IntroMessageBuilder::LESS_FRAMES ) { |
| 37 |
return $html; |
| 38 |
} |
| 39 |
return str_replace( '$1', $html, $wrap ); |
| 40 |
} |
| 41 |
|
| 42 |
public function add( Message $msg, string $wrap = '$1' ): void { |
| 43 |
if ( !$msg->isDisabled() && !isset( $this->skip[ $msg->getKey() ] ) ) { |
| 44 |
$this->addWithKey( $msg->getKey(), $msg->parse(), $wrap ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function addWithKey( string $key, string $html, string $wrap = '$1' ): void { |
| 49 |
if ( $html === '' ) { |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
if ( !isset( $this->skip[$key] ) ) { |
| 54 |
$this->list[$key] = $this->wrap( $html, $wrap ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public function getList(): array { |
| 59 |
return $this->list; |
| 60 |
} |
| 61 |
} |