Completed
Push — master ( c83bef...20fddf )
by Freek
09:52
created

Url::setHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Crawler;
4
5
class Url
6
{
7
    /**
8
     * @var null|string
9
     */
10
    public $scheme;
11
12
    /**
13
     * @var null|string
14
     */
15
    public $host;
16
17
    /**
18
     * @var int
19
     */
20
    public $port = 80;
21
22
    /**
23
     * @var null|string
24
     */
25
    public $path;
26
27
    /**
28
     * @var null|string
29
     */
30
    public $query;
31
32
    /**
33
     * @param string $url
34
     *
35
     * @return static
36
     */
37
    public static function create(string $url)
38
    {
39
        return new static($url);
40
    }
41
42
    public function __construct(string $url)
43
    {
44
        $urlProperties = parse_url($url);
45
46
        foreach (['scheme', 'host', 'path', 'port', 'query'] as $property) {
47
            if (isset($urlProperties[$property])) {
48
                $this->$property = $urlProperties[$property];
49
            }
50
        }
51
    }
52
53
    /**
54
     * Determine if the url is relative.
55
     */
56
    public function isRelative(): bool
57
    {
58
        return is_null($this->host);
59
    }
60
61
    /**
62
     * Determine if the url is protocol independent.
63
     */
64
    public function isProtocolIndependent(): bool
65
    {
66
        return is_null($this->scheme);
67
    }
68
69
    /**
70
     * Determine if this is a mailto-link.
71
     */
72
    public function isEmailUrl(): bool
73
    {
74
        return $this->scheme === 'mailto';
75
    }
76
77
    /**
78
     * Determine if this is a tel-link.
79
     */
80
    public function isTelUrl(): bool
81
    {
82
        return $this->scheme === 'tel';
83
    }
84
85
    /**
86
     * Determine if this is an inline javascript.
87
     */
88
    public function isJavascript(): bool
89
    {
90
        return $this->scheme === 'javascript';
91
    }
92
93
    /**
94
     * Set the scheme.
95
     *
96
     * @param string $scheme
97
     *
98
     * @return $this
99
     */
100
    public function setScheme(string $scheme)
101
    {
102
        $this->scheme = $scheme;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Set the host.
109
     *
110
     * @param string $host
111
     *
112
     * @return $this
113
     */
114
    public function setHost(string $host)
115
    {
116
        $this->host = $host;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param $port
123
     *
124
     * @return $this
125
     */
126
    public function setPort(int $port)
127
    {
128
        $this->port = $port;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Remove the fragment.
135
     *
136
     * @return $this
137
     */
138
    public function removeFragment()
139
    {
140
        $this->path = explode('#', $this->path)[0];
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return null|string
147
     */
148
    public function path()
149
    {
150
        return $this->path;
151
    }
152
153
    /**
154
     * @param int|null $index
155
     *
156
     * @return array|null|string
157
     */
158
    public function segments(int $index = null)
159
    {
160
        $segments = collect(explode('/', $this->path()))
161
            ->filter(function ($value) {
162
                return $value !== '';
163
            })
164
            ->values()
165
            ->toArray();
166
167
        if (! is_null($index)) {
168
            return $this->segment($index);
169
        }
170
171
        return $segments;
172
    }
173
174
    /**
175
     * @param int $index
176
     *
177
     * @return string|null
178
     */
179
    public function segment(int $index)
180
    {
181
        if (! isset($this->segments()[$index - 1])) {
182
            return;
183
        }
184
185
        return $this->segments()[$index - 1];
186
    }
187
188
    public function isEqual(Url $otherUrl): bool
189
    {
190
        return (string) $this === (string) $otherUrl;
191
    }
192
193
    /**
194
     * Convert the url to string.
195
     *
196
     * @return string
197
     */
198
    public function __toString()
199
    {
200
        $path = $this->startsWith($this->path, '/') ? substr($this->path, 1) : $this->path;
201
202
        $port = ($this->port === 80 ? '' : ":{$this->port}");
203
204
        $queryString = (is_null($this->query) ? '' : "?{$this->query}");
205
206
        return "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}";
207
    }
208
209
    /**
210
     * @param string|null $haystack
211
     * @param string|array $needles
212
     * @return bool
213
     */
214
    public function startsWith($haystack, $needles): bool
215
    {
216
        foreach ((array) $needles as $needle) {
217
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
218
                return true;
219
            }
220
        }
221
222
        return false;
223
    }
224
225
}
226