comparison vendor/zendframework/zend-feed/src/Uri.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Feed;
11
12 class Uri
13 {
14 /**
15 * @var string
16 */
17 protected $fragment;
18
19 /**
20 * @var string
21 */
22 protected $host;
23
24 /**
25 * @var string
26 */
27 protected $pass;
28
29 /**
30 * @var string
31 */
32 protected $path;
33
34 /**
35 * @var int
36 */
37 protected $port;
38
39 /**
40 * @var string
41 */
42 protected $query;
43
44 /**
45 * @var string
46 */
47 protected $scheme;
48
49 /**
50 * @var string
51 */
52 protected $user;
53
54 /**
55 * @var bool
56 */
57 protected $valid;
58
59 /**
60 * Valid schemes
61 */
62 protected $validSchemes = [
63 'http',
64 'https',
65 'file',
66 ];
67
68 /**
69 * @param string $uri
70 */
71 public function __construct($uri)
72 {
73 $parsed = parse_url($uri);
74 if (false === $parsed) {
75 $this->valid = false;
76 return;
77 }
78
79 $this->scheme = isset($parsed['scheme']) ? $parsed['scheme'] : null;
80 $this->host = isset($parsed['host']) ? $parsed['host'] : null;
81 $this->port = isset($parsed['port']) ? $parsed['port'] : null;
82 $this->user = isset($parsed['user']) ? $parsed['user'] : null;
83 $this->pass = isset($parsed['pass']) ? $parsed['pass'] : null;
84 $this->path = isset($parsed['path']) ? $parsed['path'] : null;
85 $this->query = isset($parsed['query']) ? $parsed['query'] : null;
86 $this->fragment = isset($parsed['fragment']) ? $parsed['fragment'] : null;
87 }
88
89 /**
90 * Create an instance
91 *
92 * Useful for chained validations
93 *
94 * @param string $uri
95 * @return self
96 */
97 public static function factory($uri)
98 {
99 return new static($uri);
100 }
101
102 /**
103 * Retrieve the host
104 *
105 * @return string
106 */
107 public function getHost()
108 {
109 return $this->host;
110 }
111
112 /**
113 * Retrieve the URI path
114 *
115 * @return string
116 */
117 public function getPath()
118 {
119 return $this->path;
120 }
121
122 /**
123 * Retrieve the scheme
124 *
125 * @return string
126 */
127 public function getScheme()
128 {
129 return $this->scheme;
130 }
131
132 /**
133 * Is the URI valid?
134 *
135 * @return bool
136 */
137 public function isValid()
138 {
139 if (false === $this->valid) {
140 return false;
141 }
142
143 if ($this->scheme && !in_array($this->scheme, $this->validSchemes)) {
144 return false;
145 }
146
147 if ($this->host) {
148 if ($this->path && substr($this->path, 0, 1) != '/') {
149 return false;
150 }
151 return true;
152 }
153
154 // no host, but user and/or port... what?
155 if ($this->user || $this->port) {
156 return false;
157 }
158
159 if ($this->path) {
160 // Check path-only (no host) URI
161 if (substr($this->path, 0, 2) == '//') {
162 return false;
163 }
164 return true;
165 }
166
167 if (! ($this->query || $this->fragment)) {
168 // No host, path, query or fragment - this is not a valid URI
169 return false;
170 }
171
172 return true;
173 }
174
175 /**
176 * Is the URI absolute?
177 *
178 * @return bool
179 */
180 public function isAbsolute()
181 {
182 return ($this->scheme !== null);
183 }
184 }