From b401a4eb953867cd70cc0eba6e7f8465137bea0d Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 3 Sep 2023 17:35:04 -0500 Subject: [PATCH] Implement Stringable in RequestBody --- src/RequestBody.php | 16 +++++++++++++--- test/RequestBodyTest.php | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/RequestBody.php b/src/RequestBody.php index 34eac82a..06f9810a 100644 --- a/src/RequestBody.php +++ b/src/RequestBody.php @@ -19,7 +19,7 @@ * * @implements \IteratorAggregate */ -final class RequestBody implements ReadableStream, \IteratorAggregate +final class RequestBody implements ReadableStream, \IteratorAggregate, \Stringable { use ReadableStreamIteratorAggregate; @@ -46,8 +46,7 @@ public function read(?Cancellation $cancellation = null): ?string /** * @see Payload::buffer() - * @throws ClientException - * @throws BufferException|StreamException + * @throws ClientException|BufferException|StreamException */ public function buffer(?Cancellation $cancellation = null, int $limit = \PHP_INT_MAX): string { @@ -88,4 +87,15 @@ public function increaseSizeLimit(int $size): void ($this->upgradeSize)($size); } + + /** + * Buffers entire stream before returning. Use {@see self::buffer()} to optionally provide a {@see Cancellation} + * and/or length limit. + * + * @throws ClientException|BufferException|StreamException + */ + public function __toString(): string + { + return $this->buffer(); + } } diff --git a/test/RequestBodyTest.php b/test/RequestBodyTest.php index db8aef82..63e60e70 100644 --- a/test/RequestBodyTest.php +++ b/test/RequestBodyTest.php @@ -12,6 +12,6 @@ public function testIncreaseWithoutCallback(): void { $body = new RequestBody(new ReadableBuffer("foobar")); $body->increaseSizeLimit(1); - $this->assertSame("foobar", $body->buffer()); + $this->assertSame("foobar", (string) $body); } }