-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResponseHelper.php
More file actions
53 lines (46 loc) · 1.25 KB
/
ResponseHelper.php
File metadata and controls
53 lines (46 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class ResponseHelper
{
/**
* Converts json api response to formated object
*
* @param str $jsonString string in JSON format
* @param str $setResultFrom respose field from which result object param will be set
*
* @return stdClass
*
* $obj->code - api response code
* $obj->message - api response code
* $obj->cltrid - api client transaction id
* $obj->svtrid - api server transaction id
* $obj->result - other data from response or from $setResultFrom field if specified
*/
public static function fromJSON($jsonString, $setResultFrom = false)
{
$json = json_decode($jsonString, true);
$obj = new stdClass();
$obj->code = $json['code'];
$obj->message = $json['message'];
$obj->svtrid = $json['svtrid'];
$obj->cltrid = $json['cltrid'];
unset($json['code'], $json['message'], $json['svtrid'], $json['cltrid']);
if ($setResultFrom && isset($json[$setResultFrom])) {
$obj->result = $json[$setResultFrom];
} else {
$obj->result = $json;
}
return $obj;
}
/**
* Check if operation was succeed
*
* @param str $jsonString operation response
*
* @return bool
*/
public static function isSuccess($jsonString)
{
$json = ResponseHelper::fromJSON($jsonString);
return $json->code == 1000;
}
}