Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parameter EOLCharacterLength and detect it automatically #46

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions src/StreamedPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -145,29 +132,21 @@ public function __construct($stream, $EOLCharacterLength = 2)

$partOffset = 0;
$endOfBody = false;
$eofLength = 0;

while ($line = fgets($this->stream, $bufferSize)) {
$trimmed = rtrim($line, "\r\n");

// Search the separator
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);
}
Expand All @@ -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);
}


Expand Down
12 changes: 1 addition & 11 deletions tests/StreamedPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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());
Expand Down