PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
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 / Submission / SubmissionFieldFormatter.php

SubmissionFieldFormatter.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Services/Submission/SubmissionFieldFormatter.php

119 lines 3.3 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\Submission;
4
5 /**
6 * Submission Field Formatter
7 *
8 * Handles formatting of field values for display in integrations.
9 *
10 * @package IvyForms\Services\Submission
11 */
12 class SubmissionFieldFormatter
13 {
14 /**
15 * Format field value for display (convert complex values to readable strings).
16 *
17 * @param mixed $value
18 * @param string $type
19 * @return string
20 */
21 public function formatForDisplay($value, string $type): string
22 {
23 // Strip whitespace via the single canonical implementation in
24 // SubmissionFieldTypeFormatter so the rule is defined in one place.
25 if ($type === 'phone' && is_string($value)) {
26 return SubmissionFieldTypeFormatter::stripPhoneWhitespace($value);
27 }
28
29 // Handle compound field types (name, address) - format as readable string
30 if (in_array($type, ['name', 'address'], true) && is_array($value)) {
31 return $type === 'name'
32 ? $this->formatNameField($value)
33 : $this->formatAddressField($value);
34 }
35
36 // Handle other array types (checkboxes, multi-select) - join with comma
37 if (is_array($value)) {
38 $filtered = array_filter($value, function ($fieldValue) {
39 return $fieldValue !== '' && $fieldValue !== null;
40 });
41 return implode(', ', $filtered);
42 }
43
44 return (string)$value;
45 }
46
47 /**
48 * Format name field as readable string.
49 *
50 * @param array<string, mixed> $value
51 * @return string
52 */
53 private function formatNameField(array $value): string
54 {
55 $values = array_filter($value, function ($fieldValue) {
56 return $fieldValue !== '' && $fieldValue !== null;
57 });
58
59 if (count($values) < 2) {
60 return implode(' ', $values);
61 }
62
63 $valuesList = array_values($values);
64 $lastName = $valuesList[1] ?? '';
65 $firstName = $valuesList[0] ?? '';
66 $middle = isset($valuesList[2]) ? ' ' . implode(' ', array_slice($valuesList, 2)) : '';
67
68 return $lastName . ' ' . $firstName . $middle;
69 }
70
71 /**
72 * Format address field as readable string.
73 *
74 * @param array<string, mixed> $value
75 * @return string
76 */
77 private function formatAddressField(array $value): string
78 {
79 $parts = [];
80
81 if (!empty($value['streetAddress'])) {
82 $parts[] = $value['streetAddress'];
83 }
84 if (!empty($value['addressLine2'])) {
85 $parts[] = $value['addressLine2'];
86 }
87
88 $cityStateZip = $this->buildCityStateZip($value);
89 if ($cityStateZip) {
90 $parts[] = $cityStateZip;
91 }
92
93 if (!empty($value['country'])) {
94 $parts[] = $value['country'];
95 }
96
97 return implode("\n", $parts);
98 }
99
100 /**
101 * Build city, state, zip line.
102 *
103 * @param array<string, mixed> $value
104 * @return string
105 */
106 private function buildCityStateZip(array $value): string
107 {
108 $parts = array_filter([
109 $value['city'] ?? '',
110 $value['state'] ?? '',
111 $value['zip'] ?? ''
112 ], function ($part) {
113 return $part !== '' && $part !== null;
114 });
115
116 return implode(', ', $parts);
117 }
118 }
119