1
1
<?php
2
+
2
3
/**
3
4
* This is a PHP library that handles calling reCAPTCHA.
4
5
*
39
40
*/
40
41
class Response
41
42
{
42
- /**
43
- * Success or failure.
44
- * @var boolean
45
- */
46
- private $ success = false ;
47
-
48
- /**
49
- * Error code strings.
50
- * @var array
51
- */
52
- private $ errorCodes = array ();
53
-
54
- /**
55
- * The hostname of the site where the reCAPTCHA was solved.
56
- * @var string
57
- */
58
- private $ hostname ;
59
-
60
- /**
61
- * Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
62
- * @var string
63
- */
64
- private $ challengeTs ;
65
-
66
- /**
67
- * APK package name
68
- * @var string
69
- */
70
- private $ apkPackageName ;
71
-
72
- /**
73
- * Score assigned to the request
74
- * @var float
75
- */
76
- private $ score ;
77
-
78
- /**
79
- * Action as specified by the page
80
- * @var string
81
- */
82
- private $ action ;
83
-
84
43
/**
85
44
* Build the response from the expected JSON returned by the service.
86
- *
87
- * @param string $json
88
- * @return \ReCaptcha\Response
89
45
*/
90
- public static function fromJson (string $ json )
46
+ public static function fromJson (string $ json ): Response
91
47
{
92
48
$ responseData = json_decode ($ json , true );
93
49
94
50
if (!$ responseData ) {
95
- return new Response (false , array ( ReCaptcha::E_INVALID_JSON ) );
51
+ return new Response (false , [ ReCaptcha::E_INVALID_JSON ] );
96
52
}
97
53
98
54
$ hostname = isset ($ responseData ['hostname ' ]) ? $ responseData ['hostname ' ] : '' ;
@@ -102,117 +58,97 @@ public static function fromJson(string $json)
102
58
$ action = isset ($ responseData ['action ' ]) ? $ responseData ['action ' ] : '' ;
103
59
104
60
if (isset ($ responseData ['success ' ]) && $ responseData ['success ' ] == true ) {
105
- return new Response (true , array () , $ hostname , $ challengeTs , $ apkPackageName , $ score , $ action );
61
+ return new Response (true , [] , $ hostname , $ challengeTs , $ apkPackageName , $ score , $ action );
106
62
}
107
63
108
64
if (isset ($ responseData ['error-codes ' ]) && is_array ($ responseData ['error-codes ' ])) {
109
65
return new Response (false , $ responseData ['error-codes ' ], $ hostname , $ challengeTs , $ apkPackageName , $ score , $ action );
110
66
}
111
67
112
- return new Response (false , array ( ReCaptcha::E_UNKNOWN_ERROR ) , $ hostname , $ challengeTs , $ apkPackageName , $ score , $ action );
68
+ return new Response (false , [ ReCaptcha::E_UNKNOWN_ERROR ] , $ hostname , $ challengeTs , $ apkPackageName , $ score , $ action );
113
69
}
114
70
115
71
/**
116
72
* Constructor.
117
73
*
118
- * @param boolean $success
119
- * @param string $hostname
120
- * @param string $challengeTs
121
- * @param string $apkPackageName
122
- * @param float $score
123
- * @param string $action
124
- * @param array $errorCodes
125
- */
126
- public function __construct (bool $ success , array $ errorCodes = [], string $ hostname = '' , string $ challengeTs = '' , string $ apkPackageName = '' , ?float $ score = null , string $ action = '' )
74
+ * @param boolean $success Success or failure
75
+ * @param string $hostname The hostname of the site where the reCAPTCHA was solved
76
+ * @param string $challengeTs Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
77
+ * @param string $apkPackageName APK package name
78
+ * @param ? float $score Score assigned to the request
79
+ * @param string $action Action as specified by the page
80
+ * @param array $errorCodes Error code strings.
81
+ */
82
+ public function __construct (private bool $ success , private array $ errorCodes = [], private string $ hostname = '' , private string $ challengeTs = '' , private string $ apkPackageName = '' , private ?float $ score = null , private string $ action = '' )
127
83
{
128
- $ this ->success = $ success ;
129
- $ this ->hostname = $ hostname ;
130
- $ this ->challengeTs = $ challengeTs ;
131
- $ this ->apkPackageName = $ apkPackageName ;
132
- $ this ->score = $ score ;
133
- $ this ->action = $ action ;
134
- $ this ->errorCodes = $ errorCodes ;
135
84
}
136
85
137
86
/**
138
87
* Is success?
139
- *
140
- * @return boolean
141
88
*/
142
- public function isSuccess ()
89
+ public function isSuccess (): bool
143
90
{
144
91
return $ this ->success ;
145
92
}
146
93
147
94
/**
148
95
* Get error codes.
149
- *
150
- * @return array
151
96
*/
152
- public function getErrorCodes ()
97
+ public function getErrorCodes (): array
153
98
{
154
99
return $ this ->errorCodes ;
155
100
}
156
101
157
102
/**
158
103
* Get hostname.
159
- *
160
- * @return string
161
104
*/
162
- public function getHostname ()
105
+ public function getHostname (): string
163
106
{
164
107
return $ this ->hostname ;
165
108
}
166
109
167
110
/**
168
111
* Get challenge timestamp
169
- *
170
- * @return string
171
112
*/
172
- public function getChallengeTs ()
113
+ public function getChallengeTs (): string
173
114
{
174
115
return $ this ->challengeTs ;
175
116
}
176
117
177
118
/**
178
119
* Get APK package name
179
- *
180
- * @return string
181
120
*/
182
- public function getApkPackageName ()
121
+ public function getApkPackageName (): string
183
122
{
184
123
return $ this ->apkPackageName ;
185
124
}
125
+
186
126
/**
187
127
* Get score
188
- *
189
- * @return float
190
128
*/
191
- public function getScore ()
129
+ public function getScore (): ? float
192
130
{
193
131
return $ this ->score ;
194
132
}
195
133
196
134
/**
197
135
* Get action
198
- *
199
- * @return string
200
136
*/
201
- public function getAction ()
137
+ public function getAction (): string
202
138
{
203
139
return $ this ->action ;
204
140
}
205
141
206
- public function toArray ()
142
+ public function toArray (): array
207
143
{
208
- return array (
144
+ return [
209
145
'success ' => $ this ->isSuccess (),
210
146
'hostname ' => $ this ->getHostname (),
211
147
'challenge_ts ' => $ this ->getChallengeTs (),
212
148
'apk_package_name ' => $ this ->getApkPackageName (),
213
149
'score ' => $ this ->getScore (),
214
150
'action ' => $ this ->getAction (),
215
151
'error-codes ' => $ this ->getErrorCodes (),
216
- ) ;
152
+ ] ;
217
153
}
218
154
}
0 commit comments