Debian Codegraph @

includes/auth/ButtonAuthenticationRequest.php

raw symbols SCIP
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 MediaWiki\Language\RawMessage;
25 use MediaWiki\Message\Message;
26
27 /**
28 * This is an authentication request that just implements a simple button.
29 * @stable to extend
30 * @ingroup Auth
31 * @since 1.27
32 */
33 #[\AllowDynamicProperties]
34 class ButtonAuthenticationRequest extends AuthenticationRequest {
35 /** @var string */
36 protected $name;
37
38 /** @var Message */
39 protected $label;
40
41 /** @var Message */
42 protected $help;
43
44 /**
45 * @stable to call
46 * @param string $name Button name
47 * @param Message $label Button label
48 * @param Message $help Button help
49 * @param bool $required The button is required for authentication to proceed.
50 */
51 public function __construct( $name, Message $label, Message $help, $required = false ) {
52 $this->name = $name;
53 $this->label = $label;
54 $this->help = $help;
55 $this->required = $required ? self::REQUIRED : self::OPTIONAL;
56 }
57
58 /**
59 * @inheritDoc
60 * @stable to override
61 */
62 public function getUniqueId() {
63 return parent::getUniqueId() . ':' . $this->name;
64 }
65
66 /**
67 * @inheritDoc
68 * @stable to override
69 */
70 public function getFieldInfo() {
71 return [
72 $this->name => [
73 'type' => 'button',
74 'label' => $this->label,
75 'help' => $this->help,
76 ]
77 ];
78 }
79
80 /**
81 * Fetch a ButtonAuthenticationRequest or subclass by name
82 * @param AuthenticationRequest[] $reqs Requests to search
83 * @param string $name Name to look for
84 * @return ButtonAuthenticationRequest|null Returns null if there is not
85 * exactly one matching request.
86 */
87 public static function getRequestByName( array $reqs, $name ) {
88 $requests = array_filter( $reqs, static function ( $req ) use ( $name ) {
89 return $req instanceof ButtonAuthenticationRequest && $req->name === $name;
90 } );
91 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
92 return count( $requests ) === 1 ? reset( $requests ) : null;
93 }
94
95 /**
96 * @codeCoverageIgnore
97 * @stable to override
98 * @param array $data
99 * @return AuthenticationRequest|static
100 */
101 public static function __set_state( $data ) {
102 if ( !isset( $data['label'] ) ) {
103 $data['label'] = new RawMessage( '$1', $data['name'] );
104 } elseif ( is_string( $data['label'] ) ) {
105 $data['label'] = new Message( $data['label'] );
106 } elseif ( is_array( $data['label'] ) && $data['label'] ) {
107 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
108 $data['label'] = Message::newFromKey( ...$data['label'] );
109 }
110 if ( !isset( $data['help'] ) ) {
111 $data['help'] = new RawMessage( '$1', $data['name'] );
112 } elseif ( is_string( $data['help'] ) ) {
113 $data['help'] = new Message( $data['help'] );
114 } elseif ( is_array( $data['help'] ) && $data['help'] ) {
115 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
116 $data['help'] = Message::newFromKey( ...$data['help'] );
117 }
118 $ret = new static( $data['name'], $data['label'], $data['help'] );
119 foreach ( $data as $k => $v ) {
120 $ret->$k = $v;
121 }
122 return $ret;
123 }
124 }