| 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\Services\Submission; |
| 11 |
|
| 12 |
use IvyForms\Entity\Field\Field as FieldEntity; |
| 13 |
use IvyForms\Infrastructure\WP\REST\SubmissionMultipartReader; |
| 14 |
|
| 15 |
/** |
| 16 |
* Merges multipart file parts into submission values (file keys like file-upload_0). |
| 17 |
*/ |
| 18 |
class SubmissionUploadService |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @param array<string, mixed> $params Request params with decoded `values` array |
| 22 |
* @param array<string, mixed> $uploadedFiles From {@see SubmissionMultipartReader::resolveUploadedFiles()} |
| 23 |
* @param array<\IvyForms\Entity\Field\Field> $formFields |
| 24 |
* |
| 25 |
* @return array<string, mixed> |
| 26 |
*/ |
| 27 |
public function mergeUploadedFilesIntoValues( |
| 28 |
array $params, |
| 29 |
array $uploadedFiles, |
| 30 |
array $formFields |
| 31 |
): array { |
| 32 |
if ($uploadedFiles === [] || !$this->hasMergeableValues($params)) { |
| 33 |
unset($params['ivyforms_fm']); |
| 34 |
|
| 35 |
return $params; |
| 36 |
} |
| 37 |
|
| 38 |
$mapRaw = isset($params['ivyforms_fm']) ? $params['ivyforms_fm'] : ''; |
| 39 |
unset($params['ivyforms_fm']); |
| 40 |
if (!is_string($mapRaw) || $mapRaw === '') { |
| 41 |
return $params; |
| 42 |
} |
| 43 |
|
| 44 |
$decodedMap = SubmissionMultipartReader::decodeFileParamMap($mapRaw); |
| 45 |
$this->ensureUploadHelpersLoaded(); |
| 46 |
|
| 47 |
$this->assertUploadCountsBeforePersist($uploadedFiles, $decodedMap, $formFields); |
| 48 |
|
| 49 |
$byLogicalKey = SubmissionUploadFilePersister::collectUploadedUrls( |
| 50 |
$uploadedFiles, |
| 51 |
$decodedMap, |
| 52 |
$formFields |
| 53 |
); |
| 54 |
|
| 55 |
return $this->applyUploadedUrls($params, $byLogicalKey); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param array<string, mixed> $params |
| 60 |
* @param array<string, string[]> $byLogicalKey |
| 61 |
* |
| 62 |
* @return array<string, mixed> |
| 63 |
*/ |
| 64 |
private function applyUploadedUrls(array $params, array $byLogicalKey): array |
| 65 |
{ |
| 66 |
foreach ($byLogicalKey as $logicalKey => $urls) { |
| 67 |
if ($urls === []) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
$params['values'][$logicalKey] = count($urls) === 1 ? $urls[0] : wp_json_encode($urls); |
| 71 |
} |
| 72 |
|
| 73 |
return $params; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param array<string, mixed> $uploadedFiles |
| 78 |
* @param array<string, string> $decodedMap |
| 79 |
* @param array<int, FieldEntity> $formFields |
| 80 |
*/ |
| 81 |
private function assertUploadCountsBeforePersist( |
| 82 |
array $uploadedFiles, |
| 83 |
array $decodedMap, |
| 84 |
array $formFields |
| 85 |
): void { |
| 86 |
$countsByKey = SubmissionMultipartReader::groupUploadCountsByLogicalKey($uploadedFiles, $decodedMap); |
| 87 |
foreach ($countsByKey as $logicalKey => $count) { |
| 88 |
if ($count === 0) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
$fieldEntity = SubmissionUploadFileValidator::findFieldForUploadKey($formFields, $logicalKey); |
| 92 |
SubmissionUploadFileValidator::assertUploadCountWithinLimits($count, $fieldEntity, $logicalKey); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param array<string, mixed> $params |
| 98 |
*/ |
| 99 |
private function hasMergeableValues(array $params): bool |
| 100 |
{ |
| 101 |
return array_key_exists('values', $params) && is_array($params['values']); |
| 102 |
} |
| 103 |
|
| 104 |
private function ensureUploadHelpersLoaded(): void |
| 105 |
{ |
| 106 |
if (!function_exists('wp_handle_upload')) { |
| 107 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|