Skip to content

Commit

Permalink
[Mime] Throw InvalidArgumentException on invalid form field type insi…
Browse files Browse the repository at this point in the history
…de array
  • Loading branch information
l-naumann authored and nicolas-grekas committed Oct 17, 2023
1 parent 9257580 commit ca4f58b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public function __construct(array $fields = [])
{
parent::__construct();

foreach ($fields as $name => $value) {
if (!\is_string($value) && !\is_array($value) && !$value instanceof TextPart) {
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)));
}
$this->fields = $fields;

$this->fields[$name] = $value;
}
// HTTP does not support \r\n in header values
$this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
}
Expand Down Expand Up @@ -75,6 +70,10 @@ private function prepareFields(array $fields): array
return;
}

if (!\is_string($item) && !$item instanceof TextPart) {
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)));
}

$values[] = $this->preparePart($fieldName, $item);
};

Expand Down
16 changes: 16 additions & 0 deletions Tests/Part/Multipart/FormDataPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ public function testExceptionOnFormFieldsWithIntegerKeysAndMultipleValues()
$f->getParts();
}

public function testExceptionOnFormFieldsWithDisallowedTypesInsideArray()
{
$f = new FormDataPart([
'foo' => [
'bar' => 'baz',
'qux' => [
'quux' => 1,
],
],
]);

$this->expectException(InvalidArgumentException::class);
$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.');
$f->getParts();
}

public function testToString()
{
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');
Expand Down

0 comments on commit ca4f58b

Please sign in to comment.