Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8cce62e

Browse files
committed
Merge branch 'hotfix/96'
Close #96
2 parents f7b1017 + ca416e6 commit 8cce62e

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#96](https://github.com/zendframework/zend-diactoros/pull/96) updates
22+
`withPort()` to allow `null` port values (indicating usage of default for
23+
the given scheme).
2224

2325
## 1.1.3 - 2015-08-10
2426

src/Uri.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,23 @@ public function withHost($host)
306306
*/
307307
public function withPort($port)
308308
{
309-
if (! is_numeric($port)) {
309+
if (! is_numeric($port) && $port !== null) {
310310
throw new InvalidArgumentException(sprintf(
311-
'Invalid port "%s" specified; must be an integer or integer string',
311+
'Invalid port "%s" specified; must be an integer, an integer string, or null',
312312
(is_object($port) ? get_class($port) : gettype($port))
313313
));
314314
}
315315

316-
$port = (int) $port;
316+
if ($port !== null) {
317+
$port = (int) $port;
318+
}
317319

318320
if ($port === $this->port) {
319321
// Do nothing if no change was made.
320322
return clone $this;
321323
}
322324

323-
if ($port < 1 || $port > 65535) {
325+
if ($port !== null && $port < 1 || $port > 65535) {
324326
throw new InvalidArgumentException(sprintf(
325327
'Invalid port "%d" specified; must be a valid TCP/UDP port',
326328
$port

test/UriTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function testWithHostReturnsNewInstanceWithProvidedHost()
7373
public function validPorts()
7474
{
7575
return [
76+
'null' => [ null ],
7677
'int' => [ 3000 ],
7778
'string' => [ "3000" ]
7879
];
@@ -88,15 +89,14 @@ public function testWithPortReturnsNewInstanceWithProvidedPort($port)
8889
$this->assertNotSame($uri, $new);
8990
$this->assertEquals($port, $new->getPort());
9091
$this->assertEquals(
91-
sprintf('https://user:[email protected]:%d/foo?bar=baz#quz', $port),
92+
sprintf('https://user:[email protected]%s/foo?bar=baz#quz', $port === null ? '' : ':' . $port),
9293
(string) $new
9394
);
9495
}
9596

9697
public function invalidPorts()
9798
{
9899
return [
99-
'null' => [ null ],
100100
'true' => [ true ],
101101
'false' => [ false ],
102102
'string' => [ 'string' ],

0 commit comments

Comments
 (0)