Changeset 1496
- Timestamp:
- 04/04/08 22:57:25 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/stubURL.php
r1485 r1496 37 37 { 38 38 $this->url = parse_url($url); 39 if (isset($this->url['host']) == true) {39 if (isset($this->url['host']) === true) { 40 40 $this->url['host'] = strtolower($this->url['host']); 41 41 } … … 44 44 // will lead to an unset $this->url['pass'] which is wrong 45 45 // due to RFC1738 3.1, it has to be an empty string 46 if (isset($this->url['user']) == true && isset($this->url['pass']) == false && $this->get(true) != $url) {46 if (isset($this->url['user']) === true && isset($this->url['pass']) === false && $this->get(true) !== $url) { 47 47 $this->url['pass'] = ''; 48 48 } 49 49 50 if ($this->hasQuery() == true) {50 if ($this->hasQuery() === true) { 51 51 parse_str($this->url['query'], $this->params); 52 52 } … … 62 62 public static function fromString($urlString) 63 63 { 64 if (strlen($urlString) == 0) {64 if (strlen($urlString) === 0) { 65 65 return null; 66 66 } 67 67 68 68 $url = new self($urlString); 69 if ($url->isValid() == false) {69 if ($url->isValid() === false) { 70 70 throw new stubMalformedURLException('The URL ' . $urlString . ' is not a valid URL.'); 71 71 } … … 81 81 public function isValid() 82 82 { 83 if (strlen($this->get()) == 0) {83 if (strlen($this->get()) === 0) { 84 84 return false; 85 85 } 86 86 87 if (isset($this->url['scheme']) == false) {87 if (isset($this->url['scheme']) === false) { 88 88 return false; 89 89 } 90 90 91 if (isset($this->url['user']) == true) {91 if (isset($this->url['user']) === true) { 92 92 if (ereg('([@:/])', $this->url['user']) == true) { 93 93 return false; 94 94 } 95 95 96 if (isset($this->url['pass']) == true && ereg('([@:/])', $this->url['pass']) == true) {96 if (isset($this->url['pass']) === true && ereg('([@:/])', $this->url['pass']) == true) { 97 97 return false; 98 98 } … … 100 100 101 101 // if host is set and seems to comply with host character rules or host is localhost syntax is ok 102 if (isset($this->url['host']) == true && (eregi('([a-z0-9-]*)\.([a-z]{2,4})', $this->url['host']) == true || eregi('([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})', $this->url['host']) == true || 'localhost' == $this->url['host'])) { 102 if (isset($this->url['host']) === true && (eregi('([a-z0-9-]*)\.([a-z]{2,4})', $this->url['host']) == true || eregi('([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})', $this->url['host']) == true || 'localhost' == $this->url['host'])) { 103 return true; 104 } elseif (isset($this->url['host']) === false || strlen($this->url['host']) === 0) { 103 105 return true; 104 106 } … … 133 135 } 134 136 135 if (checkdnsrr($this->url['host'], 'ANY') == true || checkdnsrr($this->url['host'], 'MX')== true) {137 if (checkdnsrr($this->url['host'], 'ANY') === true || checkdnsrr($this->url['host'], 'MX') === true) { 136 138 return true; 137 139 } … … 150 152 $url = ''; 151 153 $user = ''; 152 if (isset($this->url['user']) == true) {154 if (isset($this->url['user']) === true) { 153 155 $user = $this->url['user']; 154 if (isset($this->url['pass']) == true) {156 if (isset($this->url['pass']) === true) { 155 157 $user .= ':' . $this->url['pass']; 156 158 } … … 159 161 } 160 162 161 if (true == $port && isset($this->url['port']) == true) {163 if (true == $port && isset($this->url['port']) === true) { 162 164 $port = ':' . $this->url['port']; 163 165 } else { … … 165 167 } 166 168 167 if (isset($this->url['scheme']) == true && isset($this->url['host']) == true) { 168 $url = $this->url['scheme'] . '://' . $user . $this->url['host'] . $port; 169 if (isset($this->url['path']) == true) { 169 if (isset($this->url['scheme']) === true) { 170 $url = $this->url['scheme'] . '://'; 171 if (isset($this->url['host']) === true) { 172 $url .= $user . $this->url['host'] . $port; 173 } 174 175 if (isset($this->url['path']) === true) { 170 176 $url .= $this->url['path']; 171 177 } 172 178 } 173 179 174 if ($this->hasQuery() == true) {180 if ($this->hasQuery() === true) { 175 181 $url .= '?' . $this->url['query']; 176 182 } … … 190 196 public function getScheme() 191 197 { 192 if (isset($this->url['scheme']) == true) {198 if (isset($this->url['scheme']) === true) { 193 199 return $this->url['scheme']; 194 200 } … … 205 211 public function getUser($defaultUser = null) 206 212 { 207 if (isset($this->url['user']) == true) {213 if (isset($this->url['user']) === true) { 208 214 return $this->url['user']; 209 215 } … … 220 226 public function getPassword($defaultPassword = null) 221 227 { 222 if (isset($this->url['pass']) == true) {228 if (isset($this->url['pass']) === true) { 223 229 return $this->url['pass']; 224 230 } … … 235 241 public function getHost($defaultHost = null) 236 242 { 237 if (isset($this->url['host']) == true) {243 if (isset($this->url['host']) === true) { 238 244 return $this->url['host']; 239 245 } … … 250 256 public function getPort($defaultPort = null) 251 257 { 252 if (isset($this->url['port']) == true) {258 if (isset($this->url['port']) === true) { 253 259 return $this->url['port']; 254 260 } … … 274 280 public function getPath() 275 281 { 276 if (isset($this->url['path']) == false) {282 if (isset($this->url['path']) === false) { 277 283 return null; 278 284 } 279 285 280 if ($this->hasQuery() == true) {286 if ($this->hasQuery() === true) { 281 287 return $this->url['path'] . '?' . $this->url['query']; 282 288 } … … 292 298 public function hasQuery() 293 299 { 294 return (isset($this->url['query']) == true && strlen($this->url['query']) > 0);300 return (isset($this->url['query']) === true && strlen($this->url['query']) > 0); 295 301 } 296 302 … … 304 310 public function getParam($name, $defaultValue = null) 305 311 { 306 if (isset($this->params[$name]) == false) {312 if (isset($this->params[$name]) === false) { 307 313 return $defaultValue; 308 314 } trunk/src/test/php/net/stubbles/peer/stubURLTestCase.php
r1485 r1496 214 214 215 215 /** 216 * assure that values are returned the expected way 217 * 218 * @test 219 */ 220 public function valueIPNoPath() 221 { 222 $url = stubURL::fromString('http://127.0.0.1'); 223 $this->assertTrue($url->isValid()); 224 $this->assertEquals('http://127.0.0.1', $url->get()); 225 $this->assertEquals('http', $url->getScheme()); 226 $this->assertNull($url->getUser()); 227 $this->assertEquals('foo', $url->getUser('foo')); 228 $this->assertNull($url->getPassword()); 229 $this->assertEquals('foo', $url->getPassword('foo')); 230 $this->assertEquals('127.0.0.1', $url->getHost()); 231 $this->assertNull($url->getPort()); 232 $this->assertEquals(313, $url->getPort(313)); 233 $this->assertNull($url->getPath()); 234 $this->assertFalse($url->hasQuery()); 235 $this->assertTrue($url->checkDNS()); 236 } 237 238 /** 239 * assure that values are returned the expected way 240 * 241 * @test 242 */ 243 public function fileURL() 244 { 245 $url = stubURL::fromString('file:///home'); 246 $this->assertTrue($url->isValid()); 247 $this->assertEquals('file:///home', $url->get()); 248 $this->assertEquals('file', $url->getScheme()); 249 $this->assertNull($url->getUser()); 250 $this->assertEquals('foo', $url->getUser('foo')); 251 $this->assertNull($url->getPassword()); 252 $this->assertEquals('foo', $url->getPassword('foo')); 253 $this->assertNull($url->getHost()); 254 $this->assertEquals('127.0.0.1', $url->getHost('127.0.0.1')); 255 $this->assertNull($url->getPort()); 256 $this->assertEquals(313, $url->getPort(313)); 257 $this->assertEquals('/home', $url->getPath()); 258 $this->assertFalse($url->hasQuery()); 259 $this->assertFalse($url->checkDNS()); 260 } 261 262 /** 216 263 * assure that wrong values trigger an exception 217 264 *
