PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.1
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 / Common / Helpers / NameFieldKeyHelper.php

NameFieldKeyHelper.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Common/Helpers/NameFieldKeyHelper.php

116 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Common\Helpers;
4
5 class NameFieldKeyHelper
6 {
7 /**
8 * Sort child field IDs by stored display order before positional fallback.
9 *
10 * @param array<int> $childIds
11 * @param array<int, object> $fieldById
12 * @return array<int>
13 */
14 public static function sortChildIdsByDisplayOrder(array $childIds, array $fieldById): array
15 {
16 $sortKeys = [];
17 foreach ($childIds as $position => $childId) {
18 $displayIndex = self::getSubFieldDisplayIndex($fieldById[$childId] ?? null);
19 $sortKeys[] = [$displayIndex ?? PHP_INT_MAX, $position, $childId];
20 }
21
22 usort(
23 $sortKeys,
24 static function (array $left, array $right): int {
25 if ($left[0] !== $right[0]) {
26 return $left[0] <=> $right[0];
27 }
28
29 return $left[1] <=> $right[1];
30 }
31 );
32
33 return array_column($sortKeys, 2);
34 }
35
36 /**
37 * Resolve the stable name subfield key stored on a child field.
38 *
39 * @param object|null $field
40 * @return string|null
41 */
42 public static function resolveStoredNameSubfieldKey(?object $field): ?string
43 {
44 if ($field === null) {
45 return null;
46 }
47
48 $nameFieldType = null;
49 if (method_exists($field, 'getAdditionalProperty')) {
50 $nameFieldType = $field->getAdditionalProperty('nameFieldType');
51 }
52
53 if (is_string($nameFieldType) && preg_match('/^nameField\d+$/i', $nameFieldType) === 1) {
54 return $nameFieldType;
55 }
56
57 $label = null;
58 if (method_exists($field, 'getFieldGeneralSettings')) {
59 $label = $field->getFieldGeneralSettings()->getLabel();
60 }
61
62 return self::inferNameFieldKeyFromLabel($label);
63 }
64
65 /**
66 * Infer a stable name subfield key from a field label.
67 *
68 * @param mixed $label
69 * @return string|null
70 */
71 public static function inferNameFieldKeyFromLabel($label): ?string
72 {
73 if (!is_string($label) || trim($label) === '') {
74 return null;
75 }
76
77 $trimmedLabel = trim($label);
78
79 if (preg_match('/^nameField(\d+)$/i', $trimmedLabel, $matches) === 1) {
80 return 'nameField' . $matches[1];
81 }
82
83 if (preg_match('/name\s*field\s*(\d+)/i', $trimmedLabel, $matches) === 1) {
84 return 'nameField' . $matches[1];
85 }
86
87 return null;
88 }
89
90 /**
91 * @param object|null $field
92 * @return int|null
93 */
94 private static function getSubFieldDisplayIndex(?object $field): ?int
95 {
96 if ($field === null || !method_exists($field, 'getAdditionalProperty')) {
97 return null;
98 }
99
100 $subFieldIndex = $field->getAdditionalProperty('subFieldIndex');
101 if (is_int($subFieldIndex)) {
102 return $subFieldIndex;
103 }
104
105 if (is_string($subFieldIndex) && trim($subFieldIndex) !== '' && is_numeric(trim($subFieldIndex))) {
106 return (int)$subFieldIndex;
107 }
108
109 if (is_numeric($subFieldIndex)) {
110 return (int)$subFieldIndex;
111 }
112
113 return null;
114 }
115 }
116