| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace IvyForms\Infrastructure\WP\REST; |
| 11 |
|
| 12 |
use IvyForms\Common\Exceptions\ValidationException; |
| 13 |
use IvyForms\Services\Translations\BackendStrings; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
/** |
| 17 |
* Reads multipart file params and decodes the ivyforms_fm upload map. |
| 18 |
*/ |
| 19 |
final class SubmissionMultipartReader |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @param WP_REST_Request<array<string, mixed>> $request |
| 23 |
* |
| 24 |
* @return array<string, mixed> |
| 25 |
*/ |
| 26 |
public static function resolveUploadedFiles(WP_REST_Request $request): array |
| 27 |
{ |
| 28 |
return $request->get_file_params(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @return array<string, string> |
| 33 |
*/ |
| 34 |
public static function decodeFileParamMap(string $mapRaw): array |
| 35 |
{ |
| 36 |
/** @var array<string, mixed> $decodedMap */ |
| 37 |
$decodedMap = json_decode(wp_unslash($mapRaw), true); |
| 38 |
if (!is_array($decodedMap)) { |
| 39 |
throw new ValidationException( |
| 40 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
$normalized = []; |
| 45 |
foreach ($decodedMap as $paramName => $logicalKey) { |
| 46 |
if (is_string($paramName) && is_string($logicalKey)) { |
| 47 |
$normalized[$paramName] = $logicalKey; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $normalized; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array<string, string> $decodedMap |
| 56 |
*/ |
| 57 |
public static function resolveLogicalUploadKey(string $paramName, array $decodedMap): ?string |
| 58 |
{ |
| 59 |
if (strpos($paramName, 'ivyforms_fu_') !== 0 || !isset($decodedMap[$paramName])) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
$logicalKey = $decodedMap[$paramName]; |
| 63 |
|
| 64 |
return strpos($logicalKey, 'file-upload_') === 0 ? $logicalKey : null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param array<string, mixed> $filesLookup |
| 69 |
* @param array<string, string> $decodedMap |
| 70 |
* |
| 71 |
* @return array<string, int> |
| 72 |
*/ |
| 73 |
public static function groupUploadCountsByLogicalKey(array $filesLookup, array $decodedMap): array |
| 74 |
{ |
| 75 |
/** @var array<string, int> $counts */ |
| 76 |
$counts = []; |
| 77 |
|
| 78 |
foreach ($filesLookup as $paramName => $fileStruct) { |
| 79 |
$logicalKey = self::resolveLogicalUploadKey((string) $paramName, $decodedMap); |
| 80 |
if ($logicalKey === null || !is_array($fileStruct)) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
if (($fileStruct['error'] ?? \UPLOAD_ERR_NO_FILE) === \UPLOAD_ERR_NO_FILE) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
if (!isset($counts[$logicalKey])) { |
| 88 |
$counts[$logicalKey] = 0; |
| 89 |
} |
| 90 |
$counts[$logicalKey]++; |
| 91 |
} |
| 92 |
|
| 93 |
return $counts; |
| 94 |
} |
| 95 |
} |
| 96 |
|