Refactor validation for withUploadedFiles
This commit is contained in:
parent
81055c3bd9
commit
f98ee59e4a
|
|
@ -485,48 +485,44 @@ class ServerRequest extends Request implements ServerRequestInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $uploadedFiles
|
* @param array $root
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isValidUploadedFilesTree(array $uploadedFiles)
|
private function isValidUploadedFilesTree(array $root)
|
||||||
{
|
{
|
||||||
// Allow empty array.
|
// Allow empty array.
|
||||||
if (count($uploadedFiles) === 0) {
|
if (count($root) === 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All keys MUST be strings.
|
// If not empty, the array MUST have all string keys.
|
||||||
$keys = array_keys($uploadedFiles);
|
$keys = array_keys($root);
|
||||||
if (count($keys) !== count(array_filter($keys, "is_string"))) {
|
if (count($keys) !== count(array_filter($keys, "is_string"))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($uploadedFiles as $branch) {
|
// Valid if each child branch is valid.
|
||||||
|
foreach ($root as $branch) {
|
||||||
if (!$this->isValidUploadedFilesBranch($branch)) {
|
if (!$this->isValidUploadedFilesBranch($branch)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isValidUploadedFilesBranch($branch)
|
private function isValidUploadedFilesBranch($branch)
|
||||||
{
|
{
|
||||||
if ($branch instanceof UploadedFileInterface) {
|
if (is_array($branch)) {
|
||||||
return true;
|
// Branch.
|
||||||
}
|
foreach ($branch as $child) {
|
||||||
|
if (!$this->isValidUploadedFilesBranch($child)) {
|
||||||
if (!is_array($branch)) {
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$values = array_values($branch);
|
|
||||||
foreach ($values as $value) {
|
|
||||||
if (!$this->isValidUploadedFilesBranch($value)) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// Leaf. Valid only if this is an UploadedFileInterface.
|
||||||
|
return $branch instanceof UploadedFileInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -447,6 +447,7 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
|
||||||
/**
|
/**
|
||||||
* @covers ::withUploadedFiles
|
* @covers ::withUploadedFiles
|
||||||
* @covers ::isValidUploadedFilesTree
|
* @covers ::isValidUploadedFilesTree
|
||||||
|
* @covers ::isValidUploadedFilesBranch
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @dataProvider invalidUploadedFilesProvider
|
* @dataProvider invalidUploadedFilesProvider
|
||||||
*/
|
*/
|
||||||
|
|
@ -481,7 +482,9 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
|
||||||
"error" => UPLOAD_ERR_OK,
|
"error" => UPLOAD_ERR_OK,
|
||||||
"size" => 1024
|
"size" => 1024
|
||||||
]
|
]
|
||||||
],
|
]
|
||||||
|
],
|
||||||
|
[
|
||||||
"nestedList" => [
|
"nestedList" => [
|
||||||
"level2" => [
|
"level2" => [
|
||||||
"name" => [
|
"name" => [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue