Rocket.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Artful;
  4. use ArrayAccess;
  5. use JsonSerializable as JsonSerializableInterface;
  6. use Psr\Http\Message\MessageInterface;
  7. use Psr\Http\Message\RequestInterface;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Yansongda\Artful\Contract\DirectionInterface;
  10. use Yansongda\Artful\Contract\PackerInterface;
  11. use Yansongda\Supports\Collection;
  12. use Yansongda\Supports\Traits\Accessable;
  13. use Yansongda\Supports\Traits\Arrayable;
  14. use Yansongda\Supports\Traits\Serializable;
  15. class Rocket implements JsonSerializableInterface, ArrayAccess
  16. {
  17. use Accessable;
  18. use Arrayable;
  19. use Serializable;
  20. private ?RequestInterface $radar = null;
  21. private array $params = [];
  22. private ?Collection $payload = null;
  23. private string $packer = PackerInterface::class;
  24. private string $direction = DirectionInterface::class;
  25. private null|Collection|MessageInterface $destination = null;
  26. private null|RequestInterface|ResponseInterface $destinationOrigin = null;
  27. public function getRadar(): ?RequestInterface
  28. {
  29. return $this->radar;
  30. }
  31. public function setRadar(?RequestInterface $radar): Rocket
  32. {
  33. $this->radar = $radar;
  34. return $this;
  35. }
  36. public function getParams(): array
  37. {
  38. return $this->params;
  39. }
  40. public function setParams(array $params): Rocket
  41. {
  42. $this->params = $params;
  43. return $this;
  44. }
  45. public function mergeParams(array $params): Rocket
  46. {
  47. $this->params = array_merge($this->params, $params);
  48. return $this;
  49. }
  50. public function getPayload(): ?Collection
  51. {
  52. return $this->payload;
  53. }
  54. public function setPayload(null|array|Collection $payload): Rocket
  55. {
  56. if (is_array($payload)) {
  57. $payload = new Collection($payload);
  58. }
  59. $this->payload = $payload;
  60. return $this;
  61. }
  62. public function mergePayload(array $payload): Rocket
  63. {
  64. if (empty($this->payload)) {
  65. $this->payload = new Collection();
  66. }
  67. $this->payload = $this->payload->merge($payload);
  68. return $this;
  69. }
  70. public function exceptPayload(mixed $key): Rocket
  71. {
  72. if (empty($this->payload)) {
  73. return $this;
  74. }
  75. $this->payload = $this->payload->except($key);
  76. return $this;
  77. }
  78. public function getPacker(): string
  79. {
  80. return $this->packer;
  81. }
  82. public function setPacker(string $packer): Rocket
  83. {
  84. $this->packer = $packer;
  85. return $this;
  86. }
  87. public function getDirection(): string
  88. {
  89. return $this->direction;
  90. }
  91. public function setDirection(string $direction): Rocket
  92. {
  93. $this->direction = $direction;
  94. return $this;
  95. }
  96. public function getDestination(): null|Collection|MessageInterface
  97. {
  98. return $this->destination;
  99. }
  100. public function setDestination(null|Collection|MessageInterface $destination): Rocket
  101. {
  102. $this->destination = $destination;
  103. return $this;
  104. }
  105. public function getDestinationOrigin(): null|RequestInterface|ResponseInterface
  106. {
  107. return $this->destinationOrigin;
  108. }
  109. public function setDestinationOrigin(null|RequestInterface|ResponseInterface $destinationOrigin): Rocket
  110. {
  111. $this->destinationOrigin = $destinationOrigin;
  112. return $this;
  113. }
  114. public function toArray(): array
  115. {
  116. $request = $this->getRadar();
  117. $destination = $this->getDestinationOrigin();
  118. return [
  119. 'radar' => [
  120. 'url' => $request?->getUri()->__toString(),
  121. 'method' => $request?->getMethod(),
  122. 'headers' => $request?->getHeaders(),
  123. 'body' => (string) $request?->getBody(),
  124. ],
  125. 'params' => $this->getParams(),
  126. 'payload' => $this->getPayload()?->toArray(),
  127. 'packer' => $this->getPacker(),
  128. 'direction' => $this->getDirection(),
  129. 'destination' => $this->getDestination(),
  130. 'destination_origin' => [
  131. 'status' => $destination instanceof ResponseInterface ? $destination->getStatusCode() : null,
  132. 'headers' => $destination?->getHeaders(),
  133. 'body' => (string) $destination?->getBody(),
  134. ],
  135. ];
  136. }
  137. }