1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 Fabian Grutschus. All rights reserved. |
5
|
|
|
* |
6
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
7
|
|
|
* are permitted provided that the following conditions are met: |
8
|
|
|
* |
9
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
10
|
|
|
* list of conditions and the following disclaimer. |
11
|
|
|
* |
12
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
13
|
|
|
* this list of conditions and the following disclaimer in the documentation |
14
|
|
|
* and/or other materials provided with the distribution. |
15
|
|
|
* |
16
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
20
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26
|
|
|
* |
27
|
|
|
* The views and conclusions contained in the software and documentation are those |
28
|
|
|
* of the authors and should not be interpreted as representing official policies, |
29
|
|
|
* either expressed or implied, of the copyright holders. |
30
|
|
|
* |
31
|
|
|
* @author Fabian Grutschus <[email protected]> |
32
|
|
|
* @copyright 2014 Fabian Grutschus. All rights reserved. |
33
|
|
|
* @license BSD |
34
|
|
|
* @link http://github.com/fabiang/xmpp |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
namespace Fabiang\Xmpp\Protocol; |
38
|
|
|
|
39
|
|
|
use Fabiang\Xmpp\Util\XML; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Protocol setting for Xmpp. |
43
|
|
|
* |
44
|
|
|
* @package Xmpp\Protocol |
45
|
|
|
*/ |
46
|
|
|
class Message implements ProtocolImplementationInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Chat between to users. |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
const TYPE_CHAT = 'chat'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Chat in a multi-user channel (MUC). |
56
|
|
|
*/ |
57
|
|
|
const TYPE_GROUPCHAT = 'groupchat'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Message type. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $type = self::TYPE_CHAT; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set message receiver. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $to; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Message. |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $message = ''; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Constructor. |
82
|
|
|
* |
83
|
|
|
* @param string $message |
84
|
|
|
* @param string $to |
85
|
|
|
* @param string $type |
86
|
|
|
*/ |
87
|
3 |
|
public function __construct($message = '', $to = '', $type = self::TYPE_CHAT) |
88
|
|
|
{ |
89
|
3 |
|
$this->setMessage($message)->setTo($to)->setType($type); |
90
|
3 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
3 |
|
public function toString() |
96
|
|
|
{ |
97
|
3 |
|
return XML::quoteMessage( |
98
|
3 |
|
'<message type="%s" id="%s" to="%s"><body>%s</body></message>', |
99
|
3 |
|
$this->getType(), |
100
|
3 |
|
XML::generateId(), |
101
|
3 |
|
$this->getTo(), |
102
|
3 |
|
$this->getMessage() |
103
|
3 |
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get message type. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
3 |
|
public function getType() |
112
|
|
|
{ |
113
|
3 |
|
return $this->type; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set message type. |
118
|
|
|
* |
119
|
|
|
* See {@link self::TYPE_CHAT} and {@link self::TYPE_GROUPCHAT} |
120
|
|
|
* |
121
|
|
|
* @param string $type |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
3 |
|
public function setType($type) |
125
|
|
|
{ |
126
|
3 |
|
$this->type = $type; |
127
|
3 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get message receiver. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
3 |
|
public function getTo() |
136
|
|
|
{ |
137
|
3 |
|
return $this->to; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set message receiver. |
142
|
|
|
* |
143
|
|
|
* @param string $to |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
3 |
|
public function setTo($to) |
147
|
|
|
{ |
148
|
3 |
|
$this->to = (string) $to; |
149
|
3 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get message. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
3 |
|
public function getMessage() |
158
|
|
|
{ |
159
|
3 |
|
return $this->message; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set message. |
164
|
|
|
* |
165
|
|
|
* @param string $message |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
3 |
|
public function setMessage($message) |
169
|
|
|
{ |
170
|
3 |
|
$this->message = (string) $message; |
171
|
3 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|