PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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.0, at backend/src/ValueObjects/Form/Form.php

374 lines 9.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\ValueObjects\Form;
4
5 use IvyForms\Common\Exceptions\ValidationException;
6 use IvyForms\Services\Template\TemplateDefaults;
7 use IvyForms\Services\Translations\BackendStrings;
8 use IvyForms\ValueObjects\Form\IntegrationSettings;
9
10 /**
11 * Class Form
12 *
13 * @package IvyForms\ValueObjects\Form
14 */
15 final class Form
16 {
17 /**
18 * @var int
19 */
20 public int $id;
21 /**
22 * @var string
23 */
24 public string $name;
25 /**
26 * @var string
27 */
28 public string $author;
29 /**
30 * @var bool
31 */
32 public bool $published;
33 /**
34 * @var bool
35 */
36 public bool $starred;
37 /**
38 * @var string
39 */
40 public string $dateCreated;
41 /**
42 * @var string
43 */
44 public string $dateEdited;
45 /**
46 * @var array<int, array<string, mixed>>|null
47 */
48 public ?array $fields;
49 /**
50 * @var string
51 */
52 public string $description;
53 /**
54 * @var bool
55 */
56 public bool $showTitle;
57 /**
58 * @var bool
59 */
60 public bool $showDescription;
61 /**
62 * @var bool
63 */
64 public bool $storeEntries;
65 /**
66 * @var IntegrationSettings|null
67 */
68 public ?IntegrationSettings $integrationSettings;
69 /**
70 * @var StyleSettings|null
71 */
72 public ?StyleSettings $styleSettings;
73
74 /**
75 * @var array<string, mixed>
76 */
77 public array $formActionButtons;
78
79 /**
80 * Form constructor.
81 *
82 * @param int $id
83 * @param string $name
84 * @param string $author
85 * @param bool $starred
86 * @param bool $published
87 * @param string $description
88 * @param bool $showTitle
89 * @param bool $showDescription
90 * @param bool $storeEntries
91 * @param array<int, array<string, mixed>> $fields
92 * @param IntegrationSettings|null $integrationSettings
93 * @param StyleSettings|null $styleSettings
94 * @param array<string, mixed>|null $formActionButtons
95 * @throws ValidationException
96 * @SuppressWarnings("ExcessiveParameterList")
97 */
98 public function __construct(
99 int $id,
100 string $name,
101 string $author,
102 bool $starred,
103 bool $published,
104 string $description,
105 bool $showTitle,
106 bool $showDescription,
107 bool $storeEntries,
108 array $fields = [],
109 IntegrationSettings $integrationSettings = null,
110 ?StyleSettings $styleSettings = null,
111 ?array $formActionButtons = null
112 ) {
113 $this->id = $this->validateId($id);
114 $this->name = $this->validateString($name, 255, 'name');
115 $this->author = $this->validateString($author, 1000, 'author');
116 $this->starred = $this->validateBool($starred);
117 $this->published = $this->validateBool($published);
118 $this->dateCreated = gmdate('Y-m-d H:i:s');
119 $this->dateEdited = gmdate('Y-m-d H:i:s');
120 $this->fields = $fields;
121 $this->description = $this->validateString($description, 1000, 'description');
122 $this->showTitle = $this->validateBool($showTitle);
123 $this->showDescription = $this->validateBool($showDescription);
124 $this->storeEntries = $this->validateBool($storeEntries);
125 $this->integrationSettings = $integrationSettings;
126 $this->formActionButtons = $formActionButtons ?? [
127 'submitButtonSettings' => [
128 'label' => BackendStrings::getCommonStrings()['submit'],
129 'position' => 'default'
130 ]
131 ];
132 $this->styleSettings = $styleSettings;
133 }
134
135 /**
136 * Validates the ID.
137 *
138 * @param int $id
139 *
140 * @return int
141 * @throws ValidationException
142 */
143 private function validateId(int $id): int
144 {
145 if ($id < 0) {
146 throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
147 }
148 return $id;
149 }
150
151 /**
152 * Validates a string value.
153 *
154 * @param string $value
155 * @param int $maxLength
156 * @param string $fieldName
157 *
158 * @return string
159 *
160 * @throws ValidationException
161 */
162 private function validateString(string $value, int $maxLength, string $fieldName): string
163 {
164 if (strlen($value) > $maxLength) {
165 throw new ValidationException(
166 sprintf(
167 /* translators: 1: String value, 2: String max length. */
168 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
169 esc_html($fieldName),
170 $maxLength
171 )
172 );
173 }
174 return $value;
175 }
176
177 /**
178 * Validates a boolean field.
179 *
180 * @param bool $value
181 *
182 * @return bool
183 */
184 private function validateBool(bool $value): bool
185 {
186 return $value;
187 }
188
189 /**
190 * Get the form ID.
191 *
192 * @return int
193 */
194 public function getId(): int
195 {
196 return $this->id;
197 }
198
199 /**
200 * Get the form name.
201 *
202 * @return string
203 */
204 public function getName(): string
205 {
206 return $this->name;
207 }
208
209 /**
210 * Get the form author.
211 *
212 * @return string
213 */
214 public function getAuthor(): string
215 {
216 return $this->author;
217 }
218
219 /**
220 * Get the form starred status.
221 *
222 * @return bool
223 */
224 public function isStarred(): bool
225 {
226 return $this->starred;
227 }
228 /**
229 * Get the form published status.
230 *
231 * @return bool
232 */
233 public function isPublished(): bool
234 {
235 return $this->published;
236 }
237 /**
238 * Get the date of form creation.
239 *
240 * @return string
241 */
242 public function getDateCreated(): string
243 {
244 return $this->dateCreated;
245 }
246 /**
247 * Get the form date edited.
248 *
249 * @return string
250 */
251 public function getDateEdited(): string
252 {
253 return $this->dateEdited;
254 }
255 /**
256 * Get the form fields.
257 *
258 * @return array<int, array<string, mixed>>|null
259 */
260 public function getFields(): ?array
261 {
262 return $this->fields;
263 }
264 /**
265 * Get the form description.
266 *
267 * @return string
268 */
269 public function getDescription(): string
270 {
271 return $this->description;
272 }
273 /**
274 * Get the show description of the form.
275 *
276 * @return bool The form show description status.
277 */
278 public function isDescriptionVisible(): bool
279 {
280 return $this->showDescription;
281 }
282 /**
283 * Get the form store entries' status.
284 * @return bool
285 */
286 public function isStoreEntries(): bool
287 {
288 return $this->storeEntries;
289 }
290
291 /**
292 * Get integration settings.
293 * @return IntegrationSettings|null
294 */
295 public function getIntegrationSettings(): ?IntegrationSettings
296 {
297 return $this->integrationSettings;
298 }
299
300 /**
301 * Set integration settings.
302 * @param IntegrationSettings $settings
303 */
304 public function setIntegrationSettings(IntegrationSettings $settings): void
305 {
306 $this->integrationSettings = $settings;
307 }
308
309 /**
310 * Get style settings value object.
311 *
312 * @return StyleSettings|null
313 */
314 public function getStyleSettings(): ?StyleSettings
315 {
316 return $this->styleSettings;
317 }
318
319 /**
320 * Set style settings.
321 *
322 * @param StyleSettings|null $settings Style settings or null.
323 */
324 public function setStyleSettings(?StyleSettings $settings): void
325 {
326 $this->styleSettings = $settings;
327 }
328
329 /**
330 * Get the show title of the form.
331 *
332 * @return bool The form show title status.
333 */
334 public function isTitleVisible(): bool
335 {
336 return $this->showTitle;
337 }
338
339 /**
340 * Convert the form to an array.
341 *
342 * @return array<string, mixed>
343 */
344 public function toArray(): array
345 {
346 $integrationPayload = $this->integrationSettings !== null
347 ? $this->integrationSettings->toArray()
348 : ['integrationSettings' => TemplateDefaults::getDefaultIntegrationSettings()];
349
350 $stylePayload = $this->styleSettings !== null
351 ? $this->styleSettings->toArray()
352 : ['styleSettings' => TemplateDefaults::getDefaultFormStylesSafe()];
353
354 return array_merge(
355 [
356 'id' => $this->getId(),
357 'name' => $this->getName(),
358 'author' => $this->getAuthor(),
359 'starred' => $this->isStarred(),
360 'published' => $this->isPublished(),
361 'dateCreated' => $this->getDateCreated(),
362 'dateEdited' => $this->getDateEdited(),
363 'fields' => $this->getFields(),
364 'description' => $this->getDescription(),
365 'showTitle' => $this->isTitleVisible(),
366 'showDescription' => $this->isDescriptionVisible(),
367 'storeEntries' => $this->isStoreEntries(),
368 ],
369 $integrationPayload,
370 $stylePayload
371 );
372 }
373 }
374