1 | <?php |
||
33 | class Stanza extends XmlElement implements XmlFactoryCollocations |
||
34 | { |
||
35 | /** @var bool|Jid */ |
||
36 | private $_from = false; |
||
37 | /** @var bool|Jid */ |
||
38 | private $_to = false; |
||
39 | |||
40 | /** |
||
41 | * Stanza constructor. |
||
42 | * @param string $kind Stanza kind. According to XMPP RFC, one of "iq", "message", "presence" |
||
43 | * @param array $options { |
||
44 | * @var Jid $from Jid representing "from" stanza attribute |
||
45 | * @var Jid $to Jid representing "to" stanza attribute |
||
46 | * @var string $id Unique id, will be generated if omitted |
||
47 | * @var string $type Stanza type |
||
48 | * } |
||
49 | */ |
||
50 | 7 | public function __construct(string $kind, array $options = []) |
|
51 | { |
||
52 | 7 | $this->regenerateId($kind); |
|
53 | 7 | parent::__construct($kind, 'jabber:client', $options); |
|
54 | 7 | } |
|
55 | |||
56 | 1 | public function getFrom() |
|
57 | { |
||
58 | 1 | if((string)$this->_from !== $this->hasAttribute('from')) { |
|
59 | 1 | $this->_from = $this->hasAttribute('from') ? new Jid($this->getAttribute('from')) : null; |
|
60 | } |
||
61 | |||
62 | 1 | return $this->_from; |
|
63 | } |
||
64 | |||
65 | 1 | public function getTo() |
|
66 | { |
||
67 | 1 | if((string)$this->_to !== $this->hasAttribute('to')) { |
|
68 | 1 | $this->_to = $this->hasAttribute('to') ? new Jid($this->getAttribute('to')) : null; |
|
69 | } |
||
70 | |||
71 | 1 | return $this->_to; |
|
72 | } |
||
73 | |||
74 | public function getType() |
||
75 | { |
||
76 | return $this->getAttribute('type'); |
||
77 | } |
||
78 | |||
79 | 4 | public function getId() |
|
80 | { |
||
81 | 4 | return $this->getAttribute('id'); |
|
82 | } |
||
83 | |||
84 | public function getError() |
||
88 | |||
89 | 2 | public function setFrom($from) |
|
90 | { |
||
91 | 2 | if($from instanceof Jid) { |
|
92 | 1 | $this->_from = $from instanceof Jid ? $from : new Jid($from); |
|
93 | } |
||
94 | |||
95 | 2 | $this->setAttribute('from', (string)$from); |
|
96 | 2 | } |
|
97 | |||
98 | 4 | public function setTo($to) |
|
99 | { |
||
100 | 4 | if($to instanceof Jid) { |
|
101 | 3 | $this->_to = $to instanceof Jid ? $to : new Jid($to); |
|
102 | } |
||
103 | |||
104 | 4 | $this->setAttribute('to', (string)$to); |
|
105 | 4 | } |
|
106 | |||
107 | 7 | public function setType(string $type) |
|
108 | { |
||
109 | 7 | $this->setAttribute('type', $type); |
|
110 | 7 | } |
|
111 | |||
112 | 7 | public function setId(string $id) |
|
113 | { |
||
114 | 7 | $this->setAttribute('id', $id); |
|
115 | 7 | } |
|
116 | |||
117 | 7 | public function regenerateId(string $prefix = null) |
|
118 | { |
||
119 | 7 | $this->id = uniqid($prefix, true); |
|
120 | 7 | } |
|
121 | |||
122 | 1 | public function response() |
|
123 | { |
||
124 | 1 | $response = static::plain($this->fullName, $this->namespace); |
|
125 | |||
126 | 1 | $response->to = $this->from; |
|
127 | 1 | $response->from = $this->to; |
|
128 | 1 | $response->id = $this->id; |
|
129 | |||
130 | 1 | return $response; |
|
131 | } |
||
132 | |||
133 | public static function getXmlCollocations() : array |
||
139 | } |
||
140 |