| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Common\Helpers; |
| 9 |
|
| 10 |
class FormPageHelper |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Group fields by page ID |
| 14 |
* |
| 15 |
* @param array<mixed> $fields |
| 16 |
* @return array<string, array<mixed>> |
| 17 |
*/ |
| 18 |
public static function groupFieldsByPageId(array $fields): array |
| 19 |
{ |
| 20 |
$fieldsByPage = []; |
| 21 |
foreach ($fields as $field) { |
| 22 |
$pageId = $field['pageId'] ?? ($field['settings']['pageId'] ?? null); |
| 23 |
if ($pageId) { |
| 24 |
if (!isset($fieldsByPage[$pageId])) { |
| 25 |
$fieldsByPage[$pageId] = []; |
| 26 |
} |
| 27 |
$fieldsByPage[$pageId][] = $field; |
| 28 |
} |
| 29 |
} |
| 30 |
return $fieldsByPage; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Assign grouped fields to pages |
| 35 |
* |
| 36 |
* @param array<mixed> $pages |
| 37 |
* @param array<string, array<mixed>> $fieldsByPage |
| 38 |
* @return array<mixed> |
| 39 |
*/ |
| 40 |
public static function assignFieldsToPages(array $pages, array $fieldsByPage): array |
| 41 |
{ |
| 42 |
foreach ($pages as &$page) { |
| 43 |
$pageId = $page['id'] ?? null; |
| 44 |
$page['fields'] = $fieldsByPage[$pageId] ?? []; |
| 45 |
} |
| 46 |
return $pages; |
| 47 |
} |
| 48 |
} |
| 49 |
|