From f76bde0de6bb9a1122c7d03a3164a18f54be25e2 Mon Sep 17 00:00:00 2001 From: Mahmood Dehghani Date: Sun, 8 Jun 2025 16:13:28 +0330 Subject: [PATCH] add support for attaching files via raw contents --- .gitattributes | 3 ++- src/Request.php | 24 ++++++++++++------------ src/RequestInterface.php | 9 ++++++++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.gitattributes b/.gitattributes index d80024f..1adb868 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ -* text=auto +* text=auto eol=lf + *.md diff=markdown *.php diff=php /.github export-ignore diff --git a/src/Request.php b/src/Request.php index a708933..d4d7be9 100644 --- a/src/Request.php +++ b/src/Request.php @@ -78,11 +78,20 @@ public function getBody(): array * {@inheritDoc} */ public function addFile(string $fieldName, string $path): static + { + return $this->addRawFile($fieldName, (string) file_get_contents($path)); + } + + /** + * {@inheritDoc} + */ + public function addRawFile(string $fieldName, string $contents): static { $this->contentType = ContentType::MULTIPART; + $this->files[] = [ - 'fieldName' => $fieldName, - 'path' => $path, + $fieldName, + $contents, ]; return $this; @@ -93,16 +102,7 @@ public function addFile(string $fieldName, string $path): static */ public function getFiles(): array { - $data = []; - - foreach ($this->files as $file) { - $data[] = [ - $file['fieldName'], - file_get_contents($file['path']), - ]; - } - - return $data; + return $this->files; } /** diff --git a/src/RequestInterface.php b/src/RequestInterface.php index 5f7224f..49dffaa 100644 --- a/src/RequestInterface.php +++ b/src/RequestInterface.php @@ -24,12 +24,19 @@ public function getUrl(): string; public function getBody(): array; /** - * Add a file to the request data. + * Add a file from a given path to the request payload. * * @return $this */ public function addFile(string $fieldName, string $path): static; + /** + * Add a file using its raw contents to the request payload. + * + * @return $this + */ + public function addRawFile(string $fieldName, string $contents): static; + /** * Get an array of files attached to the request. */