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
|
13 |
|
public function addBuffer($data) |
42
|
|
|
{ |
43
|
13 |
|
$this->buffer .= $data; |
44
|
13 |
|
} |
45
|
|
|
|
46
|
1 |
|
public function clearBuffer() |
47
|
|
|
{ |
48
|
1 |
|
$this->buffer = ''; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
13 |
|
public function getBuffer() |
52
|
|
|
{ |
53
|
13 |
|
return $this->buffer; |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
public function removeFromBuffer(Frame $frame) : string |
57
|
|
|
{ |
58
|
9 |
|
$this->buffer = StringTools::removeStart($this->getBuffer(), $frame->getRawData(), '8bit'); |
59
|
|
|
|
60
|
9 |
|
return $this->buffer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Frame $frame |
65
|
|
|
* @return Message |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
* @throws LimitationException |
68
|
|
|
*/ |
69
|
35 |
|
public function addFrame(Frame $frame) : Message |
70
|
|
|
{ |
71
|
35 |
|
if ($this->isComplete) { |
72
|
|
|
throw new \InvalidArgumentException('The message is already complete.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
35 |
|
if (count($this->frames) > 19) { |
76
|
1 |
|
throw new LimitationException('We don\'t accept more than 20 frames by message. This is a security limitation.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
35 |
|
$this->isComplete = $frame->isFinal(); |
80
|
35 |
|
$this->frames[] = $frame; |
81
|
|
|
|
82
|
35 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Frame |
87
|
|
|
* @throws MissingDataException |
88
|
|
|
*/ |
89
|
23 |
|
public function getFirstFrame() : Frame |
90
|
|
|
{ |
91
|
23 |
|
if (empty($this->frames[0])) { |
92
|
|
|
throw new MissingDataException('There is no first frame for now.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
23 |
|
return $this->frames[0]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* This could in the future be deprecated in favor of a stream object. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
* @throws MissingDataException |
103
|
|
|
*/ |
104
|
10 |
|
public function getContent() : string |
105
|
|
|
{ |
106
|
10 |
|
if (!$this->isComplete) { |
107
|
1 |
|
throw new MissingDataException('The message is not complete. Frames are missing.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
|
$res = ''; |
111
|
|
|
|
112
|
9 |
|
foreach ($this->frames as $frame) { |
113
|
9 |
|
$res .= $frame->getPayload(); |
114
|
|
|
} |
115
|
|
|
|
116
|
9 |
|
return $res; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
19 |
|
public function getOpcode() |
123
|
|
|
{ |
124
|
19 |
|
return $this->getFirstFrame()->getOpcode(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
10 |
|
public function isComplete() |
131
|
|
|
{ |
132
|
10 |
|
return $this->isComplete; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function isOperation() |
139
|
|
|
{ |
140
|
|
|
return in_array($this->getFirstFrame()->getOpcode(), [Frame::OP_TEXT, Frame::OP_BINARY]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Frame[] |
145
|
|
|
*/ |
146
|
4 |
|
public function getFrames() |
147
|
|
|
{ |
148
|
4 |
|
return $this->frames; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
3 |
|
public function countFrames() : int |
155
|
|
|
{ |
156
|
3 |
|
return count($this->frames); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|