Skip to content

Commit 1c33633

Browse files
authored
Allow MultipartStreamBuilder subclass to manipulate data (#49)
* Add MultipartStreamBuilder::addData * Rewrite MultipartStreamBuilder::addResource with addData * Remove internal 'filename' key from MultipartStreamBuilder::data.
1 parent 78df82a commit 1c33633

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"extra": {
4040
"branch-alias": {
41-
"dev-master": "1.1-dev"
41+
"dev-master": "1.x-dev"
4242
}
4343
}
4444
}

src/MultipartStreamBuilder.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MultipartStreamBuilder
3434
private $boundary;
3535

3636
/**
37-
* @var array Element where each Element is an array with keys ['contents', 'headers', 'filename']
37+
* @var array Element where each Element is an array with keys ['contents', 'headers']
3838
*/
3939
private $data = [];
4040

@@ -72,6 +72,24 @@ public function __construct($streamFactory = null)
7272
}
7373
}
7474

75+
/**
76+
* Add a resource to the Multipart Stream
77+
*
78+
* @param string|resource|\Psr\Http\Message\StreamInterface $resource
79+
* The filepath, resource or StreamInterface of the data.
80+
* @param array $headers
81+
* Additional headers array: ['header-name' => 'header-value'].
82+
*
83+
* @return MultipartStreamBuilder
84+
*/
85+
public function addData($resource, array $headers = [])
86+
{
87+
$stream = $this->createStream($resource);
88+
$this->data[] = ['contents' => $stream, 'headers' => $headers];
89+
90+
return $this;
91+
}
92+
7593
/**
7694
* Add a resource to the Multipart Stream.
7795
*
@@ -104,9 +122,8 @@ public function addResource($name, $resource, array $options = [])
104122
}
105123

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

109-
return $this;
126+
return $this->addData($stream, $options['headers']);
110127
}
111128

112129
/**

0 commit comments

Comments
 (0)