PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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
← All changes | backend/src/ValueObjects/Form/Form.php +162 -128 0.4 → 1.5.1 View file →
@@ -1,20 +1,18 @@
1 1 <?php
2 2
3 3 namespace IvyForms\ValueObjects\Form;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
10 5 use IvyForms\Common\Exceptions\ValidationException;
6 +use IvyForms\Common\Sanitizer\Sanitizer;
7 +use IvyForms\Services\Template\TemplateDefaults;
11 8 use IvyForms\Services\Translations\BackendStrings;
12 9
13 10 /**
14 - * Class Form
11 + * Class Form - Core form value object with composed metadata and settings
15 12 *
16 13 * @package IvyForms\ValueObjects\Form
14 + * @SuppressWarnings(PHPMD.TooManyFields)
17 15 */
18 16 final class Form
19 17 {
20 18 /**
@@ -20,96 +18,138 @@
20 18 /**
21 19 * @var int
22 20 */
23 21 public int $id;
22 +
24 23 /**
25 24 * @var string
26 25 */
27 26 public string $name;
27 +
28 28 /**
29 29 * @var string
30 30 */
31 - public string $author;
31 + public string $description;
32 +
32 33 /**
33 - * @var bool
34 + * @var FormMetadata
34 35 */
35 - public bool $published;
36 + public FormMetadata $metadata;
37 +
36 38 /**
37 - * @var bool
39 + * @var FormSettings
38 40 */
39 - public bool $starred;
41 + public FormSettings $settings;
42 +
40 43 /**
41 - * @var string
44 + * @var array<int, array<string, mixed>>|null
42 45 */
43 - public string $dateCreated;
46 + public ?array $fields;
47 +
44 48 /**
45 - * @var string
49 + * @var IntegrationSettings|null
46 50 */
47 - public string $dateEdited;
51 + public ?IntegrationSettings $integrationSettings;
48 52 /**
49 - * @var mixed[]|null
53 + * @var StyleSettings|null
50 54 */
51 - public ?array $fields;
55 + public ?StyleSettings $styleSettings;
56 +
52 57 /**
53 - * @var string
58 + * @var array<int, array<string, mixed>>|null
54 59 */
55 - public string $description;
60 + public ?array $pages;
61 +
56 62 /**
57 - * @var bool
63 + * @var array<string, mixed>|null
58 64 */
59 - public bool $showTitle;
65 + public ?array $progressIndicator;
66 +
60 67 /**
61 - * @var bool
68 + * @var array<string, mixed>
62 69 */
63 - public bool $showDescription;
70 + public array $formActionButtons;
71 +
64 72 /**
65 - * @var bool
73 + * Lowercase presentation type (classic, conversational, or plugin-defined via
74 + * ivyforms/form/allowed_form_types). Always matches Sanitizer::sanitizeFormType().
75 + *
76 + * @var string
66 77 */
67 - public bool $storeEntries;
78 + public string $formType;
68 79
69 80 /**
81 + * @var PaymentSettings
82 + */
83 + public PaymentSettings $paymentSettings;
84 +
85 + /**
70 86 * Form constructor.
71 87 *
72 - * @param int $id
73 - * @param string $name
74 - * @param string $author
75 - * @param bool $starred
76 - * @param bool $published
77 - * @param string $description
78 - * @param bool $showTitle
79 - * @param bool $showDescription
80 - * @param bool $storeEntries
81 - * @param mixed[]|null $fields
82 - *
88 + * @param int $id
89 + * @param string $name
90 + * @param string $description
91 + * @param FormMetadata $metadata
92 + * @param FormSettings $settings
93 + * @param array<int, array<string, mixed>> $fields
94 + * @param IntegrationSettings|null $integrationSettings
95 + * @param array<int, array<string, mixed>>|null $pages
96 + * @param array<string, mixed>|null $progressIndicator
97 + * @param StyleSettings|null $styleSettings
98 + * @param array<string, mixed>|null $formActionButtons
99 + * @param string $formType presentation type (sanitized to the allowed allowlist)
100 + * @param PaymentSettings|array<string, mixed>|null $paymentSettings
101 + * @SuppressWarnings("ExcessiveParameterList")
83 102 * @throws ValidationException
84 103 */
85 104 public function __construct(
86 105 int $id,
87 106 string $name,
88 - string $author,
89 - bool $starred,
90 - bool $published,
91 107 string $description,
92 - bool $showTitle,
93 - bool $showDescription,
94 - bool $storeEntries,
95 - array $fields = null
108 + FormMetadata $metadata,
109 + FormSettings $settings,
110 + array $fields = [],
111 + ?IntegrationSettings $integrationSettings = null,
112 + ?array $pages = null,
113 + ?array $progressIndicator = null,
114 + ?StyleSettings $styleSettings = null,
115 + ?array $formActionButtons = null,
116 + string $formType = Sanitizer::FORM_TYPE_CLASSIC,
117 + $paymentSettings = null
96 118 ) {
97 - $this->id = $this->validateId($id);
98 - $this->name = $this->validateString($name, 255, 'name');
99 - $this->author = $this->validateString($author, 1000, 'author');
100 - $this->starred = $this->validateBool($starred);
101 - $this->published = $this->validateBool($published);
102 - $this->dateCreated = gmdate('Y-m-d H:i:s');
103 - $this->dateEdited = gmdate('Y-m-d H:i:s');
104 - $this->fields = $fields ?? [];
105 - $this->description = $this->validateString($description, 1000, 'description');
106 - $this->showTitle = $this->validateBool($showTitle);
107 - $this->showDescription = $this->validateBool($showDescription);
108 - $this->storeEntries = $this->validateBool($storeEntries);
119 + $this->id = $this->validateId($id);
120 + $this->name = $this->validateString($name, 255, 'name');
121 + $this->description = $this->validateString($description, 1000, 'description');
122 + $this->metadata = $metadata;
123 + $this->settings = $settings;
124 + $this->fields = $fields;
125 + $this->integrationSettings = $integrationSettings;
126 + $this->pages = $pages;
127 + $this->progressIndicator = $progressIndicator;
128 + $this->formActionButtons = $formActionButtons ?? [
129 + 'submitButtonSettings' => [
130 + 'label' => BackendStrings::getCommonStrings()['submit'],
131 + 'position' => 'default',
132 + ],
133 + ];
134 + $this->styleSettings = $styleSettings;
135 + $this->formType = $this->normalizeStoredFormType($formType);
136 +
137 + $this->paymentSettings = $paymentSettings instanceof PaymentSettings
138 + ? $paymentSettings
139 + : new PaymentSettings(is_array($paymentSettings) ? $paymentSettings : null);
109 140 }
110 141
111 142 /**
143 + * @param string $formType
144 + * @return string
145 + */
146 + private function normalizeStoredFormType(string $formType): string
147 + {
148 + return Sanitizer::sanitizeFormType($formType);
149 + }
150 +
151 + /**
112 152 * Validates the ID.
113 153 *
114 154 * @param int $id
115 155 *
@@ -139,9 +179,9 @@
139 179 {
140 180 if (strlen($value) > $maxLength) {
141 181 throw new ValidationException(
142 182 sprintf(
143 - /* translators: 1: String value, 2: String max length. */
183 + /* translators: 1: String value, 2: String max length. */
144 184 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
145 185 esc_html($fieldName),
146 186 $maxLength
147 187 )
@@ -149,19 +189,8 @@
149 189 }
150 190 return $value;
151 191 }
152 192
153 - /**
154 - * Validates a boolean field.
155 - *
156 - * @param bool $value
157 - *
158 - * @return bool
159 - */
160 - private function validateBool(bool $value): bool
161 - {
162 - return $value;
163 - }
164 193
165 194 /**
166 195 * Get the form ID.
167 196 *
@@ -182,97 +211,89 @@
182 211 return $this->name;
183 212 }
184 213
185 214 /**
186 - * Get the form author.
187 - *
188 215 * @return string
189 216 */
190 - public function getAuthor(): string
217 + public function getFormType(): string
191 218 {
192 - return $this->author;
219 + return $this->formType;
193 220 }
194 221
195 222 /**
196 - * Get the form starred status.
197 - *
198 - * @return bool
223 + * @param string $formType
199 224 */
200 - public function isStarred(): bool
225 + public function setFormType(string $formType): void
201 226 {
202 - return $this->starred;
227 + $this->formType = $this->normalizeStoredFormType($formType);
203 228 }
229 +
204 230 /**
205 - * Get the form published status.
231 + * Get the form description.
206 232 *
207 - * @return bool
233 + * @return string
208 234 */
209 - public function isPublished(): bool
235 + public function getDescription(): string
210 236 {
211 - return $this->published;
237 + return $this->description;
212 238 }
239 +
213 240 /**
214 - * Get the date of form creation.
241 + * Get the form fields.
215 242 *
216 - * @return string
243 + * @return array<int, array<string, mixed>>|null
217 244 */
218 - public function getDateCreated(): string
245 + public function getFields(): ?array
219 246 {
220 - return $this->dateCreated;
247 + return $this->fields;
221 248 }
249 +
222 250 /**
223 - * Get the form date edited.
224 - *
225 - * @return string
251 + * Get the integration settings.
252 + * @return IntegrationSettings|null
226 253 */
227 - public function getDateEdited(): string
254 + public function getIntegrationSettings(): ?IntegrationSettings
228 255 {
229 - return $this->dateEdited;
256 + return $this->integrationSettings;
230 257 }
258 +
231 259 /**
232 - * Get the form fields.
233 - *
234 - * @return mixed[]|null
260 + * Set integration settings.
261 + * @param IntegrationSettings $settings
235 262 */
236 - public function getFields(): ?array
263 + public function setIntegrationSettings(IntegrationSettings $settings): void
237 264 {
238 - return $this->fields;
265 + $this->integrationSettings = $settings;
239 266 }
240 - /**
241 - * Get the form description.
242 - *
243 - * @return string
244 - */
245 - public function getDescription(): string
267 +
268 + public function getPaymentSettings(): PaymentSettings
246 269 {
247 - return $this->description;
270 + return $this->paymentSettings;
248 271 }
249 - /**
250 - * Get the show description of the form.
251 - *
252 - * @return bool The form show description status.
253 - */
254 - public function isDescriptionVisible(): bool
272 +
273 + public function setPaymentSettings(PaymentSettings $paymentSettings): void
255 274 {
256 - return $this->showDescription;
275 + $this->paymentSettings = $paymentSettings;
257 276 }
277 +
258 278 /**
259 - * Get the form store entries' status.
260 - * @return bool
279 + * Get style settings value object.
280 + *
281 + * @return StyleSettings|null
261 282 */
262 - public function isStoreEntries(): bool
283 + public function getStyleSettings(): ?StyleSettings
263 284 {
264 - return $this->storeEntries;
285 + return $this->styleSettings;
265 286 }
266 287
267 288 /**
268 - * Get the show title of the form.
289 + * Set style settings.
269 290 *
270 - * @return bool The form show title status.
291 + * @param StyleSettings|null $settings Style settings or null.
271 292 */
272 - public function isTitleVisible(): bool
293 + public function setStyleSettings(?StyleSettings $settings): void
273 294 {
274 - return $this->showTitle;
295 + $this->styleSettings = $settings;
275 296 }
276 297
277 298 /**
278 299 * Convert the form to an array.
@@ -280,18 +301,31 @@
280 301 * @return array<string, mixed>
281 302 */
282 303 public function toArray(): array
283 304 {
284 - return [
285 - 'id' => $this->getId(),
286 - 'name' => $this->getName(),
287 - 'author' => $this->getAuthor(),
288 - 'starred' => $this->isStarred(),
289 - 'published' => $this->isPublished(),
290 - 'fields' => $this->getFields(),
291 - 'description' => $this->getDescription(),
292 - 'showTitle' => $this->isTitleVisible(),
293 - 'showDescription' => $this->isDescriptionVisible(),
294 - 'storeEntries' => $this->isStoreEntries(),
295 - ];
305 + $integrationPayload = $this->integrationSettings !== null
306 + ? $this->integrationSettings->toArray()
307 + : ['integrationSettings' => TemplateDefaults::getDefaultIntegrationSettings()];
308 +
309 + $stylePayload = $this->styleSettings !== null
310 + ? $this->styleSettings->toArray()
311 + : ['styleSettings' => TemplateDefaults::getDefaultFormStylesSafe()];
312 +
313 + return array_merge(
314 + [
315 + 'id' => $this->getId(),
316 + 'name' => $this->getName(),
317 + 'description' => $this->getDescription(),
318 + 'formType' => $this->getFormType(),
319 + 'fields' => $this->getFields(),
320 + 'pages' => $this->pages,
321 + 'progressIndicator' => $this->progressIndicator,
322 + 'formActionButtons' => $this->formActionButtons,
323 + ],
324 + $this->metadata->toArray(),
325 + $this->settings->toArray(),
326 + $integrationPayload,
327 + $stylePayload,
328 + $this->getPaymentSettings()->toArray()
329 + );
296 330 }
297 331 }