PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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 / ValueObjects / Form / Form.php

Form.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/ValueObjects/Form/Form.php

271 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\ValueObjects\Form;
4
5 use IvyForms\Common\Exceptions\ValidationException;
6 use IvyForms\Services\Template\TemplateDefaults;
7 use IvyForms\Services\Translations\BackendStrings;
8
9 /**
10 * Class Form - Core form value object with composed metadata and settings
11 *
12 * @package IvyForms\ValueObjects\Form
13 */
14 final class Form
15 {
16 /**
17 * @var int
18 */
19 public int $id;
20
21 /**
22 * @var string
23 */
24 public string $name;
25
26 /**
27 * @var string
28 */
29 public string $description;
30
31 /**
32 * @var FormMetadata
33 */
34 public FormMetadata $metadata;
35
36 /**
37 * @var FormSettings
38 */
39 public FormSettings $settings;
40
41 /**
42 * @var array<int, array<string, mixed>>|null
43 */
44 public ?array $fields;
45
46 /**
47 * @var IntegrationSettings|null
48 */
49 public ?IntegrationSettings $integrationSettings;
50 /**
51 * @var StyleSettings|null
52 */
53 public ?StyleSettings $styleSettings;
54
55 /**
56 * @var array<int, array<string, mixed>>|null
57 */
58 public ?array $pages;
59
60 /**
61 * @var array<string, mixed>|null
62 */
63 public ?array $progressIndicator;
64
65 /**
66 * @var array<string, mixed>
67 */
68 public array $formActionButtons;
69
70 /**
71 * Form constructor.
72 *
73 * @param int $id
74 * @param string $name
75 * @param string $description
76 * @param FormMetadata $metadata
77 * @param FormSettings $settings
78 * @param array<int, array<string, mixed>> $fields
79 * @param IntegrationSettings|null $integrationSettings
80 * @param array<int, array<string, mixed>>|null $pages
81 * @param array<string, mixed>|null $progressIndicator
82 * @param StyleSettings|null $styleSettings
83 * @param array<string, mixed>|null $formActionButtons
84 * @SuppressWarnings("ExcessiveParameterList")
85 * @throws ValidationException
86 */
87 public function __construct(
88 int $id,
89 string $name,
90 string $description,
91 FormMetadata $metadata,
92 FormSettings $settings,
93 array $fields = [],
94 ?IntegrationSettings $integrationSettings = null,
95 ?array $pages = null,
96 ?array $progressIndicator = null,
97 ?StyleSettings $styleSettings = null,
98 ?array $formActionButtons = null
99 ) {
100 $this->id = $this->validateId($id);
101 $this->name = $this->validateString($name, 255, 'name');
102 $this->description = $this->validateString($description, 1000, 'description');
103 $this->metadata = $metadata;
104 $this->settings = $settings;
105 $this->fields = $fields;
106 $this->integrationSettings = $integrationSettings;
107 $this->pages = $pages;
108 $this->progressIndicator = $progressIndicator;
109 $this->formActionButtons = $formActionButtons ?? [
110 'submitButtonSettings' => [
111 'label' => BackendStrings::getCommonStrings()['submit'],
112 'position' => 'default',
113 ],
114 ];
115 $this->styleSettings = $styleSettings;
116 }
117
118 /**
119 * Validates the ID.
120 *
121 * @param int $id
122 *
123 * @return int
124 * @throws ValidationException
125 */
126 private function validateId(int $id): int
127 {
128 if ($id < 0) {
129 throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
130 }
131 return $id;
132 }
133
134 /**
135 * Validates a string value.
136 *
137 * @param string $value
138 * @param int $maxLength
139 * @param string $fieldName
140 *
141 * @return string
142 *
143 * @throws ValidationException
144 */
145 private function validateString(string $value, int $maxLength, string $fieldName): string
146 {
147 if (strlen($value) > $maxLength) {
148 throw new ValidationException(
149 sprintf(
150 /* translators: 1: String value, 2: String max length. */
151 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
152 esc_html($fieldName),
153 $maxLength
154 )
155 );
156 }
157 return $value;
158 }
159
160
161 /**
162 * Get the form ID.
163 *
164 * @return int
165 */
166 public function getId(): int
167 {
168 return $this->id;
169 }
170
171 /**
172 * Get the form name.
173 *
174 * @return string
175 */
176 public function getName(): string
177 {
178 return $this->name;
179 }
180
181 /**
182 * Get the form description.
183 *
184 * @return string
185 */
186 public function getDescription(): string
187 {
188 return $this->description;
189 }
190
191 /**
192 * Get the form fields.
193 *
194 * @return array<int, array<string, mixed>>|null
195 */
196 public function getFields(): ?array
197 {
198 return $this->fields;
199 }
200
201 /**
202 * Get the integration settings.
203 * @return IntegrationSettings|null
204 */
205 public function getIntegrationSettings(): ?IntegrationSettings
206 {
207 return $this->integrationSettings;
208 }
209
210 /**
211 * Set integration settings.
212 * @param IntegrationSettings $settings
213 */
214 public function setIntegrationSettings(IntegrationSettings $settings): void
215 {
216 $this->integrationSettings = $settings;
217 }
218
219 /**
220 * Get style settings value object.
221 *
222 * @return StyleSettings|null
223 */
224 public function getStyleSettings(): ?StyleSettings
225 {
226 return $this->styleSettings;
227 }
228
229 /**
230 * Set style settings.
231 *
232 * @param StyleSettings|null $settings Style settings or null.
233 */
234 public function setStyleSettings(?StyleSettings $settings): void
235 {
236 $this->styleSettings = $settings;
237 }
238
239 /**
240 * Convert the form to an array.
241 *
242 * @return array<string, mixed>
243 */
244 public function toArray(): array
245 {
246 $integrationPayload = $this->integrationSettings !== null
247 ? $this->integrationSettings->toArray()
248 : ['integrationSettings' => TemplateDefaults::getDefaultIntegrationSettings()];
249
250 $stylePayload = $this->styleSettings !== null
251 ? $this->styleSettings->toArray()
252 : ['styleSettings' => TemplateDefaults::getDefaultFormStylesSafe()];
253
254 return array_merge(
255 [
256 'id' => $this->getId(),
257 'name' => $this->getName(),
258 'description' => $this->getDescription(),
259 'fields' => $this->getFields(),
260 'pages' => $this->pages,
261 'progressIndicator' => $this->progressIndicator,
262 'formActionButtons' => $this->formActionButtons,
263 ],
264 $this->metadata->toArray(),
265 $this->settings->toArray(),
266 $integrationPayload,
267 $stylePayload
268 );
269 }
270 }
271