Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\RFC6455\Messaging;
3
 
4
class Message implements \IteratorAggregate, MessageInterface {
5
    /**
6
     * @var \SplDoublyLinkedList
7
     */
8
    private $_frames;
9
 
10
    /**
11
     * @var int
12
     */
13
    private $len;
14
 
15
    public function __construct() {
16
        $this->_frames = new \SplDoublyLinkedList;
17
        $this->len = 0;
18
    }
19
 
20
    public function getIterator() {
21
        return $this->_frames;
22
    }
23
 
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function count() {
28
        return count($this->_frames);
29
    }
30
 
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function isCoalesced() {
35
        if (count($this->_frames) == 0) {
36
            return false;
37
        }
38
 
39
        $last = $this->_frames->top();
40
 
41
        return ($last->isCoalesced() && $last->isFinal());
42
    }
43
 
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function addFrame(FrameInterface $fragment) {
48
        $this->len += $fragment->getPayloadLength();
49
        $this->_frames->push($fragment);
50
 
51
        return $this;
52
    }
53
 
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getOpcode() {
58
        if (count($this->_frames) == 0) {
59
            throw new \UnderflowException('No frames have been added to this message');
60
        }
61
 
62
        return $this->_frames->bottom()->getOpcode();
63
    }
64
 
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getPayloadLength() {
69
        return $this->len;
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getPayload() {
76
        if (!$this->isCoalesced()) {
77
            throw new \UnderflowException('Message has not been put back together yet');
78
        }
79
 
80
        return $this->__toString();
81
    }
82
 
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getContents() {
87
        if (!$this->isCoalesced()) {
88
            throw new \UnderflowException("Message has not been put back together yet");
89
        }
90
 
91
        $buffer = '';
92
 
93
        foreach ($this->_frames as $frame) {
94
            $buffer .= $frame->getContents();
95
        }
96
 
97
        return $buffer;
98
    }
99
 
100
    public function __toString() {
101
        $buffer = '';
102
 
103
        foreach ($this->_frames as $frame) {
104
            $buffer .= $frame->getPayload();
105
        }
106
 
107
        return $buffer;
108
    }
109
 
110
    /**
111
     * @return boolean
112
     */
113
    public function isBinary() {
114
        if ($this->_frames->isEmpty()) {
115
            throw new \UnderflowException('Not enough data has been received to determine if message is binary');
116
        }
117
 
118
        return Frame::OP_BINARY === $this->_frames->bottom()->getOpcode();
119
    }
120
 
121
    /**
122
     * @return boolean
123
     */
124
    public function getRsv1() {
125
        if ($this->_frames->isEmpty()) {
126
            return false;
127
            //throw new \UnderflowException('Not enough data has been received to determine if message is binary');
128
        }
129
 
130
        return $this->_frames->bottom()->getRsv1();
131
    }
132
}