|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PEIP\Message; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the PEIP package. |
|
7
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/* |
|
14
|
|
|
* GenericMessage |
|
15
|
|
|
* |
|
16
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
|
17
|
|
|
* @package PEIP |
|
18
|
|
|
* @subpackage message |
|
19
|
|
|
* @extends PEIP\ABS\Base\Container |
|
20
|
|
|
* @implements \PEIP\INF\Base\Container, \PEIP\INF\Message\Message, \PEIP\INF\Base\Buildable |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
use PEIP\Base\GenericBuilder; |
|
24
|
|
|
use PEIP\Util\Test; |
|
25
|
|
|
|
|
26
|
|
|
class GenericMessage implements |
|
27
|
|
|
\PEIP\INF\Message\Message, |
|
28
|
|
|
\PEIP\INF\Base\Buildable |
|
29
|
|
|
{ |
|
30
|
|
|
const CONTENT_CAST_TYPE = ''; |
|
31
|
|
|
|
|
32
|
|
|
private $content, |
|
33
|
|
|
$headers; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $content The content/payload of the message |
|
39
|
|
|
* @param array|ArrayAccess $headers headers as key/value pairs |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($content, $headers = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->doSetContent($content); |
|
44
|
|
|
$this->doSetHeaders($headers); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the content of the container. |
|
49
|
|
|
* |
|
50
|
|
|
* @implements \PEIP\INF\Base\Container |
|
51
|
|
|
* |
|
52
|
|
|
* @return |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getContent() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->content; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* sets content/payload of message - to be overwritten by derived classes. |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $content The content/payload of the message |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function doSetContent($content) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->content = Test::castType($content, self::CONTENT_CAST_TYPE); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function doSetHeaders($headers) |
|
70
|
|
|
{ |
|
71
|
|
|
$headers = Test::ensureArrayAccess($headers); |
|
72
|
|
|
if (is_array($headers)) { |
|
73
|
|
|
$headers = new \ArrayObject($headers); |
|
74
|
|
|
} |
|
75
|
|
|
$this->headers = $headers; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* returns all headers of the message. |
|
80
|
|
|
* |
|
81
|
|
|
* @return ArrayAccess ArrayAccess object of headers |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getHeaders() |
|
84
|
|
|
{ |
|
85
|
|
|
return (array) $this->headers; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* returns one specific header of the message. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $name the name of the header |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed the value of the header |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getHeader($name) |
|
96
|
|
|
{ |
|
97
|
|
|
$name = (string) $name; |
|
98
|
|
|
|
|
99
|
|
|
return isset($this->headers[$name]) ? $this->headers[$name] : null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* adds a specific header to the message if that header |
|
104
|
|
|
* has not allready been set. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $name the name of the header |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool wether the header has been successfully set |
|
109
|
|
|
*/ |
|
110
|
|
|
public function addHeader($name, $value) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!$this->hasHeader($name)) { |
|
113
|
|
|
$this->headers[$name] = $value; |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* checks wether a specific header is set on the message. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name the name of the header |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool wether the header is set |
|
127
|
|
|
*/ |
|
128
|
|
|
public function hasHeader($name) |
|
129
|
|
|
{ |
|
130
|
|
|
return isset($this->headers[$name]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* returns content/payload of the message as string representation for the instance. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string content/payload of the message |
|
137
|
|
|
*/ |
|
138
|
|
|
public function __toString() |
|
139
|
|
|
{ |
|
140
|
|
|
$res = false; |
|
141
|
|
|
try { |
|
142
|
|
|
$res = (string) $this->getContent(); |
|
143
|
|
|
} catch (\Exception $e) { |
|
144
|
|
|
try { |
|
145
|
|
|
$res = get_class($this->getContent()); |
|
146
|
|
|
} catch (\Exception $e) { |
|
147
|
|
|
return ''; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $res; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Provides a static build method to create new Instances of this class. |
|
156
|
|
|
* Implements \PEIP\INF\Base\Buildable. |
|
157
|
|
|
* |
|
158
|
|
|
* @static |
|
159
|
|
|
* @implements \PEIP\INF\Base\Buildable |
|
160
|
|
|
* |
|
161
|
|
|
* @param array $arguments argumends for the constructor |
|
162
|
|
|
* |
|
163
|
|
|
* @return GenericMessage new class instance |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function build(array $arguments = []) |
|
166
|
|
|
{ |
|
167
|
|
|
return GenericBuilder::getInstance(__CLASS__)->build($arguments); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|