Result.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace AlibabaCloud\Client\Result;
  3. use Countable;
  4. use Exception;
  5. use ArrayAccess;
  6. use IteratorAggregate;
  7. use InvalidArgumentException;
  8. use GuzzleHttp\Psr7\Response;
  9. use Psr\Http\Message\ResponseInterface;
  10. use AlibabaCloud\Client\Request\Request;
  11. use AlibabaCloud\Client\Traits\HasDataTrait;
  12. /**
  13. * Result from Alibaba Cloud
  14. *
  15. * @property string|null RequestId
  16. *
  17. * @package AlibabaCloud\Client\Result
  18. */
  19. class Result extends Response implements ArrayAccess, IteratorAggregate, Countable
  20. {
  21. use HasDataTrait;
  22. /**
  23. * Instance of the request.
  24. *
  25. * @var Request
  26. */
  27. protected $request;
  28. /**
  29. * Result constructor.
  30. *
  31. * @param ResponseInterface $response
  32. * @param Request $request
  33. */
  34. public function __construct(ResponseInterface $response, Request $request = null)
  35. {
  36. parent::__construct(
  37. $response->getStatusCode(),
  38. $response->getHeaders(),
  39. $response->getBody(),
  40. $response->getProtocolVersion(),
  41. $response->getReasonPhrase()
  42. );
  43. $this->request = $request;
  44. $this->resolveData();
  45. }
  46. private function resolveData()
  47. {
  48. $content = $this->getBody()->getContents();
  49. switch ($this->getRequestFormat()) {
  50. case 'JSON':
  51. $result_data = $this->jsonToArray($content);
  52. break;
  53. case 'XML':
  54. $result_data = $this->xmlToArray($content);
  55. break;
  56. case 'RAW':
  57. $result_data = $this->jsonToArray($content);
  58. break;
  59. default:
  60. $result_data = $this->jsonToArray($content);
  61. }
  62. if (!$result_data) {
  63. $result_data = [];
  64. }
  65. $this->dot($result_data);
  66. }
  67. /**
  68. * @return string
  69. */
  70. private function getRequestFormat()
  71. {
  72. return ($this->request instanceof Request)
  73. ? \strtoupper($this->request->format)
  74. : 'JSON';
  75. }
  76. /**
  77. * @param string $response
  78. *
  79. * @return array
  80. */
  81. private function jsonToArray($response)
  82. {
  83. try {
  84. return \GuzzleHttp\json_decode($response, true);
  85. } catch (InvalidArgumentException $exception) {
  86. return [];
  87. }
  88. }
  89. /**
  90. * @param string $string
  91. *
  92. * @return array
  93. */
  94. private function xmlToArray($string)
  95. {
  96. try {
  97. return json_decode(json_encode(simplexml_load_string($string)), true);
  98. } catch (Exception $exception) {
  99. return [];
  100. }
  101. }
  102. /**
  103. * @return string
  104. */
  105. public function __toString()
  106. {
  107. return (string)$this->getBody();
  108. }
  109. /**
  110. * @return Request
  111. */
  112. public function getRequest()
  113. {
  114. return $this->request;
  115. }
  116. /**
  117. * @codeCoverageIgnore
  118. * @return Response
  119. * @deprecated
  120. */
  121. public function getResponse()
  122. {
  123. return $this;
  124. }
  125. /**
  126. * @return bool
  127. */
  128. public function isSuccess()
  129. {
  130. return 200 <= $this->getStatusCode()
  131. && 300 > $this->getStatusCode();
  132. }
  133. }