ResponseCastable.php 2.9 KB

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