StompMessage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. namespace Enqueue\Stomp;
  4. use Interop\Queue\Message;
  5. use Stomp\Transport\Frame;
  6. class StompMessage implements Message
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $body;
  12. /**
  13. * @var array
  14. */
  15. private $properties;
  16. /**
  17. * @var array
  18. */
  19. private $headers;
  20. /**
  21. * @var bool
  22. */
  23. private $redelivered;
  24. /**
  25. * @var Frame
  26. */
  27. private $frame;
  28. public function __construct(string $body = '', array $properties = [], array $headers = [])
  29. {
  30. $this->body = $body;
  31. $this->properties = $properties;
  32. $this->headers = $headers;
  33. $this->redelivered = false;
  34. }
  35. public function setBody(string $body): void
  36. {
  37. $this->body = $body;
  38. }
  39. public function getBody(): string
  40. {
  41. return $this->body;
  42. }
  43. public function setProperties(array $properties): void
  44. {
  45. $this->properties = $properties;
  46. }
  47. public function getProperties(): array
  48. {
  49. return $this->properties;
  50. }
  51. public function setProperty(string $name, $value): void
  52. {
  53. if (null === $value) {
  54. unset($this->properties[$name]);
  55. } else {
  56. $this->properties[$name] = $value;
  57. }
  58. }
  59. public function getProperty(string $name, $default = null)
  60. {
  61. return array_key_exists($name, $this->properties) ? $this->properties[$name] : $default;
  62. }
  63. public function setHeaders(array $headers): void
  64. {
  65. $this->headers = $headers;
  66. }
  67. public function getHeaders(): array
  68. {
  69. return $this->headers;
  70. }
  71. public function setHeader(string $name, $value): void
  72. {
  73. if (null === $value) {
  74. unset($this->headers[$name]);
  75. } else {
  76. $this->headers[$name] = $value;
  77. }
  78. }
  79. public function getHeader(string $name, $default = null)
  80. {
  81. return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default;
  82. }
  83. public function isPersistent(): bool
  84. {
  85. return $this->getHeader('persistent', false);
  86. }
  87. public function setPersistent(bool $persistent): void
  88. {
  89. $this->setHeader('persistent', $persistent);
  90. }
  91. public function isRedelivered(): bool
  92. {
  93. return $this->redelivered;
  94. }
  95. public function setRedelivered(bool $redelivered): void
  96. {
  97. $this->redelivered = $redelivered;
  98. }
  99. public function setCorrelationId(string $correlationId = null): void
  100. {
  101. $this->setHeader('correlation_id', (string) $correlationId);
  102. }
  103. public function getCorrelationId(): ?string
  104. {
  105. return $this->getHeader('correlation_id');
  106. }
  107. public function setMessageId(string $messageId = null): void
  108. {
  109. $this->setHeader('message_id', (string) $messageId);
  110. }
  111. public function getMessageId(): ?string
  112. {
  113. return $this->getHeader('message_id');
  114. }
  115. public function getTimestamp(): ?int
  116. {
  117. $value = $this->getHeader('timestamp');
  118. return null === $value ? null : (int) $value;
  119. }
  120. public function setTimestamp(int $timestamp = null): void
  121. {
  122. $this->setHeader('timestamp', $timestamp);
  123. }
  124. public function getFrame(): ?Frame
  125. {
  126. return $this->frame;
  127. }
  128. public function setFrame(Frame $frame = null): void
  129. {
  130. $this->frame = $frame;
  131. }
  132. public function setReplyTo(string $replyTo = null): void
  133. {
  134. $this->setHeader('reply-to', $replyTo);
  135. }
  136. public function getReplyTo(): ?string
  137. {
  138. return $this->getHeader('reply-to');
  139. }
  140. }