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