Skip to content

Commit ca4f58b

Browse files
l-naumannnicolas-grekas
authored andcommitted
[Mime] Throw InvalidArgumentException on invalid form field type inside array
1 parent 9257580 commit ca4f58b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Part/Multipart/FormDataPart.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ public function __construct(array $fields = [])
3232
{
3333
parent::__construct();
3434

35-
foreach ($fields as $name => $value) {
36-
if (!\is_string($value) && !\is_array($value) && !$value instanceof TextPart) {
37-
throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart ("%s" given).', $name, get_debug_type($value)));
38-
}
35+
$this->fields = $fields;
3936

40-
$this->fields[$name] = $value;
41-
}
4237
// HTTP does not support \r\n in header values
4338
$this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
4439
}
@@ -75,6 +70,10 @@ private function prepareFields(array $fields): array
7570
return;
7671
}
7772

73+
if (!\is_string($item) && !$item instanceof TextPart) {
74+
throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart, "%s" given.', $fieldName, get_debug_type($item)));
75+
}
76+
7877
$values[] = $this->preparePart($fieldName, $item);
7978
};
8079

Tests/Part/Multipart/FormDataPartTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ public function testExceptionOnFormFieldsWithIntegerKeysAndMultipleValues()
199199
$f->getParts();
200200
}
201201

202+
public function testExceptionOnFormFieldsWithDisallowedTypesInsideArray()
203+
{
204+
$f = new FormDataPart([
205+
'foo' => [
206+
'bar' => 'baz',
207+
'qux' => [
208+
'quux' => 1,
209+
],
210+
],
211+
]);
212+
213+
$this->expectException(InvalidArgumentException::class);
214+
$this->expectExceptionMessage('The value of the form field "foo[qux][quux]" can only be a string, an array, or an instance of TextPart, "int" given.');
215+
$f->getParts();
216+
}
217+
202218
public function testToString()
203219
{
204220
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');

0 commit comments

Comments
 (0)