composer package updates
[openemr.git] / vendor / zendframework / zend-uri / src / UriFactory.php
blob79a31247910a272da351edce67cf5a04772af854
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-uri for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-uri/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Uri;
10 /**
11 * URI Factory Class
13 * The URI factory can be used to generate URI objects from strings, using a
14 * different URI subclass depending on the input URI scheme. New scheme-specific
15 * classes can be registered using the registerScheme() method.
17 * Note that this class contains only static methods and should not be
18 * instantiated
20 abstract class UriFactory
22 /**
23 * Registered scheme-specific classes
25 * @var array
27 protected static $schemeClasses = [
28 'http' => 'Zend\Uri\Http',
29 'https' => 'Zend\Uri\Http',
30 'mailto' => 'Zend\Uri\Mailto',
31 'file' => 'Zend\Uri\File',
32 'urn' => 'Zend\Uri\Uri',
33 'tag' => 'Zend\Uri\Uri',
36 /**
37 * Register a scheme-specific class to be used
39 * @param string $scheme
40 * @param string $class
42 public static function registerScheme($scheme, $class)
44 $scheme = strtolower($scheme);
45 static::$schemeClasses[$scheme] = $class;
48 /**
49 * Unregister a scheme
51 * @param string $scheme
53 public static function unregisterScheme($scheme)
55 $scheme = strtolower($scheme);
56 if (isset(static::$schemeClasses[$scheme])) {
57 unset(static::$schemeClasses[$scheme]);
61 /**
62 * Get the class name for a registered scheme
64 * If provided scheme is not registered, will return NULL
66 * @param string $scheme
67 * @return string|null
69 public static function getRegisteredSchemeClass($scheme)
71 if (isset(static::$schemeClasses[$scheme])) {
72 return static::$schemeClasses[$scheme];
75 return;
78 /**
79 * Create a URI from a string
81 * @param string $uriString
82 * @param string $defaultScheme
83 * @throws Exception\InvalidArgumentException
84 * @return \Zend\Uri\Uri
86 public static function factory($uriString, $defaultScheme = null)
88 if (! is_string($uriString)) {
89 throw new Exception\InvalidArgumentException(sprintf(
90 'Expecting a string, received "%s"',
91 (is_object($uriString) ? get_class($uriString) : gettype($uriString))
92 ));
95 $uri = new Uri($uriString);
96 $scheme = strtolower($uri->getScheme());
97 if (! $scheme && $defaultScheme) {
98 $scheme = $defaultScheme;
101 if ($scheme && ! isset(static::$schemeClasses[$scheme])) {
102 throw new Exception\InvalidArgumentException(sprintf(
103 'no class registered for scheme "%s"',
104 $scheme
107 if ($scheme && isset(static::$schemeClasses[$scheme])) {
108 $class = static::$schemeClasses[$scheme];
109 $uri = new $class($uri);
110 if (! $uri instanceof UriInterface) {
111 throw new Exception\InvalidArgumentException(
112 sprintf(
113 'class "%s" registered for scheme "%s" does not implement Zend\Uri\UriInterface',
114 $class,
115 $scheme
121 return $uri;