Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / auth / ButtonAuthenticationRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Auth
20 */
21
22 namespace MediaWiki\Auth;
23
24 use Message;
25
26 /**
27 * This is an authentication request that just implements a simple button.
28 * @ingroup Auth
29 * @since 1.27
30 */
31 class ButtonAuthenticationRequest extends AuthenticationRequest {
32 /** @var string */
33 protected $name;
34
35 /** @var Message */
36 protected $label;
37
38 /** @var Message */
39 protected $help;
40
41 /**
42 * @param string $name Button name
43 * @param Message $label Button label
44 * @param Message $help Button help
45 * @param bool $required The button is required for authentication to proceed.
46 */
47 public function __construct( $name, Message $label, Message $help, $required = false ) {
48 $this->name = $name;
49 $this->label = $label;
50 $this->help = $help;
51 $this->required = $required ? self::REQUIRED : self::OPTIONAL;
52 }
53
54 public function getUniqueId() {
55 return parent::getUniqueId() . ':' . $this->name;
56 }
57
58 public function getFieldInfo() {
59 return [
60 $this->name => [
61 'type' => 'button',
62 'label' => $this->label,
63 'help' => $this->help,
64 ]
65 ];
66 }
67
68 /**
69 * Fetch a ButtonAuthenticationRequest or subclass by name
70 * @param AuthenticationRequest[] $reqs Requests to search
71 * @param string $name Name to look for
72 * @return ButtonAuthenticationRequest|null Returns null if there is not
73 * exactly one matching request.
74 */
75 public static function getRequestByName( array $reqs, $name ) {
76 $requests = array_filter( $reqs, function ( $req ) use ( $name ) {
77 return $req instanceof ButtonAuthenticationRequest && $req->name === $name;
78 } );
79 return count( $requests ) === 1 ? reset( $requests ) : null;
80 }
81
82 /**
83 * @codeCoverageIgnore
84 * @param array $data
85 * @return AuthenticationRequest|static
86 */
87 public static function __set_state( $data ) {
88 if ( !isset( $data['label'] ) ) {
89 $data['label'] = new \RawMessage( '$1', $data['name'] );
90 } elseif ( is_string( $data['label'] ) ) {
91 $data['label'] = new \Message( $data['label'] );
92 } elseif ( is_array( $data['label'] ) ) {
93 $data['label'] = call_user_func_array( 'Message::newFromKey', $data['label'] );
94 }
95 if ( !isset( $data['help'] ) ) {
96 $data['help'] = new \RawMessage( '$1', $data['name'] );
97 } elseif ( is_string( $data['help'] ) ) {
98 $data['help'] = new \Message( $data['help'] );
99 } elseif ( is_array( $data['help'] ) ) {
100 $data['help'] = call_user_func_array( 'Message::newFromKey', $data['help'] );
101 }
102 $ret = new static( $data['name'], $data['label'], $data['help'] );
103 foreach ( $data as $k => $v ) {
104 $ret->$k = $v;
105 }
106 return $ret;
107 }
108 }