Completed
Branch FET/11251/url-vo (5ad00b)
by
unknown
165:47 queued 154:48
created

Url::queryStringArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\values;
4
5
use InvalidArgumentException;
6
7
defined('EVENT_ESPRESSO_VERSION') || exit;
8
9
10
11
/**
12
 * Class Url
13
 * Immutable Value Object representing a URL
14
 * But just a really simple representation
15
 * ie: does not fully support FTP or authority (username, password, port)
16
 *
17
 * @package EventEspresso\core\domain\values
18
 * @author  Brent Christensen
19
 * @since   4.9.54
20
 */
21
class Url
22
{
23
24
    /**
25
     * @var string $scheme
26
     */
27
    private $scheme;
28
29
    /**
30
     * @var string $host
31
     */
32
    private $host;
33
34
    /**
35
     * @var string $path
36
     */
37
    private $path;
38
39
    /**
40
     * @var string $query_string
41
     */
42
    private $query_string;
43
44
    /**
45
     * @var string $fragment
46
     */
47
    private $fragment;
48
49
50
    /**
51
     * Url constructor.
52
     *
53
     * @param $url
54
     * @throws InvalidArgumentException
55
     */
56
    public function __construct($url)
57
    {
58
        if (
59
            ! filter_var(
60
                $url,
61
                FILTER_VALIDATE_URL,
62
                array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED)
63
            )
64
        ) {
65
            throw new InvalidArgumentException(esc_html__('Invalid URL. Both the "Scheme" and "Host" are required.',
66
                'event_espresso'));
67
        }
68
        $url = parse_url($url);
69
        $this->setScheme($url);
70
        $this->setHost($url);
71
        $this->setPath($url);
72
        $this->setQueryString($url);
73
        $this->setFragment($url);
74
    }
75
76
77
    /**
78
     * @return string
79
     */
80
    public function scheme()
81
    {
82
        return $this->scheme;
83
    }
84
85
86
    /**
87
     * @param array $url
88
     */
89
    private function setScheme($url)
90
    {
91
        $this->scheme = $url['scheme'] . '://';
92
    }
93
94
95
    /**
96
     * @return string
97
     */
98
    public function host()
99
    {
100
        return $this->host;
101
    }
102
103
104
    /**
105
     * @param array $url
106
     */
107
    private function setHost($url)
108
    {
109
        $this->host = $url['host'];
110
    }
111
112
113
    /**
114
     * @return string
115
     */
116
    public function path()
117
    {
118
        return $this->path;
119
    }
120
121
122
    /**
123
     * @param array $url
124
     */
125
    private function setPath($url)
126
    {
127
        $this->path = isset($url['path']) ? $url['path'] : '';
128
    }
129
130
131
    /**
132
     * @return string
133
     */
134
    public function queryString()
135
    {
136
        return $this->query_string !== '' ? '?' . $this->query_string : '';
137
    }
138
139
140
    /**
141
     * @return array
142
     */
143
    public function queryStringArray()
144
    {
145
        return wp_parse_args($this->query_string);
146
    }
147
148
149
    /**
150
     * @param array $url
151
     */
152
    private function setQueryString($url)
153
    {
154
        $this->query_string = isset($url['query']) ? $url['query'] : '';
155
    }
156
157
158
    /**
159
     * @return string
160
     */
161
    public function fragment()
162
    {
163
        return $this->fragment !== '' ? '#' . $this->fragment : '';
164
    }
165
166
167
    /**
168
     * @param array $url
169
     */
170
    private function setFragment($url)
171
    {
172
        $this->fragment = isset($url['fragment']) ? $url['fragment'] : '';
173
    }
174
175
176
    /**
177
     * @return string
178
     */
179
    public function getFullUrl()
180
    {
181
        return $this->scheme() . $this->host() . $this->path() . $this->queryString() . $this->fragment();
182
    }
183
184
185
    /**
186
     * @return string
187
     */
188
    public function __toString()
189
    {
190
        return $this->getFullUrl();
191
    }
192
}
193