kamerk22 /
AmazonGiftCode
| 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 CreateResponse |
||
| 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 gcClaimCode |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $_claim_code; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Amazon Gift Card amount |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $_value; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Amazon Gift Card currency |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $_currency; |
||
| 50 | /** |
||
| 51 | * Amazon Gift Card status |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $_status; |
||
| 56 | /** |
||
| 57 | * Amazon Gift Card Expiration Date |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $_expiration_date; |
||
| 62 | /** |
||
| 63 | * Amazon Gift Card Expiration Date |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $_card_status; |
||
| 68 | /** |
||
| 69 | * Amazon Gift Card Raw JSON |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $_raw_json; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Response constructor. |
||
| 77 | * @param $jsonResponse |
||
| 78 | */ |
||
| 79 | public function __construct($jsonResponse) |
||
| 80 | { |
||
| 81 | $this->_raw_json = $jsonResponse; |
||
| 82 | $this->_status = TRUE; |
||
|
0 ignored issues
–
show
|
|||
| 83 | $this->parseJsonResponse($jsonResponse); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getId(): string |
||
| 91 | { |
||
| 92 | return $this->_id; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getCreationRequestId(): string |
||
| 99 | { |
||
| 100 | return $this->_creation_request_id; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getClaimCode(): string |
||
| 108 | { |
||
| 109 | return $this->_claim_code; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getValue(): string |
||
| 116 | { |
||
| 117 | return $this->_value; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getCurrency(): string |
||
| 124 | { |
||
| 125 | return $this->_currency; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getStatus(): string |
||
| 132 | { |
||
| 133 | return $this->_status; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getExpirationDate(): string |
||
| 140 | { |
||
| 141 | return $this->_expiration_date; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getCardStatus(): string |
||
| 148 | { |
||
| 149 | return $this->_card_status; |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getRawJson(): string |
||
| 157 | { |
||
| 158 | return json_encode($this->_raw_json); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $jsonResponse |
||
| 163 | * @return CreateResponse |
||
| 164 | */ |
||
| 165 | public function parseJsonResponse($jsonResponse): self |
||
| 166 | { |
||
| 167 | if (!is_array($jsonResponse)) { |
||
| 168 | throw new \RuntimeException('Response must be a scalar value'); |
||
| 169 | } |
||
| 170 | if (array_key_exists('gcId', $jsonResponse)) { |
||
| 171 | $this->_id = $jsonResponse['gcId']; |
||
| 172 | } |
||
| 173 | if (array_key_exists('creationRequestId', $jsonResponse)) { |
||
| 174 | $this->_creation_request_id = $jsonResponse['creationRequestId']; |
||
| 175 | } |
||
| 176 | if (array_key_exists('gcClaimCode', $jsonResponse)) { |
||
| 177 | $this->_claim_code = $jsonResponse['gcClaimCode']; |
||
| 178 | } |
||
| 179 | if (array_key_exists('amount', $jsonResponse['cardInfo']['value'])) { |
||
| 180 | $this->_value = $jsonResponse['cardInfo']['value']['amount']; |
||
| 181 | } |
||
| 182 | if (array_key_exists('currencyCode', $jsonResponse['cardInfo']['value'])) { |
||
| 183 | $this->_currency = $jsonResponse['cardInfo']['value']['currencyCode']; |
||
| 184 | } |
||
| 185 | if (array_key_exists('gcExpirationDate', $jsonResponse)) { |
||
| 186 | $this->_expiration_date = $jsonResponse['gcExpirationDate']; |
||
| 187 | } |
||
| 188 | if (array_key_exists('cardStatus', $jsonResponse['cardInfo'])) { |
||
| 189 | $this->_card_status = $jsonResponse['cardInfo']['cardStatus']; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $this; |
||
| 193 | |||
| 194 | } |
||
| 195 | |||
| 196 | } |
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.