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