ResponseCastable.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Traits;
  16. use ByteDance\Kernel\Contracts\Arrayable;
  17. use ByteDance\Kernel\Exceptions\InvalidArgumentException;
  18. use ByteDance\Kernel\Http\Response;
  19. use ByteDance\Kernel\Support\Collection;
  20. use Psr\Http\Message\ResponseInterface;
  21. /**
  22. * Trait ResponseCastable.
  23. *
  24. * @author overtrue <i@overtrue.me>
  25. */
  26. trait ResponseCastable
  27. {
  28. /**
  29. * @param \Psr\Http\Message\ResponseInterface $response
  30. * @param string|null $type
  31. *
  32. * @throws \ByteDance\Kernel\Exceptions\InvalidArgumentException
  33. *
  34. * @return array|\ByteDance\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  35. */
  36. protected function castResponseToType(ResponseInterface $response, $type = null)
  37. {
  38. $response = Response::buildFromPsrResponse($response);
  39. $response->getBody()->rewind();
  40. switch ($type ?? 'array') {
  41. case 'collection':
  42. return $response->toCollection();
  43. case 'array':
  44. return $response->toArray();
  45. case 'object':
  46. return $response->toObject();
  47. case 'raw':
  48. return $response;
  49. default:
  50. if (!is_subclass_of($type, Arrayable::class)) {
  51. throw new InvalidArgumentException(sprintf(
  52. 'Config key "response_type" classname must be an instanceof %s',
  53. Arrayable::class
  54. ));
  55. }
  56. return new $type($response);
  57. }
  58. }
  59. /**
  60. * @param mixed $response
  61. * @param string|null $type
  62. *
  63. * @throws \ByteDance\Kernel\Exceptions\InvalidArgumentException
  64. * @throws \ByteDance\Kernel\Exceptions\InvalidConfigException
  65. *
  66. * @return array|\ByteDance\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  67. */
  68. protected function detectAndCastResponseToType($response, $type = null)
  69. {
  70. switch (true) {
  71. case $response instanceof ResponseInterface:
  72. $response = Response::buildFromPsrResponse($response);
  73. break;
  74. case $response instanceof Arrayable:
  75. $response = new Response(200, [], json_encode($response->toArray()));
  76. break;
  77. case ($response instanceof Collection) || is_array($response) || is_object($response):
  78. $response = new Response(200, [], json_encode($response));
  79. break;
  80. case is_scalar($response):
  81. $response = new Response(200, [], $response);
  82. break;
  83. default:
  84. throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
  85. }
  86. return $this->castResponseToType($response, $type);
  87. }
  88. }