Completed
Pull Request — master (#41)
by Sebastian
03:51 queued 02:03
created

Url::hasCrawlableScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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