3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib;
14 class Message implements MessageInterface
19 protected $metadata = [];
24 protected $content = '';
27 * Set message metadata
29 * Non-destructive setting of message metadata; always adds to the metadata, never overwrites
30 * the entire metadata container.
32 * @param string|int|array|Traversable $spec
34 * @throws Exception\InvalidArgumentException
37 public function setMetadata($spec, $value = null)
39 if (is_scalar($spec)) {
40 $this->metadata[$spec] = $value;
43 if (! is_array($spec) && ! $spec instanceof Traversable) {
44 throw new Exception\InvalidArgumentException(sprintf(
45 'Expected a string, array, or Traversable argument in first position; received "%s"',
46 (is_object($spec) ? get_class($spec) : gettype($spec))
49 foreach ($spec as $key => $value) {
50 $this->metadata[$key] = $value;
56 * Retrieve all metadata or a single metadatum as specified by key
58 * @param null|string|int $key
59 * @param null|mixed $default
60 * @throws Exception\InvalidArgumentException
63 public function getMetadata($key = null, $default = null)
66 return $this->metadata;
69 if (! is_scalar($key)) {
70 throw new Exception\InvalidArgumentException('Non-scalar argument provided for key');
73 if (array_key_exists($key, $this->metadata)) {
74 return $this->metadata[$key];
86 public function setContent($value)
88 $this->content = $value;
97 public function getContent()
99 return $this->content;
105 public function toString()
108 foreach ($this->getMetadata() as $key => $value) {
115 $request .= "\r\n" . $this->getContent();