PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Page / PageIdNormalizer.php

PageIdNormalizer.php in The Innovative Form Builder – IvyForms 1.5, at backend/src/Services/Page/PageIdNormalizer.php

71 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Page;
4
5 /**
6 * Normalizes pages and field pageId values to deterministic IDs.
7 */
8 class PageIdNormalizer
9 {
10 /**
11 * @param array<int, mixed> $pages
12 * @return array{pages: array<int, array<string, mixed>>, pageIdMap: array<string, string>, firstPageId: string}
13 */
14 public static function normalizePagesForForm(array $pages): array
15 {
16 $pageIdMap = [];
17 /** @var array<int, array<string, mixed>> $validPages */
18 $validPages = array_values(array_filter($pages, 'is_array'));
19
20 if ($validPages === []) {
21 $firstPageId = self::buildPageId(1);
22
23 return [
24 'pages' => [[
25 'id' => $firstPageId,
26 'label' => 'Page 1',
27 'closable' => false,
28 'fields' => [],
29 ]],
30 'pageIdMap' => $pageIdMap,
31 'firstPageId' => $firstPageId,
32 ];
33 }
34
35 $normalizedPages = [];
36 foreach ($validPages as $index => $page) {
37 $oldId = isset($page['id']) ? (string) $page['id'] : '';
38 $newId = self::buildPageId($index + 1);
39 if ($oldId !== '') {
40 $pageIdMap[$oldId] = $newId;
41 }
42
43 $page['id'] = $newId;
44 $normalizedPages[] = $page;
45 }
46
47
48 return [
49 'pages' => $normalizedPages,
50 'pageIdMap' => $pageIdMap,
51 'firstPageId' => (string) $normalizedPages[0]['id'],
52 ];
53 }
54
55 /**
56 * Build a deterministic page ID for the given page index (1-based).
57 */
58 public static function buildPageId(int $pageNumber): string
59 {
60 return sprintf('page_%d', max(1, $pageNumber));
61 }
62
63 /**
64 * Return true when $pageId already has the deterministic `page_N` format.
65 */
66 public static function isDeterministicPageIdForForm(string $pageId): bool
67 {
68 return preg_match('/^page_[1-9][0-9]*$/', $pageId) === 1;
69 }
70 }
71