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 / Entry / EntryField.php

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

155 lines 3.3 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\Entry;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 use IvyForms\Common\Exceptions\ValidationException;
7 use IvyForms\Services\Translations\BackendStrings;
8
9 if (!defined('ABSPATH')) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Class EntryField
15 *
16 * Represents a single field value in a form entry.
17 */
18 final class EntryField
19 {
20 /**
21 * @var int|null
22 */
23 public ?int $id;
24 /**
25 * @var int
26 */
27 public int $fieldId;
28 /**
29 * @var string
30 */
31 public string $fieldValue;
32 /**
33 * @var int
34 */
35 public int $entryId;
36
37 /**
38 * EntryField constructor.
39 *
40 * @param int|null $id
41 * @param int $entryId
42 * @param int $fieldId
43 * @param string $fieldValue
44 * @throws ValidationException
45 */
46 public function __construct(
47 ?int $id,
48 int $entryId,
49 int $fieldId,
50 string $fieldValue
51 ) {
52 $this->id = $id ? $this->validateId($id) : null;
53 $this->entryId = $this->validateId($entryId);
54 $this->fieldId = $this->validateId($fieldId);
55 $this->fieldValue = $this->validateString($fieldValue, 65535, 'fieldValue');
56 }
57
58 /**
59 * Get the ID of the entry field.
60 *
61 * @return int|null The entry field ID.
62 */
63 public function getId(): ?int
64 {
65 return $this->id;
66 }
67
68 /**
69 * Get the field ID.
70 *
71 * @return int The field ID.
72 */
73 public function getFieldId(): int
74 {
75 return $this->fieldId;
76 }
77
78 /**
79 * Get the field value.
80 *
81 * @return string The field value.
82 */
83 public function getFieldValue(): string
84 {
85 return $this->fieldValue;
86 }
87
88 /**
89 * Get the entry ID.
90 *
91 * @return int The entry ID or null if not set.
92 */
93 public function getEntryId(): int
94 {
95 return $this->entryId;
96 }
97
98 /**
99 * Validates the ID.
100 *
101 * @param int $id
102 *
103 * @return int
104 * @throws ValidationException
105 */
106 private function validateId(int $id): int
107 {
108 if ($id < 0) {
109 throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
110 }
111 return $id;
112 }
113
114 /**
115 * Validates a string value.
116 *
117 * @param string $value
118 * @param int $maxLength
119 * @param string $fieldName
120 *
121 * @return string
122 *
123 * @throws ValidationException
124 */
125 private function validateString(string $value, int $maxLength, string $fieldName): string
126 {
127 if (strlen($value) > $maxLength) {
128 throw new ValidationException(
129 sprintf(
130 /* translators: 1: String value, 2: String max length. */
131 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
132 esc_html($fieldName),
133 $maxLength
134 )
135 );
136 }
137 return $value;
138 }
139
140 /**
141 * Convert the EntryField object to an array.
142 *
143 * @return array<mixed> The array representation of the EntryField.
144 */
145 public function toArray(): array
146 {
147 return [
148 'id' => $this->getId(),
149 'entryId' => $this->getEntryId(),
150 'fieldId' => $this->getFieldId(),
151 'fieldValue' => $this->getFieldValue(),
152 ];
153 }
154 }
155