1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of Woketo package. |
4
|
|
|
* |
5
|
|
|
* (c) Nekland <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full license, take a look to the LICENSE file |
8
|
|
|
* on the root directory of this project |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Nekland\Woketo\Rfc6455; |
12
|
|
|
|
13
|
|
|
use Nekland\Tools\StringTools; |
14
|
|
|
use Nekland\Woketo\Exception\LimitationException; |
15
|
|
|
use Nekland\Woketo\Exception\MissingDataException; |
16
|
|
|
|
17
|
|
|
class Message |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $frames; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $isComplete; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $buffer; |
33
|
|
|
|
34
|
40 |
|
public function __construct() |
35
|
|
|
{ |
36
|
40 |
|
$this->frames = []; |
37
|
40 |
|
$this->isComplete = false; |
38
|
40 |
|
$this->buffer = ''; |
39
|
40 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add some data to the buffer. |
43
|
|
|
* |
44
|
|
|
* @param $data |
45
|
|
|
*/ |
46
|
13 |
|
public function addBuffer($data) |
47
|
|
|
{ |
48
|
13 |
|
$this->buffer .= $data; |
49
|
13 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Clear the buffer. |
53
|
|
|
*/ |
54
|
1 |
|
public function clearBuffer() |
55
|
|
|
{ |
56
|
1 |
|
$this->buffer = ''; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get data inside the buffer. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
13 |
|
public function getBuffer() |
65
|
|
|
{ |
66
|
13 |
|
return $this->buffer; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove data from the start of the buffer. |
71
|
|
|
* |
72
|
|
|
* @param Frame $frame |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
9 |
|
public function removeFromBuffer(Frame $frame) : string |
76
|
|
|
{ |
77
|
9 |
|
$this->buffer = StringTools::removeStart($this->getBuffer(), $frame->getRawData(), '8bit'); |
78
|
|
|
|
79
|
9 |
|
return $this->buffer; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Frame $frame |
84
|
|
|
* @return Message |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
* @throws LimitationException |
87
|
|
|
*/ |
88
|
35 |
|
public function addFrame(Frame $frame) : Message |
89
|
|
|
{ |
90
|
35 |
|
if ($this->isComplete) { |
91
|
|
|
throw new \InvalidArgumentException('The message is already complete.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
35 |
|
if (count($this->frames) > 19) { |
95
|
1 |
|
throw new LimitationException('We don\'t accept more than 20 frames by message. This is a security limitation.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
35 |
|
$this->isComplete = $frame->isFinal(); |
99
|
35 |
|
$this->frames[] = $frame; |
100
|
|
|
|
101
|
35 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Frame |
106
|
|
|
* @throws MissingDataException |
107
|
|
|
*/ |
108
|
23 |
|
public function getFirstFrame() : Frame |
109
|
|
|
{ |
110
|
23 |
|
if (empty($this->frames[0])) { |
111
|
|
|
throw new MissingDataException('There is no first frame for now.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
23 |
|
return $this->frames[0]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* This could in the future be deprecated in favor of a stream object. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
* @throws MissingDataException |
122
|
|
|
*/ |
123
|
10 |
|
public function getContent() : string |
124
|
|
|
{ |
125
|
10 |
|
if (!$this->isComplete) { |
126
|
1 |
|
throw new MissingDataException('The message is not complete. Frames are missing.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
9 |
|
$res = ''; |
130
|
|
|
|
131
|
9 |
|
foreach ($this->frames as $frame) { |
132
|
9 |
|
$res .= $frame->getPayload(); |
133
|
|
|
} |
134
|
|
|
|
135
|
9 |
|
return $res; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
19 |
|
public function getOpcode() |
142
|
|
|
{ |
143
|
19 |
|
return $this->getFirstFrame()->getOpcode(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
10 |
|
public function isComplete() |
150
|
|
|
{ |
151
|
10 |
|
return $this->isComplete; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isOperation() |
158
|
|
|
{ |
159
|
|
|
return in_array($this->getFirstFrame()->getOpcode(), [Frame::OP_TEXT, Frame::OP_BINARY]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return Frame[] |
164
|
|
|
*/ |
165
|
4 |
|
public function getFrames() |
166
|
|
|
{ |
167
|
4 |
|
return $this->frames; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
3 |
|
public function hasFrames() |
174
|
|
|
{ |
175
|
3 |
|
return count($this->frames) > 0; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function countFrames() : int |
182
|
|
|
{ |
183
|
|
|
return count($this->frames); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|