Completed
Push — master ( 7a5aed...4799e1 )
by Kacper
06:25
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
use Kadet\Xmpp\Exception\InvalidArgumentException;
19
use Kadet\Xmpp\Utils\Accessors;
20
use Kadet\Xmpp\Utils\Immutable;
21
22
/**
23
 * Class Jid
24
 * @package Kadet\Xmpp
25
 *
26
 * @property-read string $domain
27
 * @property-read string $local
28
 * @property-read string $resource
29
 */
30
class Jid implements Immutable
31
{
32
    use Accessors;
33
34
    private $_local;
35
36
    /**
37
     * @var string
38
     */
39
    private $_domain;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $_resource;
45
46 15
    public function __construct(string $address, string $local = null, string $resource = null)
47
    {
48 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...
49 11
            list($address, $local, $resource) = self::_split($address);
50
        }
51
52 15
        self::validate($address, $local, $resource);
53
54 11
        $this->_domain   = $address;
55 11
        $this->_local    = $local;
56 11
        $this->_resource = $resource;
57 11
    }
58
59 20
    private static function _split(string $address)
60
    {
61 20
        preg_match('#^(?:(?P<local>[^@]+)@)?(?P<host>.*?)(?:/(?P<resource>.+?))?$#i', $address, $result);
62
63 20
        return [$result['host'], $result['local'] ?: null, $result['resource'] ?? null];
64
    }
65
66
    /**
67
     * Validates address and throws InvalidArgumentException in case of failure.
68
     *
69
     * @param string      $address
70
     * @param string      $local
71
     * @param string|null $resource
72
     * @return bool
73
     */
74 34
    public static function validate(string $address, string $local = null, string $resource = null)
75
    {
76 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...
77 15
            list($address, $local, $resource) = self::_split($address);
78
        }
79
80 34
        if (empty($address)) {
81 9
            throw new InvalidArgumentException("Domain-part of JID is REQUIRED");
82
        }
83
84 25
        if (preg_match('#[<>:&"\'/@]#i', $address, $match) !== 0) {
85 3
            throw new InvalidArgumentException("Domain-part of JID contains not allowed character '{$match[0]}'");
86
        }
87
88 22
        if ($local !== null && preg_match('#[<>:&"\'/@]#i', $local, $match) !== 0) {
89 2
            throw new InvalidArgumentException("Local-part of JID contains not allowed character '{$match[0]}'");
90
        }
91
92 20
        if ($resource !== null && preg_match('#[<>:&"\'/]#i', $resource, $match) !== 0) {
93 3
            throw new InvalidArgumentException("Resource-part of JID contains not allowed character '{$match[0]}'");
94
        }
95
96 17
        return true;
97
    }
98
99
    /**
100
     * Returns if address is valid or not.
101
     *
102
     * @param string      $address
103
     * @param string      $local
104
     * @param string|null $resource
105
     * @return bool
106
     */
107 17
    public static function isValid(string $address, string $local = null, string $resource = null) : bool
108
    {
109
        try {
110 17
            return self::validate($address, $local, $resource);
111 11
        } catch (InvalidArgumentException $exception) {
112 11
            return false;
113
        }
114
    }
115
116 1
    public function __toString() : string
117
    {
118
        return
119 1
            ($this->_local ? "{$this->_local}@" : null)
120 1
            . $this->_domain
121 1
            . ($this->_resource ? "/{$this->_resource}" : null);
122
    }
123
124
    /**
125
     * Returns the domain part of address.
126
     *
127
     * @return string
128
     */
129 8
    public function getDomain() : string
130
    {
131 8
        return $this->_domain;
132
    }
133
134
    /**
135
     * Returns the local part of JID.
136
     *
137
     * @return string
138
     */
139 5
    public function getLocal() : string
140
    {
141 5
        return $this->_local;
142
    }
143
144
    /**
145
     * Returns resource part of address or null if resource is not set
146
     *
147
     * @return null|string
148
     */
149 6
    public function getResource()
150
    {
151 6
        return $this->_resource;
152
    }
153
154 1
    public function bare()
155
    {
156 1
        return new static($this->domain, $this->local, null);
157
    }
158
159 1
    public function isBare() : bool
160
    {
161 1
        return $this->_resource === null;
162
    }
163
164 1
    public function isFull() : bool
165
    {
166 1
        return $this->_resource !== null && $this->_local !== null;
167
    }
168
}
169