From f3fd223b9390ddc2579c78f74f2d1c5d78cab675 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 25 Dec 2016 21:50:33 +0100 Subject: [PATCH] Allow us to reset the builder --- src/MultipartStreamBuilder.php | 13 +++++++++++++ tests/FunctionTest.php | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/MultipartStreamBuilder.php b/src/MultipartStreamBuilder.php index 5a62d75..0f634dd 100644 --- a/src/MultipartStreamBuilder.php +++ b/src/MultipartStreamBuilder.php @@ -229,6 +229,19 @@ public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) return $this; } + /** + * Reset and clear all stored data. This allows you to use builder for a subsequent request. + * + * @return MultipartStreamBuilder + */ + public function reset() + { + $this->data = []; + $this->boundary = null; + + return $this; + } + /** * Gets the filename from a given path. * diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 3323bf2..ff6ea82 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -103,6 +103,20 @@ public function testBoundary() $this->assertEquals(5, substr_count($multipartStream, $boundary)); } + public function testReset() + { + $boundary = 'SpecialBoundary'; + $builder = new MultipartStreamBuilder(); + $builder->addResource('content0', 'foobar'); + $builder->setBoundary($boundary); + + $builder->reset(); + $multipartStream = (string) $builder->build(); + $this->assertNotContains('foobar', $multipartStream, 'Stream should not have any data after reset()'); + $this->assertNotEquals($boundary, $builder->getBoundary(), 'Stream should have a new boundary after reset()'); + $this->assertNotEmpty($builder->getBoundary()); + } + /** * @param string $body *