Debian Codegraph @

includes/editpage/IntroMessageList.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\EditPage;
4
5 use InvalidArgumentException;
6 use MediaWiki\Message\Message;
7
8 /**
9 * Encapsulates a list of edit form intro messages (as HTML) with their identifiers.
10 *
11 * @internal
12 */
13 class IntroMessageList {
14 /** @var array<string,string> */
15 public array $list = [];
16 /** @var int IntroMessageBuilder::MORE_FRAMES or IntroMessageBuilder::LESS_FRAMES */
17 private int $frames;
18 /** @var array<string,true> */
19 private array $skip;
20
21 /**
22 * @param int $frames Some intro messages come with optional wrapper frames.
23 * Pass IntroMessageBuilder::MORE_FRAMES to include the frames whenever possible,
24 * or IntroMessageBuilder::LESS_FRAMES to omit them whenever possible.
25 * @param string[] $skip Identifiers of messages not to generate
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 // Remove empty notices (T265798)
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 }