1
|
|
|
<?php |
2
|
|
|
namespace ReCaptcha2\Captcha; |
3
|
|
|
|
4
|
|
|
use Zend\Http\Response as HttpResponse; |
5
|
|
|
|
6
|
|
|
class Result |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var boolean |
10
|
|
|
*/ |
11
|
|
|
protected $status = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $errorCodes = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param HttpResponse $httpResponse |
20
|
|
|
*/ |
21
|
|
|
public function __construct(HttpResponse $httpResponse = null) |
22
|
|
|
{ |
23
|
|
|
if ($httpResponse !== null) { |
24
|
|
|
$this->setFromHttpResponse($httpResponse); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param boolean $status |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
|
|
public function setStatus($status) |
33
|
|
|
{ |
34
|
|
|
$this->status = (boolean)$status; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
public function getStatus() |
42
|
|
|
{ |
43
|
|
|
return $this->status; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public function isValid() |
50
|
|
|
{ |
51
|
|
|
return $this->getStatus(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $errorCodes |
56
|
|
|
* @return Response |
57
|
|
|
*/ |
58
|
|
|
public function setErrorCodes($errorCodes) |
59
|
|
|
{ |
60
|
|
|
if (is_string($errorCodes)) { |
61
|
|
|
$errorCodes = [$errorCodes]; |
62
|
|
|
} |
63
|
|
|
$this->errorCodes = $errorCodes; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getErrorCodes() |
72
|
|
|
{ |
73
|
|
|
return $this->errorCodes; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param HttpResponse $response |
78
|
|
|
* @return Response |
79
|
|
|
*/ |
80
|
|
|
public function setFromHttpResponse(HttpResponse $response) |
81
|
|
|
{ |
82
|
|
|
$body = $response->getBody(); |
83
|
|
|
|
84
|
|
|
$parts = json_decode($body, true); |
85
|
|
|
|
86
|
|
|
$status = false; |
87
|
|
|
$errorCodes = []; |
88
|
|
|
|
89
|
|
|
if (is_array($parts) && array_key_exists('success', $parts)) { |
90
|
|
|
$status = $parts['success']; |
91
|
|
|
if (array_key_exists('error-codes', $parts)) { |
92
|
|
|
$errorCodes = $parts['error-codes']; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->setStatus($status); |
97
|
|
|
$this->setErrorCodes($errorCodes); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|