1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @see https://github.com/laminas/laminas-uri for the canonical source repository
* @copyright https://github.com/laminas/laminas-uri/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-uri/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Uri;
use Laminas\Uri\File;
use Laminas\Uri\Http;
use Laminas\Uri\Mailto;
use Laminas\Uri\Uri;
use function get_class;
use function gettype;
use function is_object;
use function is_string;
use function sprintf;
use function strtolower;
/**
* URI Factory Class
*
* The URI factory can be used to generate URI objects from strings, using a
* different URI subclass depending on the input URI scheme. New scheme-specific
* classes can be registered using the registerScheme() method.
*
* Note that this class contains only static methods and should not be
* instantiated
*/
// phpcs:ignore WebimpressCodingStandard.NamingConventions.AbstractClass.Prefix
abstract class UriFactory
{
/**
* Registered scheme-specific classes
*
* @var array
*/
protected static $schemeClasses = [
'http' => Http::class,
'https' => Http::class,
'mailto' => Mailto::class,
'file' => File::class,
'urn' => Uri::class,
'tag' => Uri::class,
];
/**
* Register a scheme-specific class to be used
*
* @param string $scheme
* @param string $class
*/
public static function registerScheme($scheme, $class)
{
$scheme = strtolower($scheme);
static::$schemeClasses[$scheme] = $class;
}
/**
* Unregister a scheme
*
* @param string $scheme
*/
public static function unregisterScheme($scheme)
{
$scheme = strtolower($scheme);
if (isset(static::$schemeClasses[$scheme])) {
unset(static::$schemeClasses[$scheme]);
}
}
/**
* Get the class name for a registered scheme
*
* If provided scheme is not registered, will return NULL
*
* @param string $scheme
* @return string|null
*/
public static function getRegisteredSchemeClass($scheme)
{
if (! isset(static::$schemeClasses[$scheme])) {
return null;
}
return static::$schemeClasses[$scheme];
}
/**
* Create a URI from a string
*
* @param string $uriString
* @param string $defaultScheme
* @throws Exception\InvalidArgumentException
* @return Uri
*/
public static function factory($uriString, $defaultScheme = null)
{
if (! is_string($uriString)) {
throw new Exception\InvalidArgumentException(sprintf(
'Expecting a string, received "%s"',
is_object($uriString) ? get_class($uriString) : gettype($uriString)
));
}
$uri = new Uri($uriString);
$scheme = strtolower($uri->getScheme());
if (! $scheme && $defaultScheme) {
$scheme = $defaultScheme;
}
if ($scheme && ! isset(static::$schemeClasses[$scheme])) {
throw new Exception\InvalidArgumentException(sprintf(
'no class registered for scheme "%s"',
$scheme
));
}
if ($scheme && isset(static::$schemeClasses[$scheme])) {
$class = static::$schemeClasses[$scheme];
$uri = new $class($uri);
if (! $uri instanceof UriInterface) {
throw new Exception\InvalidArgumentException(
sprintf(
'class "%s" registered for scheme "%s" does not implement Laminas\Uri\UriInterface',
$class,
$scheme
)
);
}
}
return $uri;
}
}