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