Message.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib;
  10. use Traversable;
  11. class Message implements MessageInterface
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $metadata = array();
  17. /**
  18. * @var string
  19. */
  20. protected $content = '';
  21. /**
  22. * Set message metadata
  23. *
  24. * Non-destructive setting of message metadata; always adds to the metadata, never overwrites
  25. * the entire metadata container.
  26. *
  27. * @param string|int|array|Traversable $spec
  28. * @param mixed $value
  29. * @throws Exception\InvalidArgumentException
  30. * @return Message
  31. */
  32. public function setMetadata($spec, $value = null)
  33. {
  34. if (is_scalar($spec)) {
  35. $this->metadata[$spec] = $value;
  36. return $this;
  37. }
  38. if (!is_array($spec) && !$spec instanceof Traversable) {
  39. throw new Exception\InvalidArgumentException(sprintf(
  40. 'Expected a string, array, or Traversable argument in first position; received "%s"',
  41. (is_object($spec) ? get_class($spec) : gettype($spec))
  42. ));
  43. }
  44. foreach ($spec as $key => $value) {
  45. $this->metadata[$key] = $value;
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Retrieve all metadata or a single metadatum as specified by key
  51. *
  52. * @param null|string|int $key
  53. * @param null|mixed $default
  54. * @throws Exception\InvalidArgumentException
  55. * @return mixed
  56. */
  57. public function getMetadata($key = null, $default = null)
  58. {
  59. if (null === $key) {
  60. return $this->metadata;
  61. }
  62. if (!is_scalar($key)) {
  63. throw new Exception\InvalidArgumentException('Non-scalar argument provided for key');
  64. }
  65. if (array_key_exists($key, $this->metadata)) {
  66. return $this->metadata[$key];
  67. }
  68. return $default;
  69. }
  70. /**
  71. * Set message content
  72. *
  73. * @param mixed $value
  74. * @return Message
  75. */
  76. public function setContent($value)
  77. {
  78. $this->content = $value;
  79. return $this;
  80. }
  81. /**
  82. * Get message content
  83. *
  84. * @return mixed
  85. */
  86. public function getContent()
  87. {
  88. return $this->content;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function toString()
  94. {
  95. $request = '';
  96. foreach ($this->getMetadata() as $key => $value) {
  97. $request .= sprintf(
  98. "%s: %s\r\n",
  99. (string) $key,
  100. (string) $value
  101. );
  102. }
  103. $request .= "\r\n" . $this->getContent();
  104. return $request;
  105. }
  106. }