Skip to content

Commit 2c8dfee

Browse files
committed
Response: getRawResponse and throw on decode error
1 parent 98c76e8 commit 2c8dfee

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/DatingVIP/API/Response.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace DatingVIP\API;
1414

15+
use RuntimeException;
16+
1517
class Response
1618
{
1719
const KEY_RESULT = 'result';
@@ -32,19 +34,23 @@ class Response
3234
*/
3335
private $format = 'json';
3436

37+
private $response = '';
38+
3539
/**
3640
* Default API response constructor
3741
* - optionally set response.
3842
*
3943
* @param mixed $data [= '']
4044
* @param mixed $format [= 'json']
45+
* @throws RuntimeException
4146
*/
4247
public function __construct($data = '', $format = 'json')
4348
{
4449
if (!empty($format)) {
4550
$this->format = $format;
4651
}
47-
$this->set($data);
52+
$this->response = $data;
53+
$this->set($this->response);
4854
}
4955

5056
/**
@@ -71,12 +77,16 @@ public function get()
7177
return $result;
7278
}
7379

80+
public function getRawResponse () {
81+
return $this->response;
82+
}
83+
7484
/**
7585
* Set API response
7686
* - decode from JSON.
7787
*
7888
* @param string $data
79-
*
89+
* @throws RuntimeException
8090
* @return bool
8191
*/
8292
public function set($data)
@@ -92,6 +102,9 @@ public function set($data)
92102
default:
93103
$this->data = json_decode($data, true) ?: [];
94104
$result = json_last_error() == JSON_ERROR_NONE;
105+
if (empty ($result)) {
106+
throw new RuntimeException ("Error decoding: " . json_last_error_msg ());
107+
}
95108
break;
96109
}
97110

0 commit comments

Comments
 (0)