1 <?php
2 namespace DevStrefa\ReCaptcha;
3
4 /**
5 * Response class is used to store response from google reCaptcha service.
6 *
7 * @author Cichy <d3ut3r@gmail.com>
8 * @license https://opensource.org/licenses/MIT MIT
9 * @version 1.0.0
10 */
11 class Response
12 {
13 /**
14 *
15 * @var boolean TRUE if Captcha was Solved FALSE if not
16 */
17 private $success;
18 /**
19 *
20 * @var string timestamp in ISO format yyyy-MM-dd'T'HH:mm:ssZZ
21 */
22 private $date;
23
24 /**
25 *
26 * @var string host name
27 */
28 private $hostname;
29
30 /**
31 *
32 * @var array Array of errors
33 */
34 private $errors;
35
36 /**
37 *
38 * @var string Raw JSON string form google service
39 */
40 private $raw;
41
42 public function __construct($jsonData)
43 {
44 $responseObject=json_decode((string)$jsonData, FALSE, 3);
45
46 if (FALSE === is_object($responseObject))
47 {
48 throw new \RuntimeException('Invalid format of response (it\'s not JSON as expected)');
49 }
50
51 $this->raw = $jsonData;
52 $this->success=isset($responseObject->success) ? (boolean)$responseObject->success : FALSE;
53 $this->date=isset($responseObject->challenge_ts) ? $responseObject->challenge_ts : '';
54 $this->hostname=isset($responseObject->hostname) ? $responseObject->hostname : '';
55 $this->errors=isset($responseObject->{'error-codes'}) ? $responseObject->{'error-codes'} : array();
56
57
58 }
59
60 /**
61 * Return PHP DateTIme object of challenge timestamp from google response, if this response do not contain challenge_ts field it will return current date and time
62 * @return \DateTime Date object of challenge timeStamp
63 */
64 public function getDate()
65 {
66 return new \DateTime($this->date);
67 }
68
69 /**
70 *
71 * @return String Hostname
72 */
73 public function getHostname()
74 {
75 return $this->hostname;
76 }
77
78 /**
79 *
80 * @return array Array of errors
81 */
82 public function getErrors()
83 {
84 return (array)$this->errors;
85 }
86
87 /**
88 *
89 * @return boolean Result of challenge
90 */
91 public function isSuccess()
92 {
93 return (boolean)$this->success;
94 }
95
96 /**
97 *
98 * @return string Raw JSON string
99 */
100 public function getRaw()
101 {
102 return $this->raw;
103 }
104
105
106
107 }