1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Part of the AmazonGiftCode package. |
||
5 | * Author: Kashyap Merai <[email protected]> |
||
6 | * |
||
7 | */ |
||
8 | |||
9 | |||
10 | namespace kamerk22\AmazonGiftCode\Response; |
||
11 | |||
12 | |||
13 | class CancelResponse |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Amazon Gift Card gcId. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $_id; |
||
22 | |||
23 | /** |
||
24 | * Amazon Gift Card creationRequestId |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $_creation_request_id; |
||
29 | |||
30 | /** |
||
31 | * Amazon Gift Card status |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $_status; |
||
36 | |||
37 | /** |
||
38 | * Amazon Gift Card Raw JSON |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $_raw_json; |
||
43 | |||
44 | /** |
||
45 | * Response constructor. |
||
46 | * @param $jsonResponse |
||
47 | */ |
||
48 | public function __construct($jsonResponse) |
||
49 | { |
||
50 | $this->_raw_json = $jsonResponse; |
||
51 | $this->_status = TRUE; |
||
0 ignored issues
–
show
|
|||
52 | $this->parseJsonResponse($jsonResponse); |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getId(): string |
||
60 | { |
||
61 | return $this->_id; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getCreationRequestId(): string |
||
68 | { |
||
69 | return $this->_creation_request_id; |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getStatus(): string |
||
77 | { |
||
78 | return $this->_status; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getRawJson(): string |
||
86 | { |
||
87 | return json_encode($this->_raw_json); |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @param $jsonResponse |
||
93 | * @return CancelResponse |
||
94 | */ |
||
95 | public function parseJsonResponse($jsonResponse): self |
||
96 | { |
||
97 | if (!is_array($jsonResponse)) { |
||
98 | throw new \RuntimeException('Response must be a scalar value'); |
||
99 | } |
||
100 | if (array_key_exists('gcId', $jsonResponse)) { |
||
101 | $this->_id = $jsonResponse['gcId']; |
||
102 | } |
||
103 | if (array_key_exists('creationRequestId', $jsonResponse)) { |
||
104 | $this->_creation_request_id = $jsonResponse['creationRequestId']; |
||
105 | } |
||
106 | |||
107 | return $this; |
||
108 | |||
109 | } |
||
110 | |||
111 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.