Address   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
eloc 50
c 1
b 0
f 0
dl 0
loc 156
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A absAllowed() 0 3 2
A rel() 0 16 5
A setPort() 0 4 2
A abs() 0 17 5
B __set() 0 11 7
A __get() 0 3 1
A __construct() 0 8 3
A setQuery() 0 6 3
1
<?php
2
3
namespace veejay\address;
4
5
/**
6
 * Class Address
7
 * @package veejay\address
8
 *
9
 * @property string|null $scheme
10
 * @property string|null $host
11
 * @property int|null $port
12
 * @property string|null $path
13
 * @property array $query
14
 * @property string|null $fragment
15
 */
16
class Address
17
{
18
    /**
19
     * http://domain.ru:80/index.php?param=1#anchor - http
20
     * @var string|null
21
     */
22
    protected $scheme;
23
24
    /**
25
     * http://domain.ru:80/index.php?param=1#anchor - domain.ru
26
     * @var string|null
27
     */
28
    protected $host;
29
30
    /**
31
     * http://domain.ru:80/index.php?param=1#anchor - 80
32
     * @var int|null
33
     */
34
    protected $port;
35
36
    /**
37
     * http://domain.ru:80/index.php?param=1#anchor - /index.php
38
     * @var string|null
39
     */
40
    protected $path;
41
42
    /**
43
     * http://domain.ru:80/index.php?param=1#anchor - [param => 1]
44
     * @var array
45
     */
46
    protected $query = [];
47
48
    /**
49
     * http://domain.ru:80/index.php?param=1#anchor - anchor
50
     * @var string|null
51
     */
52
    protected $fragment;
53
54
    /**
55
     * @param string $url - address than can be parsed by function parse_url()
56
     */
57
    public function __construct(string $url)
58
    {
59
        $parts = parse_url($url);
60
        foreach ($parts as $key => $value) {
61
            if ($key == 'query') {
62
                parse_str($value, $value);
63
            }
64
            $this->__set($key, $value);
65
        }
66
    }
67
68
    /**
69
     * @param string $name
70
     * @param mixed $value
71
     */
72
    public function __set($name, $value)
73
    {
74
        if ($name == 'port') {
75
            $this->setPort($value);
76
        } elseif ($name == 'query') {
77
            $this->setQuery($value);
78
        } elseif (property_exists($this, $name)) {
79
            if (is_null($value) || $value == '') {
80
                $this->$name = null;
81
            } elseif (is_string($value)) {
82
                $this->$name = $value;
83
            }
84
        }
85
    }
86
87
    /**
88
     * @param string $name
89
     * @return mixed
90
     */
91
    public function __get($name)
92
    {
93
        return $this->$name;
94
    }
95
96
    /**
97
     * Generate relative url.
98
     * @return string
99
     */
100
    public function rel(): string
101
    {
102
        $str = '';
103
        if ($this->path !== null) {
104
            $str .= $this->path;
105
        }
106
        if (!empty($this->query)) {
107
            $str .= '?' . http_build_query($this->query, '', '&');
108
        }
109
        if ($this->fragment !== null) {
110
            $str .= '#' . $this->fragment;
111
        }
112
        if (substr($str, 0, 1) != '/') {
113
            $str = '/' . $str;
114
        }
115
        return $str;
116
    }
117
118
    /**
119
     * Generate absolute url.
120
     * @return string
121
     */
122
    public function abs(): string
123
    {
124
        $str = '';
125
        if ($this->scheme !== null) {
126
            $str .= $this->scheme . '://';
127
        }
128
        if ($this->host !== null) {
129
            $str .= $this->host;
130
        }
131
        if ($this->port !== null) {
132
            $str .= ':' . $this->port;
133
        }
134
        if (!$this->absAllowed()) {
135
            $str = '';
136
        }
137
        $str .= $this->rel();
138
        return $str;
139
    }
140
141
    /**
142
     * Is absolute url allowed.
143
     * @return bool
144
     */
145
    public function absAllowed(): bool
146
    {
147
        return $this->scheme !== null && $this->host !== null;
148
    }
149
150
    /**
151
     * Set port value.
152
     * @param mixed $value
153
     * @return void
154
     */
155
    protected function setPort($value)
156
    {
157
        settype($value, 'int');
158
        $this->port = empty($value) ? null : $value;
159
    }
160
161
    /**
162
     * Set query value.
163
     * @param mixed $value
164
     * @return void
165
     */
166
    protected function setQuery($value)
167
    {
168
        if (empty($value)) {
169
            $this->query = [];
170
        } elseif (is_array($value)) {
171
            $this->query = $value;
172
        }
173
    }
174
}
175