mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 07:12:26 +02:00
Merge remote-tracking branch 'origin/support/3.2' into develop
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\Form\Util;
|
||||
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
@@ -39,30 +41,84 @@ class FormUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively replaces or appends elements of the first array with elements
|
||||
* of second array. If the key is an integer, the values will be appended to
|
||||
* the new array; otherwise, the value from the second array will replace
|
||||
* the one from the first array.
|
||||
* Merges query string or post parameters with uploaded files.
|
||||
*/
|
||||
public static function mergeParamsAndFiles(array $params, array $files): array
|
||||
{
|
||||
$isFilesList = array_is_list($files);
|
||||
return self::merge($params, $files);
|
||||
}
|
||||
|
||||
foreach ($params as $key => $value) {
|
||||
if (\is_array($value) && \is_array($files[$key] ?? null)) {
|
||||
$params[$key] = self::mergeParamsAndFiles($value, $files[$key]);
|
||||
unset($files[$key]);
|
||||
private static function merge(mixed $params, mixed $files): mixed
|
||||
{
|
||||
if (null === $params) {
|
||||
return $files;
|
||||
}
|
||||
|
||||
if (\is_array($params) && self::isFileUpload($files)) {
|
||||
return $files; // if the array is a file upload field, it has the precedence
|
||||
}
|
||||
|
||||
if (\is_array($params) && \is_array($files)) {
|
||||
// if both are lists and both do not contain arrays, then merge them and return
|
||||
if (array_is_list($params) && self::doesNotContainNonFileUploadArray($params) && array_is_list($files) && self::doesNotContainNonFileUploadArray($files)) {
|
||||
return array_merge($params, $files);
|
||||
}
|
||||
|
||||
// heuristics to preserve order, the bigger array wins
|
||||
if (\count($files) > \count($params)) {
|
||||
$keys = array_unique(array_merge(array_keys($files), array_keys($params)));
|
||||
} else {
|
||||
$keys = array_unique(array_merge(array_keys($params), array_keys($files)));
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$result[$key] = self::merge($params[$key] ?? null, $files[$key] ?? null);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (!$isFilesList) {
|
||||
return array_replace($params, $files);
|
||||
if (\is_array($params)) {
|
||||
return $params; // params has the precedence
|
||||
}
|
||||
|
||||
foreach ($files as $value) {
|
||||
$params[] = $value;
|
||||
if (self::isFileUpload($files)) {
|
||||
return $files; // if the array is a file upload field, it has the precedence
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private static function isFileUpload(mixed $value): bool
|
||||
{
|
||||
if ($value instanceof UploadedFile) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!\is_array($value) || !\in_array(\count($value), [5, 6], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (\array_key_exists('full_path', $value)) {
|
||||
unset($value['full_path']);
|
||||
}
|
||||
|
||||
$keys = array_keys($value);
|
||||
sort($keys);
|
||||
|
||||
return ['error', 'name', 'size', 'tmp_name', 'type'] === $keys;
|
||||
}
|
||||
|
||||
private static function doesNotContainNonFileUploadArray(array $array): bool
|
||||
{
|
||||
foreach ($array as $value) {
|
||||
if (\is_array($value) && !self::isFileUpload($value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user