1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nucleus - XMPP Library for PHP |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some rights reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Stanza; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Xmpp\Jid; |
20
|
|
|
use Kadet\Xmpp\Xml\XmlElement; |
21
|
|
|
use Kadet\Xmpp\Xml\XmlFactoryCollocations; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Stanza |
25
|
|
|
* @package Kadet\Xmpp\Stanza |
26
|
|
|
* |
27
|
|
|
* @property Jid|null $from Jid representing "from" stanza attribute |
28
|
|
|
* @property Jid|null $to Jid representing "to" stanza attribute |
29
|
|
|
* @property string $type Stanza type |
30
|
|
|
* @property string $id Unique stanza id |
31
|
|
|
* @property Error $error Error details |
32
|
|
|
*/ |
33
|
|
|
class Stanza extends XmlElement implements XmlFactoryCollocations |
34
|
|
|
{ |
35
|
|
|
private $_from = false; |
36
|
|
|
private $_to = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Stanza constructor. |
40
|
|
|
* @param string $kind Stanza kind. According to XMPP RFC, one of "iq", "message", "presence" |
41
|
|
|
* @param array $options { |
42
|
|
|
* @var Jid $from Jid representing "from" stanza attribute |
43
|
|
|
* @var Jid $to Jid representing "to" stanza attribute |
44
|
|
|
* @var string $id Unique id, will be generated if omitted |
45
|
|
|
* @var string $type Stanza type |
46
|
|
|
* } |
47
|
|
|
*/ |
48
|
|
|
public function __construct(string $kind, array $options = []) |
49
|
|
|
{ |
50
|
|
|
$this->regenerateId($kind); |
51
|
|
|
parent::__construct($kind, 'jabber:client', $options); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getFrom() |
55
|
|
|
{ |
56
|
|
|
if((string)$this->_from !== $this->hasAttribute('from')) { |
57
|
|
|
$this->_from = $this->hasAttribute('from') ? new Jid($this->_from) : null; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->_from; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getTo() |
64
|
|
|
{ |
65
|
|
|
if((string)$this->_to !== $this->hasAttribute('to')) { |
66
|
|
|
$this->_to = $this->hasAttribute('to') ? new Jid($this->_to) : null; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->_to; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getType() |
73
|
|
|
{ |
74
|
|
|
return $this->getAttribute('type'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getId() |
78
|
|
|
{ |
79
|
|
|
return $this->getAttribute('id'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getError() |
83
|
|
|
{ |
84
|
|
|
return $this->element('error'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setFrom($from) |
88
|
|
|
{ |
89
|
|
|
if($from instanceof Jid) { |
90
|
|
|
$this->_from = $from instanceof Jid ? $from : new Jid($from); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->setAttribute('from', (string)$from); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setTo($to) |
97
|
|
|
{ |
98
|
|
|
if($to instanceof Jid) { |
99
|
|
|
$this->_to = $to instanceof Jid ? $to : new Jid($to); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->setAttribute('to', (string)$to); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setType(string $type) |
106
|
|
|
{ |
107
|
|
|
$this->setAttribute('type', $type); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setId(string $id) |
111
|
|
|
{ |
112
|
|
|
$this->setAttribute('id', $id); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function regenerateId(string $prefix = null) |
116
|
|
|
{ |
117
|
|
|
$this->id = uniqid($prefix, true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function response() |
121
|
|
|
{ |
122
|
|
|
$response = static::plain($this->name, $this->namespace); |
123
|
|
|
|
124
|
|
|
$response->to = $this->from; |
125
|
|
|
$response->from = $this->to; |
126
|
|
|
$response->id = $this->id; |
127
|
|
|
|
128
|
|
|
return $response; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function getXmlCollocations() : array |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
[ Error::class, 'name' => 'error', 'uri' => 'jabber:client' ], |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: