Skip to content

Commit

Permalink
Remove parameter EOLCharacterLength and detect it automatically (#46)
Browse files Browse the repository at this point in the history
* Change behavior of testNoNewLineAtTheEndOfThePartsWhenNewLineIsOneCharacterLong to not specify EOLCharacterLength

* Remove parameter EOLCharacterLength and detect it automatically
  • Loading branch information
jkrzefski authored Apr 12, 2023
1 parent 755f054 commit 68e5499
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
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

0 comments on commit 68e5499

Please sign in to comment.