Skip to content

Commit

Permalink
Add addFields() to Form
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 10, 2023
1 parent 9cd91e1 commit 743b843
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public function addField(string $name, string $content, ?string $contentType = n
$this->fields[] = new FormField($name, BufferedContent::fromString($content, $contentType));
}

/**
* Adds each member of the array as an entry for the given key name. Array keys are persevered.
*
* @param array<string> $fields
*/
public function addFields(string $name, array $fields): void
{
foreach ($fields as $key => $content) {
$this->addField(\sprintf('%s[%s]', $name, $key), $content);
}
}

public function addStream(string $name, HttpContent $content, ?string $filename = null): void
{
if ($this->used) {
Expand Down
5 changes: 4 additions & 1 deletion test/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public function testUrlEncoded(): void
$body->addField('d', 'd');
$body->addField('encoding', '1+2');

$body->addFields('list', ['one', 'two']);
$body->addFields('map', ['one' => 'one', 'two' => 'two']);

$content = buffer($body->getContent());
$this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2", $content);
$this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2&list%5B0%5D=one&list%5B1%5D=two&map%5Bone%5D=one&map%5Btwo%5D=two", $content);
}

public function testMultiPartFieldsStream(): void
Expand Down

0 comments on commit 743b843

Please sign in to comment.