From 5265147265a13cbb201488130882c22d2597ec9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Wed, 4 Jan 2017 03:00:07 +0100 Subject: [PATCH 1/2] Add check for empty string to stream factories --- CHANGELOG.md | 4 ++++ src/StreamFactory/DiactorosStreamFactory.php | 2 +- src/StreamFactory/SlimStreamFactory.php | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60cf5a6..8749fd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Unreleased +## Added + +- Check for empty string in Stream factories + ## Fixed - FilteredStream::getSize returns null because the contents size is unknown. diff --git a/src/StreamFactory/DiactorosStreamFactory.php b/src/StreamFactory/DiactorosStreamFactory.php index 21690de..c1eb6fd 100644 --- a/src/StreamFactory/DiactorosStreamFactory.php +++ b/src/StreamFactory/DiactorosStreamFactory.php @@ -24,7 +24,7 @@ public function createStream($body = null) } else { $stream = new Stream('php://memory', 'rw'); - if (null !== $body) { + if (null !== $body || '' !== $body) { $stream->write((string) $body); } diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php index e779f3b..167452b 100644 --- a/src/StreamFactory/SlimStreamFactory.php +++ b/src/StreamFactory/SlimStreamFactory.php @@ -28,7 +28,7 @@ public function createStream($body = null) $resource = fopen('php://memory', 'r+'); $stream = new Stream($resource); - if (null !== $body) { + if (null !== $body || '' !== $body) { $stream->write((string) $body); } } From e9c10414eb1b72314d98b34c4cb8709f8dce237d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Wed, 4 Jan 2017 19:26:08 +0100 Subject: [PATCH 2/2] Return stream when body is empty --- src/StreamFactory/DiactorosStreamFactory.php | 6 ++++-- src/StreamFactory/SlimStreamFactory.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/StreamFactory/DiactorosStreamFactory.php b/src/StreamFactory/DiactorosStreamFactory.php index c1eb6fd..d3bed9d 100644 --- a/src/StreamFactory/DiactorosStreamFactory.php +++ b/src/StreamFactory/DiactorosStreamFactory.php @@ -24,10 +24,12 @@ public function createStream($body = null) } else { $stream = new Stream('php://memory', 'rw'); - if (null !== $body || '' !== $body) { - $stream->write((string) $body); + if (null === $body || '' === $body) { + return $stream; } + $stream->write((string) $body); + $body = $stream; } } diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php index 167452b..32b9ac7 100644 --- a/src/StreamFactory/SlimStreamFactory.php +++ b/src/StreamFactory/SlimStreamFactory.php @@ -28,9 +28,11 @@ public function createStream($body = null) $resource = fopen('php://memory', 'r+'); $stream = new Stream($resource); - if (null !== $body || '' !== $body) { - $stream->write((string) $body); + if (null === $body || '' === $body) { + return $stream; } + + $stream->write((string) $body); } $stream->rewind();