PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / ValueObjects / Form / Form.php

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

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