-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jid.php
183 lines (157 loc) · 4.36 KB
/
Jid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2016, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
declare (strict_types = 1);
/**
* XMPP Library
*
* Copyright (C) 2016, Some right reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Xmpp;
use Kadet\Xmpp\Exception\InvalidArgumentException;
use Kadet\Xmpp\Utils\Accessors;
use Kadet\Xmpp\Utils\Immutable;
/**
* Class Jid
* @package Kadet\Xmpp
*
* @property-read string $domain
* @property-read string $local
* @property-read string $resource
*/
class Jid implements Immutable
{
use Accessors;
private $_local;
/**
* @var string
*/
private $_domain;
/**
* @var string|null
*/
private $_resource;
public function __construct(string $address, string $local = null, string $resource = null)
{
if ($local === null && $resource === null) {
list($address, $local, $resource) = self::_split($address);
}
self::validate($address, $local, $resource);
$this->_domain = $address;
$this->_local = $local;
$this->_resource = $resource;
}
private static function _split(string $address)
{
preg_match('#^(?:(?P<local>[^@]+)@)?(?P<host>.*?)(?:/(?P<resource>.+?))?$#i', $address, $result);
return [$result['host'], $result['local'] ?: null, $result['resource'] ?? null];
}
/**
* Validates address and throws InvalidArgumentException in case of failure.
*
* @param string $address
* @param string $local
* @param string|null $resource
* @return bool
*/
public static function validate(string $address, string $local = null, string $resource = null)
{
if ($local === null && $resource === null) {
list($address, $local, $resource) = self::_split($address);
}
if (empty($address)) {
throw new InvalidArgumentException("Domain-part of JID is REQUIRED");
}
if (preg_match('#[<>:&"\'/@]#i', $address, $match) !== 0) {
throw new InvalidArgumentException("Domain-part of JID contains not allowed character '{$match[0]}'");
}
if ($local !== null && preg_match('#[<>:&"\'/@]#i', $local, $match) !== 0) {
throw new InvalidArgumentException("Local-part of JID contains not allowed character '{$match[0]}'");
}
if ($resource !== null && preg_match('#[<>:&"\'/]#i', $resource, $match) !== 0) {
throw new InvalidArgumentException("Resource-part of JID contains not allowed character '{$match[0]}'");
}
return true;
}
/**
* Returns if address is valid or not.
*
* @param string $address
* @param string $local
* @param string|null $resource
* @return bool
*/
public static function isValid(string $address, string $local = null, string $resource = null) : bool
{
try {
return self::validate($address, $local, $resource);
} catch (InvalidArgumentException $exception) {
return false;
}
}
public function __toString() : string
{
return
($this->_local ? "{$this->_local}@" : null)
. $this->_domain
. ($this->_resource ? "/{$this->_resource}" : null);
}
/**
* Returns the domain part of address.
*
* @return string
*/
public function getDomain() : string
{
return $this->_domain;
}
/**
* Returns the local part of JID.
*
* @return string
*/
public function getLocal() : string
{
return $this->_local;
}
/**
* Returns resource part of address or null if resource is not set
*
* @return null|string
*/
public function getResource()
{
return $this->_resource;
}
public function bare()
{
return new static($this->domain, $this->local, null);
}
public function isBare() : bool
{
return $this->_resource === null;
}
public function isFull() : bool
{
return $this->_resource !== null && $this->_local !== null;
}
}