|
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
|
38 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
38 |
|
$this->frames = []; |
|
37
|
38 |
|
$this->isComplete = false; |
|
38
|
38 |
|
$this->buffer = ''; |
|
39
|
38 |
|
} |
|
40
|
|
|
|
|
41
|
11 |
|
public function addBuffer($data) |
|
42
|
|
|
{ |
|
43
|
11 |
|
$this->buffer .= $data; |
|
44
|
11 |
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function clearBuffer() |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->buffer = ''; |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
11 |
|
public function getBuffer() |
|
52
|
|
|
{ |
|
53
|
11 |
|
return $this->buffer; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
7 |
|
public function removeFromBuffer(Frame $frame) : string |
|
57
|
|
|
{ |
|
58
|
7 |
|
$this->buffer = StringTools::removeStart($this->getBuffer(), $frame->getRawData(), '8bit'); |
|
59
|
|
|
|
|
60
|
7 |
|
return $this->buffer; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Frame $frame |
|
65
|
|
|
* @return Message |
|
66
|
|
|
* @throws \InvalidArgumentException |
|
67
|
|
|
* @throws LimitationException |
|
68
|
|
|
*/ |
|
69
|
33 |
|
public function addFrame(Frame $frame) : Message |
|
70
|
|
|
{ |
|
71
|
33 |
|
if ($this->isComplete) { |
|
72
|
|
|
throw new \InvalidArgumentException('The message is already complete.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
33 |
|
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
|
33 |
|
$this->isComplete = $frame->isFinal(); |
|
80
|
33 |
|
$this->frames[] = $frame; |
|
81
|
|
|
|
|
82
|
33 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Frame |
|
87
|
|
|
* @throws MissingDataException |
|
88
|
|
|
*/ |
|
89
|
21 |
|
public function getFirstFrame() : Frame |
|
90
|
|
|
{ |
|
91
|
21 |
|
if (empty($this->frames[0])) { |
|
92
|
|
|
throw new MissingDataException('There is no first frame for now.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
21 |
|
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
|
8 |
|
public function getContent() : string |
|
105
|
|
|
{ |
|
106
|
8 |
|
if (!$this->isComplete) { |
|
107
|
1 |
|
throw new MissingDataException('The message is not complete. Frames are missing.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
7 |
|
$res = ''; |
|
111
|
|
|
|
|
112
|
7 |
|
foreach ($this->frames as $frame) { |
|
113
|
7 |
|
$res .= $frame->getPayload(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
7 |
|
return $res; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
17 |
|
public function getOpcode() |
|
123
|
|
|
{ |
|
124
|
17 |
|
return $this->getFirstFrame()->getOpcode(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
8 |
|
public function isComplete() |
|
131
|
|
|
{ |
|
132
|
8 |
|
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
|
2 |
|
public function getFrames() |
|
147
|
|
|
{ |
|
148
|
2 |
|
return $this->frames; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|