comparison vendor/zendframework/zend-stdlib/src/Message.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 5311817fb629
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Stdlib;
11
12 use Traversable;
13
14 class Message implements MessageInterface
15 {
16 /**
17 * @var array
18 */
19 protected $metadata = [];
20
21 /**
22 * @var string
23 */
24 protected $content = '';
25
26 /**
27 * Set message metadata
28 *
29 * Non-destructive setting of message metadata; always adds to the metadata, never overwrites
30 * the entire metadata container.
31 *
32 * @param string|int|array|Traversable $spec
33 * @param mixed $value
34 * @throws Exception\InvalidArgumentException
35 * @return Message
36 */
37 public function setMetadata($spec, $value = null)
38 {
39 if (is_scalar($spec)) {
40 $this->metadata[$spec] = $value;
41 return $this;
42 }
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))
47 ));
48 }
49 foreach ($spec as $key => $value) {
50 $this->metadata[$key] = $value;
51 }
52 return $this;
53 }
54
55 /**
56 * Retrieve all metadata or a single metadatum as specified by key
57 *
58 * @param null|string|int $key
59 * @param null|mixed $default
60 * @throws Exception\InvalidArgumentException
61 * @return mixed
62 */
63 public function getMetadata($key = null, $default = null)
64 {
65 if (null === $key) {
66 return $this->metadata;
67 }
68
69 if (!is_scalar($key)) {
70 throw new Exception\InvalidArgumentException('Non-scalar argument provided for key');
71 }
72
73 if (array_key_exists($key, $this->metadata)) {
74 return $this->metadata[$key];
75 }
76
77 return $default;
78 }
79
80 /**
81 * Set message content
82 *
83 * @param mixed $value
84 * @return Message
85 */
86 public function setContent($value)
87 {
88 $this->content = $value;
89 return $this;
90 }
91
92 /**
93 * Get message content
94 *
95 * @return mixed
96 */
97 public function getContent()
98 {
99 return $this->content;
100 }
101
102 /**
103 * @return string
104 */
105 public function toString()
106 {
107 $request = '';
108 foreach ($this->getMetadata() as $key => $value) {
109 $request .= sprintf(
110 "%s: %s\r\n",
111 (string) $key,
112 (string) $value
113 );
114 }
115 $request .= "\r\n" . $this->getContent();
116 return $request;
117 }
118 }