diff --git a/src/StreamedPart.php b/src/StreamedPart.php index 3fe8069..b82ac19 100644 --- a/src/StreamedPart.php +++ b/src/StreamedPart.php @@ -36,31 +36,18 @@ class StreamedPart */ private $parts = array(); - /** - * The length of the EOL character. - * - * @var int - */ - private $EOLCharacterLength; - /** * StreamParser constructor. * * @param resource $stream - * @param int $EOLCharacterLength */ - public function __construct($stream, $EOLCharacterLength = 2) + public function __construct($stream) { if (false === is_resource($stream)) { throw new \InvalidArgumentException('Input is not a stream'); } - if (false === is_integer($EOLCharacterLength)) { - throw new \InvalidArgumentException('EOL Length is not an integer'); - } - $this->stream = $stream; - $this->EOLCharacterLength = $EOLCharacterLength; // Reset the stream rewind($this->stream); @@ -145,6 +132,8 @@ public function __construct($stream, $EOLCharacterLength = 2) $partOffset = 0; $endOfBody = false; + $eofLength = 0; + while ($line = fgets($this->stream, $bufferSize)) { $trimmed = rtrim($line, "\r\n"); @@ -152,22 +141,12 @@ public function __construct($stream, $EOLCharacterLength = 2) if ($trimmed === $separator || $trimmed === $separator.'--') { if ($partOffset > 0) { $currentOffset = ftell($this->stream); - // Get end of line length (should be 2) - $eofLength = strlen($line) - strlen($trimmed); - $partLength = $currentOffset - $partOffset - strlen($trimmed) - (2 * $eofLength); - - // if we are at the end of a part, and there is no trailing new line ($eofLength == 0) - // means that we are also at the end of the stream. - // we do not know if $eofLength is 1 or two, so we'll use the EOLCharacterLength value - // which is 2 by default. - if ($eofLength === 0 && feof($this->stream)) { - $partLength = $currentOffset - $partOffset - strlen($line) - $this->EOLCharacterLength; - } + $partLength = $currentOffset - $partOffset - strlen($line) - $eofLength; // Copy part in a new stream $partStream = fopen('php://temp', 'rw'); stream_copy_to_stream($this->stream, $partStream, $partLength, $partOffset); - $this->parts[] = new self($partStream, $this->EOLCharacterLength); + $this->parts[] = new self($partStream); // Reset current stream offset fseek($this->stream, $currentOffset); } @@ -181,6 +160,9 @@ public function __construct($stream, $EOLCharacterLength = 2) // Update the part offset $partOffset = ftell($this->stream); } + + // Get end of line length (should be 2) + $eofLength = strlen($line) - strlen($trimmed); } diff --git a/tests/StreamedPartTest.php b/tests/StreamedPartTest.php index 196236a..620ab51 100644 --- a/tests/StreamedPartTest.php +++ b/tests/StreamedPartTest.php @@ -28,16 +28,6 @@ public function testInvalidStreamResource() new StreamedPart('invalid stream resource'); } - /** - * Test a multipart with invalid EOL character length - */ - public function testInvalidEOLCharacterLength() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage("EOL Length is not an integer"); - new StreamedPart(fopen(__DIR__ . '/_data/no_boundary.txt', 'r'), 'invalid EOL character length'); - } - /** * Test a multipart document without boundary header */ @@ -143,7 +133,7 @@ public function testNoNewLineAtTheEndOfThePartsWhenNewLineIsOneCharacterLong() fwrite($stream, $content); rewind($stream); - $part = new StreamedPart($stream, 1); + $part = new StreamedPart($stream); /** @var Part[] $parts */ $parts = $part->getParts(); self::assertEquals('Content', $parts[0]->getBody());