PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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 / Field / Field.php

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

434 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace IvyForms\ValueObjects\Field;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 use IvyForms\Common\Exceptions\ValidationException;
17
18 /**
19 * Class Field
20 *
21 * @package IvyForms\ValueObjects\Field
22 */
23 final class Field
24 {
25 /**
26 * @var int
27 */
28 public int $id;
29 /**
30 * @var int
31 */
32 public int $formId;
33 /**
34 * @var int
35 */
36 public int $fieldIndex;
37 /**
38 * @var string
39 */
40 public string $type;
41 /**
42 * @var string
43 */
44 public string $label;
45 /**
46 * @var string
47 */
48 public string $defaultValue;
49 /**
50 * @var bool
51 */
52 public bool $required;
53 /**
54 * @var string
55 */
56 public string $placeholder;
57 /**
58 * @var int
59 */
60 public int $position;
61 /**
62 * @var bool
63 */
64 public bool $hideLabel;
65 /**
66 * @var bool
67 */
68 public bool $readOnly;
69 /**
70 * @var string
71 */
72 public string $description;
73 /**
74 * @var string
75 */
76 public string $requiredMessage;
77 /**
78 * @var string
79 */
80 public string $cssClasses;
81
82 /**
83 * Field constructor.
84 * @SuppressWarnings(PHPMD)
85 *
86 * @param int $id
87 * @param int $formId
88 * @param int $fieldIndex
89 * @param string $type
90 * @param string $label
91 * @param string $defaultValue
92 * @param bool $required
93 * @param string $placeholder
94 * @param int $position
95 * @param bool $hideLabel
96 * @param bool $readOnly
97 * @param string $description
98 * @param string $requiredMessage
99 * @param string $cssClasses
100 *
101 * @throws ValidationException
102 */
103 public function __construct(
104 int $id,
105 int $formId,
106 int $fieldIndex,
107 string $type,
108 string $label,
109 string $defaultValue,
110 bool $required,
111 string $placeholder,
112 int $position,
113 bool $hideLabel,
114 bool $readOnly,
115 string $description = '',
116 string $requiredMessage = '',
117 string $cssClasses = ''
118 ) {
119 $this->id = $this->validateId($id);
120 $this->formId = $this->validateId($formId) ;
121 $this->fieldIndex = $this->validateId($fieldIndex);
122 $this->type = $this->validateString($type, 50, 'type');
123 $this->label = $this->validateString($label, 255, 'label');
124 $this->defaultValue = $this->validateString($defaultValue, 255, 'defaultValue');
125 $this->required = $this->validateRequired($required);
126 $this->placeholder = $this->validateString($placeholder, 255, 'placeholder');
127 $this->position = $this->validatePosition($position);
128 $this->hideLabel = $this->validateBool($hideLabel);
129 $this->readOnly = $this->validateBool($readOnly);
130 $this->description = $this->validateString($description, 255, 'description');
131 $this->requiredMessage = $this->validateString($requiredMessage, 255, 'requiredMessage');
132 $this->cssClasses = $this->validateString($cssClasses, 255, 'cssClasses');
133 }
134
135 /**
136 * Validates the ID.
137 *
138 * @param int $id
139 *
140 * @return int
141 * @throws ValidationException
142 */
143 public function validateId(int $id): int
144 {
145 if ($id < 0) {
146 throw new ValidationException("Id must be a 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 the required field.
179 *
180 * @param bool $value
181 *
182 * @return bool
183 *
184 */
185 private function validateRequired(bool $value): bool
186 {
187 return $value;
188 }
189
190 /**
191 * Validates the position.
192 *
193 * @param int $value
194 *
195 * @return int
196 *
197 * @throws ValidationException
198 */
199 private function validatePosition(int $value): int
200 {
201 if ($value < 0) {
202 throw new ValidationException("position must be a non-negative integer.");
203 }
204 return $value;
205 }
206
207 /**
208 * Validates a boolean field.
209 *
210 * @param bool $value
211 *
212 * @return bool
213 */
214 private function validateBool(bool $value): bool
215 {
216 return $value;
217 }
218
219 /**
220 * Get the form ID.
221 *
222 * @return int
223 */
224 public function getFormId(): int
225 {
226 return $this->formId;
227 }
228
229 /**
230 * Get the field ID.
231 *
232 * @return int
233 */
234 public function getId(): int
235 {
236 return $this->id;
237 }
238
239 /**
240 * Get the field index.
241 *
242 * @return int
243 */
244 public function getIndex(): int
245 {
246 return $this->fieldIndex;
247 }
248
249 /**
250 * Get the field type.
251 *
252 * @return string
253 */
254 public function getType(): string
255 {
256 return $this->type;
257 }
258
259 /**
260 * Get the field label.
261 *
262 * @return string
263 */
264 public function getLabel(): string
265 {
266 return $this->label;
267 }
268
269 /**
270 * Get the default value.
271 *
272 * @return string
273 */
274 public function getDefaultValue(): string
275 {
276 return $this->defaultValue;
277 }
278
279 /**
280 * Check if the field is required.
281 *
282 * @return bool
283 */
284 public function isRequired(): bool
285 {
286 return $this->required;
287 }
288
289 /**
290 * Get the field placeholder.
291 *
292 * @return string
293 */
294 public function getPlaceholder(): string
295 {
296 return $this->placeholder;
297 }
298
299 /**
300 * Get the field position.
301 *
302 * @return int
303 */
304 public function getPosition(): int
305 {
306 return $this->position;
307 }
308
309 /**
310 * Check if the field label should be hidden.
311 *
312 * @return bool
313 */
314 public function isLabelHidden(): bool
315 {
316 return $this->hideLabel;
317 }
318
319 /**
320 * Set the hideLabel setting.
321 *
322 * @param bool $hideLabel
323 */
324 public function setHideLabel(bool $hideLabel): void
325 {
326 $this->hideLabel = $hideLabel;
327 }
328
329 /**
330 * Check if the field is read-only.
331 *
332 * @return bool
333 */
334 public function isReadOnly(): bool
335 {
336 return $this->readOnly;
337 }
338
339 /**
340 * Set the readOnly setting.
341 *
342 * @param bool $readOnly
343 */
344 public function setReadOnly(bool $readOnly): void
345 {
346 $this->readOnly = $readOnly;
347 }
348
349 /**
350 * Get the field description.
351 *
352 * @return string
353 */
354 public function getDescription(): string
355 {
356 return $this->description;
357 }
358
359 /**
360 * Set the description setting.
361 *
362 * @param string $description
363 */
364 public function setDescription(string $description): void
365 {
366 $this->description = $description;
367 }
368
369 /**
370 * Get the field required message.
371 *
372 * @return string
373 */
374 public function getRequiredMessage(): string
375 {
376 return $this->requiredMessage;
377 }
378
379 /**
380 * Set the required message setting.
381 *
382 * @param string $requiredMessage
383 */
384 public function setRequiredMessage(string $requiredMessage): void
385 {
386 $this->requiredMessage = $requiredMessage;
387 }
388
389 /**
390 * Get the field CSS classes.
391 *
392 * @return string
393 */
394 public function getCssClasses(): string
395 {
396 return $this->cssClasses;
397 }
398
399 /**
400 * Set the CSS classes setting.
401 *
402 * @param string $cssClasses
403 */
404 public function setCssClasses(string $cssClasses): void
405 {
406 $this->cssClasses = $cssClasses;
407 }
408
409 /**
410 * Convert the field to an array.
411 *
412 * @return array<string, mixed>
413 */
414 public function toArray(): array
415 {
416 return [
417 'id' => $this->getId(),
418 'formId' => $this->getFormId(),
419 'fieldIndex' => $this->getIndex(),
420 'type' => $this->getType(),
421 'label' => $this->getLabel(),
422 'defaultValue' => $this->getDefaultValue(),
423 'required' => $this->isRequired(),
424 'placeholder' => $this->getPlaceholder(),
425 'position' => $this->getPosition(),
426 'hideLabel' => $this->isLabelHidden(),
427 'readOnly' => $this->isReadOnly(),
428 'description' => $this->getDescription(),
429 'requiredMessage' => $this->getRequiredMessage(),
430 'cssClasses' => $this->getCssClasses(),
431 ];
432 }
433 }
434