Completed
Push — master ( f4b721...596cf5 )
by Kacper
02:42
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right 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;
17
18
19
use Kadet\Xmpp\Exception\InvalidArgumentException;
20
use Kadet\Xmpp\Utils\Accessors;
21
use Kadet\Xmpp\Utils\Immutable;
22
23
/**
24
 * Class Jid
25
 * @package Kadet\Xmpp
26
 *
27
 * @property-read string $domain
28
 * @property-read string $local
29
 * @property-read string $resource
30
 */
31
class Jid implements Immutable
32
{
33
    use Accessors;
34
35
    private $_local;
36
37
    /**
38
     * @var string
39
     */
40
    private $_domain;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $_resource;
46
47 15
    public function __construct(string $address, string $local = null, string $resource = null)
48
    {
49 15 View Code Duplication
        if ($local === null && $resource === null) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 11
            list($address, $local, $resource) = self::_split($address);
51
        }
52
53 15
        self::validate($address, $local, $resource);
54
55 11
        $this->_domain = $address;
56 11
        $this->_local = $local;
57 11
        $this->_resource = $resource;
58 11
    }
59
60 20
    private static function _split(string $address)
61
    {
62 20
        preg_match('#^(?:(?P<local>[^@]+)@)?(?P<host>.*?)(?:/(?P<resource>.+?))?$#i', $address, $result);
63
64 20
        return [$result['host'], $result['local'] ?: null, $result['resource'] ?? null];
65
    }
66
67
    /**
68
     * Validates address and throws InvalidArgumentException in case of failure.
69
     *
70
     * @param string      $address
71
     * @param string      $local
72
     * @param string|null $resource
73
     * @return bool
74
     */
75 34
    public static function validate(string $address, string $local = null, string $resource = null)
76
    {
77 34 View Code Duplication
        if ($local === null && $resource === null) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78 15
            list($address, $local, $resource) = self::_split($address);
79
        }
80
81 34
        if (empty($address)) {
82 9
            throw new InvalidArgumentException("Domain-part of JID is REQUIRED");
83
        }
84
85 25
        if (preg_match('#[<>:&"\'/@]#i', $address, $match) !== 0) {
86 3
            throw new InvalidArgumentException("Domain-part of JID contains not allowed character '{$match[0]}'");
87
        }
88
89 22
        if ($local !== null && preg_match('#[<>:&"\'/@]#i', $local, $match) !== 0) {
90 2
            throw new InvalidArgumentException("Local-part of JID contains not allowed character '{$match[0]}'");
91
        }
92
93 20
        if ($resource !== null && preg_match('#[<>:&"\'/]#i', $resource, $match) !== 0) {
94 3
            throw new InvalidArgumentException("Resource-part of JID contains not allowed character '{$match[0]}'");
95
        }
96
97 17
        return true;
98
    }
99
100
    /**
101
     * Returns if address is valid or not.
102
     *
103
     * @param string      $address
104
     * @param string      $local
105
     * @param string|null $resource
106
     * @return bool
107
     */
108 17
    public static function isValid(string $address, string $local = null, string $resource = null) : bool
109
    {
110
        try {
111 17
            return self::validate($address, $local, $resource);
112 11
        } catch (InvalidArgumentException $exception) {
113 11
            return false;
114
        }
115
    }
116
117 1
    public function __toString() : string
118
    {
119
        return
120 1
            ($this->_local ? "{$this->_local}@" : null)
121 1
            . $this->_domain
122 1
            . ($this->_resource ? "/{$this->_resource}" : null);
123
    }
124
125
    /**
126
     * Returns the domain part of address.
127
     *
128
     * @return string
129
     */
130 8
    public function getDomain() : string
131
    {
132 8
        return $this->_domain;
133
    }
134
135
    /**
136
     * Returns the local part of JID.
137
     *
138
     * @return string
139
     */
140 5
    public function getLocal() : string
141
    {
142 5
        return $this->_local;
143
    }
144
145
    /**
146
     * Returns resource part of address or null if resource is not set
147
     *
148
     * @return null|string
149
     */
150 6
    public function getResource()
151
    {
152 6
        return $this->_resource;
153
    }
154
155 1
    public function bare()
156
    {
157 1
        return new static($this->domain, $this->local, null);
158
    }
159
160 1
    public function isBare() : bool
161
    {
162 1
        return $this->_resource === null;
163
    }
164
165 1
    public function isFull() : bool
166
    {
167 1
        return $this->_resource !== null && $this->_local !== null;
168
    }
169
}
170