Skip to content

Allow MultipartStreamBuilder subclass to manipulate data #49

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

Merged
merged 4 commits into from
Dec 22, 2020
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
"dev-master": "1.x-dev"
}
}
}
23 changes: 20 additions & 3 deletions src/MultipartStreamBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MultipartStreamBuilder
private $boundary;

/**
* @var array Element where each Element is an array with keys ['contents', 'headers', 'filename']
* @var array Element where each Element is an array with keys ['contents', 'headers']
*/
private $data = [];

Expand Down Expand Up @@ -72,6 +72,24 @@ public function __construct($streamFactory = null)
}
}

/**
* Add a resource to the Multipart Stream
*
* @param string|resource|\Psr\Http\Message\StreamInterface $resource
* The filepath, resource or StreamInterface of the data.
* @param array $headers
* Additional headers array: ['header-name' => 'header-value'].
*
* @return MultipartStreamBuilder
*/
public function addData($resource, array $headers = [])
{
$stream = $this->createStream($resource);
$this->data[] = ['contents' => $stream, 'headers' => $headers];

return $this;
}

/**
* Add a resource to the Multipart Stream.
*
Expand Down Expand Up @@ -104,9 +122,8 @@ public function addResource($name, $resource, array $options = [])
}

$this->prepareHeaders($name, $stream, $options['filename'], $options['headers']);
$this->data[] = ['contents' => $stream, 'headers' => $options['headers'], 'filename' => $options['filename']];

return $this;
return $this->addData($stream, $options['headers']);
}

/**
Expand Down