Skip to content

Commit b2b9b10

Browse files
committed
Added a MultipartStreamInterface and fixed some bugs
1 parent eb5b482 commit b2b9b10

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/MultipartStream.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author Michael Dowling and contributors to guzzlehttp/psr7
99
* @author Tobias Nyholm <[email protected]>
1010
*/
11-
class MultipartStream implements StreamInterface
11+
class MultipartStream implements MultipartStreamInterface
1212
{
1313
/**
1414
* @var StreamInterface[] Streams being decorated
@@ -41,7 +41,8 @@ class MultipartStream implements StreamInterface
4141
private $boundary;
4242

4343
/**
44-
* @param StreamInterface[] $streams Streams to decorate. Each stream must be readable.
44+
* @param StreamInterface[] $streams Streams to decorate. Each stream must be readable. The streams should also
45+
* include the boundary.
4546
* @param string $boundary
4647
*/
4748
public function __construct(array $streams = [], $boundary)
@@ -63,13 +64,13 @@ public function __toString()
6364
}
6465

6566
/**
66-
* Add a stream to the AppendStream
67+
* Add a stream to the MultipartStream
6768
*
6869
* @param StreamInterface $stream Stream to append. Must be readable.
6970
*
7071
* @throws \InvalidArgumentException if the stream is not readable
7172
*/
72-
public function addStream(StreamInterface $stream)
73+
private function addStream(StreamInterface $stream)
7374
{
7475
if (!$stream->isReadable()) {
7576
throw new \InvalidArgumentException('Each stream must be readable');
@@ -83,9 +84,31 @@ public function addStream(StreamInterface $stream)
8384
$this->streams[] = $stream;
8485
}
8586

87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function getBoundary()
91+
{
92+
return $this->boundary;
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
8698
public function getContents()
8799
{
88-
return copy_to_string($this);
100+
$buffer = '';
101+
102+
while (!$this->eof()) {
103+
$buf = $this->read(1048576);
104+
// Using a loose equality here to match on '' and false.
105+
if ($buf == null) {
106+
break;
107+
}
108+
$buffer .= $buf;
109+
}
110+
111+
return $buffer;
89112
}
90113

91114
/**

src/MultipartStreamInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Http\Message;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
7+
interface MultipartStreamInterface extends StreamInterface
8+
{
9+
/**
10+
* Return the boundary that separates streams.
11+
* @return string
12+
*/
13+
public function getBoundary();
14+
}

0 commit comments

Comments
 (0)