1 | <?php namespace Arcanedev\NoCaptcha\Utilities; |
||
13 | abstract class AbstractResponse implements Arrayable, Jsonable, JsonSerializable |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Constants |
||
17 | | ----------------------------------------------------------------- |
||
18 | */ |
||
19 | |||
20 | /** |
||
21 | * Did not receive a 200 from the service |
||
22 | */ |
||
23 | const E_BAD_RESPONSE = 'bad-response'; |
||
24 | |||
25 | /** |
||
26 | * ReCAPTCHA response not provided |
||
27 | */ |
||
28 | const E_MISSING_INPUT_RESPONSE = 'missing-input-response'; |
||
29 | |||
30 | /* ----------------------------------------------------------------- |
||
31 | | Properties |
||
32 | | ----------------------------------------------------------------- |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * Success or failure. |
||
37 | * |
||
38 | * @var boolean |
||
39 | */ |
||
40 | protected $success = false; |
||
41 | |||
42 | /** |
||
43 | * Error code strings. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $errorCodes = []; |
||
48 | |||
49 | /** |
||
50 | * The hostname of the site where the reCAPTCHA was solved. |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $hostname; |
||
54 | |||
55 | /** |
||
56 | * Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $challengeTs; |
||
61 | |||
62 | /** |
||
63 | * APK package name |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $apkPackageName; |
||
68 | |||
69 | /* ----------------------------------------------------------------- |
||
70 | | Constructor |
||
71 | | ----------------------------------------------------------------- |
||
72 | */ |
||
73 | |||
74 | /** |
||
75 | * Response constructor. |
||
76 | * |
||
77 | * @param bool $success |
||
78 | * @param array $errorCodes |
||
79 | * @param string|null $hostname |
||
80 | * @param string|null $challengeTs |
||
81 | * @param string|null $apkPackageName |
||
82 | * @param float|null $score |
||
83 | * @param string|null $action |
||
84 | */ |
||
85 | 100 | public function __construct($success, array $errorCodes = [], $hostname = null, $challengeTs = null, $apkPackageName = null, $score = null, $action = null) |
|
93 | |||
94 | /* ----------------------------------------------------------------- |
||
95 | | Getters |
||
96 | | ----------------------------------------------------------------- |
||
97 | */ |
||
98 | |||
99 | /** |
||
100 | * Get error codes. |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | 28 | public function getErrorCodes() |
|
108 | |||
109 | /** |
||
110 | * Get hostname. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 32 | public function getHostname() |
|
118 | |||
119 | /** |
||
120 | * Get challenge timestamp |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 24 | public function getChallengeTs() |
|
128 | |||
129 | /** |
||
130 | * Get APK package name |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 24 | public function getApkPackageName() |
|
138 | |||
139 | /* ----------------------------------------------------------------- |
||
140 | | Main Methods |
||
141 | | ----------------------------------------------------------------- |
||
142 | */ |
||
143 | |||
144 | /** |
||
145 | * Build the response from the expected JSON returned by the service. |
||
146 | * |
||
147 | * @param string $json |
||
148 | * |
||
149 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse |
||
150 | */ |
||
151 | 44 | public static function fromJson($json) |
|
160 | |||
161 | /** |
||
162 | * Build the response from an array. |
||
163 | * |
||
164 | * @param array $array |
||
165 | * |
||
166 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
167 | */ |
||
168 | abstract public static function fromArray(array $array); |
||
169 | |||
170 | /** |
||
171 | * Convert the response object to array. |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | abstract public function toArray(); |
||
176 | |||
177 | /** |
||
178 | * Convert the object to its JSON representation. |
||
179 | * |
||
180 | * @param int $options |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | 8 | public function toJson($options = 0) |
|
188 | |||
189 | /** |
||
190 | * Convert the response object to array. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | 8 | public function jsonSerialize() |
|
198 | |||
199 | /* ----------------------------------------------------------------- |
||
200 | | Check Methods |
||
201 | | ----------------------------------------------------------------- |
||
202 | */ |
||
203 | |||
204 | /** |
||
205 | * Check if the response is successful. |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | 84 | public function isSuccess() |
|
213 | |||
214 | /** |
||
215 | * Check the hostname. |
||
216 | * |
||
217 | * @param string $hostname |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 8 | public function isHostname($hostname) |
|
225 | } |
||
226 |