1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File holding the MenuFactory class |
4
|
|
|
* |
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 - 2015, Stephan Gambke |
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
9
|
|
|
* |
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
13
|
|
|
* later version. |
14
|
|
|
* |
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18
|
|
|
* details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU General Public License along |
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
* @file |
24
|
|
|
* @ingroup Skins |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Menu; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class MenuFactory |
31
|
|
|
* |
32
|
|
|
* @author Stephan Gambke |
33
|
|
|
* @since 1.0 |
34
|
|
|
* @ingroup Skins |
35
|
|
|
*/ |
36
|
|
|
class MenuFactory { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \Message|string|string[] $message |
40
|
|
|
* @param bool $forContent |
41
|
|
|
* |
42
|
|
|
* @throws \MWException |
43
|
|
|
* |
44
|
|
|
* @return Menu |
45
|
|
|
*/ |
46
|
|
|
public function getMenuFromMessage( $message, $forContent = false ) { |
47
|
|
|
|
48
|
|
|
if ( is_string( $message ) || is_array( $message ) ) { |
49
|
|
|
$message = \Message::newFromKey( $message ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->assert( is_a( $message, '\\Message' ), 'String, array of strings or Message object expected.', $message ); |
53
|
|
|
|
54
|
|
|
if ( $forContent ) { |
55
|
|
|
$message = $message->inContentLanguage(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ( !$message->exists() ) { |
59
|
|
|
return $this->getMenuFromMessageText( '', $forContent ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->getMenuFromMessageText( $message->text(), $forContent ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $text |
67
|
|
|
* @param bool $forContent |
68
|
|
|
* |
69
|
|
|
* @return Menu |
70
|
|
|
* @throws \MWException |
71
|
|
|
*/ |
72
|
|
|
public function getMenuFromMessageText( $text, $forContent = false ) { |
73
|
|
|
|
74
|
|
|
$this->assert( is_string( $text ), 'String expected.', $text ); |
75
|
|
|
|
76
|
|
|
$lines = explode( "\n", trim( $text ) ); |
77
|
|
|
|
78
|
|
|
return new MenuFromLines( $lines, $forContent ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $message |
83
|
|
|
* @throws \MWException |
84
|
|
|
*/ |
85
|
|
|
protected function assert( $condition, $message, $target ) { |
86
|
|
|
if ( !$condition ) { |
87
|
|
|
throw new \MWException( $message . ' Got ' . (is_object( $target ) ? get_class( $target ) : gettype( $target )) . '.' ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|