1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Keppler\Url\Scheme; |
5
|
|
|
|
6
|
|
|
use Keppler\Url\Scheme\Schemes\Ftp\FtpImmutable; |
7
|
|
|
use Keppler\Url\Scheme\Schemes\Http\HttpImmutable; |
8
|
|
|
use Keppler\Url\Scheme\Schemes\Https\HttpsImmutable; |
9
|
|
|
use Keppler\Url\Scheme\Schemes\Mailto\MailtoImmutable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Note: This class should not make any attempt to sanitize, correct or |
13
|
|
|
* otherwise improve the information it receives. It will be take AS IS and |
14
|
|
|
* parsed with exactly what it gets any sort of errors that result from that |
15
|
|
|
* are the concern of the caller not this class |
16
|
|
|
* |
17
|
|
|
* Class Scheme |
18
|
|
|
* |
19
|
|
|
* @package Keppler\Url\Scheme |
20
|
|
|
*/ |
21
|
|
|
class Scheme |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param string $url |
25
|
|
|
* |
26
|
|
|
* @return MailtoImmutable |
27
|
|
|
*/ |
28
|
|
|
public static function mailto(string $url) |
29
|
|
|
{ |
30
|
|
|
$url = trim($url); |
31
|
|
|
$parsed = self::parse($url); |
32
|
|
|
|
33
|
|
|
if (MailtoImmutable::SCHEME === $parsed['scheme']) { |
34
|
|
|
return new MailtoImmutable($url); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid scheme provided for %s, expected "%s" got "%s"', |
38
|
|
|
MailtoImmutable::class, MailtoImmutable::SCHEME, |
39
|
|
|
$parsed['scheme'])); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $url |
44
|
|
|
* |
45
|
|
|
* @return HttpsImmutable |
46
|
|
|
*/ |
47
|
|
|
public static function https(string $url) |
48
|
|
|
{ |
49
|
|
|
$url = trim($url); |
50
|
|
|
$parsed = self::parse($url); |
51
|
|
|
|
52
|
|
|
if (HttpsImmutable::SCHEME === $parsed['scheme']) { |
53
|
|
|
return new HttpsImmutable($url); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid scheme provided for %s, expected "%s" got "%s"', |
57
|
|
|
HttpsImmutable::class, HttpsImmutable::SCHEME, $parsed['scheme'])); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $url |
62
|
|
|
* |
63
|
|
|
* @return HttpImmutable |
64
|
|
|
*/ |
65
|
|
|
public static function http(string $url) |
66
|
|
|
{ |
67
|
|
|
$url = trim($url); |
68
|
|
|
$parsed = self::parse($url); |
69
|
|
|
|
70
|
|
|
if (HttpImmutable::SCHEME === $parsed['scheme']) { |
71
|
|
|
return new HttpImmutable($url); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid scheme provided for %s, expected "%s" got "%s"', |
75
|
|
|
HttpImmutable::class, HttpImmutable::SCHEME, $parsed['scheme'])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $url |
80
|
|
|
* |
81
|
|
|
* @return FtpImmutable |
82
|
|
|
*/ |
83
|
|
|
public static function ftp(string $url) |
84
|
|
|
{ |
85
|
|
|
$url = trim($url); |
86
|
|
|
$parsed = self::parse($url); |
87
|
|
|
|
88
|
|
|
if (FtpImmutable::SCHEME === $parsed['scheme']) { |
89
|
|
|
return new FtpImmutable($url); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid scheme provided for %s, expected "%s" got "%s"', |
93
|
|
|
FtpImmutable::class, FtpImmutable::SCHEME, $parsed['scheme'])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $url |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
private static function parse(string $url) |
102
|
|
|
{ |
103
|
|
|
$url = trim($url); |
104
|
|
|
$parsed = parse_url($url); |
105
|
|
|
|
106
|
|
|
if (false === $parsed) { |
107
|
|
|
throw new \InvalidArgumentException('The url is malformed'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!isset($parsed['scheme'])) { |
111
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to determine scheme for %s', |
112
|
|
|
$url)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $parsed; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|