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/Entity/Field/Field.php +199 -56 0.2 → 1.5 View file →
@@ -1,14 +1,11 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Entity\Field;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
10 5 use IvyForms\ValueObjects\Field\Field as FieldValueObject;
6 +use IvyForms\Entity\Field\FieldGeneralSettings;
7 +use IvyForms\Entity\Field\FieldAdvancedSettings;
11 8
12 9 /**
13 10 * Class Field
14 11 *
@@ -16,13 +13,26 @@
16 13 */
17 14 class Field
18 15 {
19 16 /**
17 + * @var FieldGeneralSettings
18 + */
19 + private FieldGeneralSettings $fieldGeneralSettings;
20 + /**
21 + * @var FieldAdvancedSettings
22 + */
23 + private FieldAdvancedSettings $fieldAdvancedSettings;
24 + /**
20 25 * @var FieldValueObject
21 26 */
22 27 private FieldValueObject $formField;
23 28
24 29 /**
30 + * @var array<string, mixed> Additional properties
31 + */
32 + private array $additionalProperties = [];
33 +
34 + /**
25 35 * Field constructor.
26 36 *
27 37 * @param FieldValueObject $formField The form field object.
28 38 */
@@ -28,11 +38,31 @@
28 38 */
29 39 public function __construct(FieldValueObject $formField)
30 40 {
31 41 $this->formField = $formField;
42 + $this->fieldGeneralSettings = new FieldGeneralSettings($formField->getGeneralSettings());
43 + $this->fieldAdvancedSettings = new FieldAdvancedSettings($formField->getAdvancedSettings());
44 +
45 + /**
46 + * Allows initialization of additional properties for the Field entity.
47 + * @since 0.1.0
48 + *
49 + * @param mixed[] $additionalProperties The current list of additional properties.
50 + * @param Field $field The Field entity instance.
51 + * @return mixed[] The modified list of additional properties.
52 + */
53 + $this->additionalProperties = apply_filters('ivyforms/field/entity/init_additional_properties', [], $this);
32 54 }
33 55
34 56 /**
57 + * Underlying field value object (used by Pro extension hooks).
58 + */
59 + public function getFormField(): FieldValueObject
60 + {
61 + return $this->formField;
62 + }
63 +
64 + /**
35 65 * Get the ID of the form to which this field belongs
36 66 *
37 67 * @return int The form ID.
38 68 */
@@ -111,108 +141,201 @@
111 141 $this->formField->type = $type;
112 142 }
113 143
114 144 /**
115 - * Get the label of the form field.
145 + * Get the position of the form field.
116 146 *
117 - * @return string The label of the form field.
147 + * @return int The position of the form field.
118 148 */
119 - public function getLabel(): string
149 + public function getPosition(): int
120 150 {
121 - return $this->formField->getLabel();
151 + return $this->formField->getPosition();
122 152 }
123 153
124 154 /**
125 - * Set the label of the form field.
155 + * Set the position of the form field.
126 156 *
127 - * @param string $label The label to set.
157 + * @param int $position The position to set.
128 158 */
129 - public function setLabel(string $label): void
159 + public function setPosition(int $position): void
130 160 {
131 - $this->formField->label = $label;
161 + $this->formField->position = $position;
132 162 }
133 163
134 164 /**
135 - * Get the default value of the form field.
165 + * Get the row index of the form field.
136 166 *
137 - * @return string The default value of the form field.
167 + * @return int The row index of the form field.
138 168 */
139 - public function getDefaultValue(): string
169 + public function getRowIndex(): int
140 170 {
141 - return $this->formField->getDefaultValue();
171 + return $this->formField->getRowIndex();
142 172 }
143 173
144 174 /**
145 - * Set the default value of the form field.
175 + * Set the row index of the form field.
146 176 *
147 - * @param string $defaultValue The default value to set.
177 + * @param int $rowIndex The row index to set.
148 178 */
149 - public function setDefaultValue(string $defaultValue): void
179 + public function setRowIndex(int $rowIndex): void
150 180 {
151 - $this->formField->defaultValue = $defaultValue;
181 + $this->formField->rowIndex = $rowIndex;
152 182 }
153 183
154 184 /**
155 - * Check if the form field is required.
185 + * Get the column index of the form field.
156 186 *
157 - * @return bool True if the form field is required, false otherwise.
187 + * @return int The column index of the form field.
158 188 */
159 - public function isRequired(): bool
189 + public function getColumnIndex(): int
160 190 {
161 - return $this->formField->isRequired();
191 + return $this->formField->getColumnIndex();
162 192 }
163 193
164 194 /**
165 - * Set the required status of the form field.
195 + * Set the column index of the form field.
166 196 *
167 - * @param bool $required The required status to set.
197 + * @param int $columnIndex The column index to set.
168 198 */
169 - public function setRequired(bool $required): void
199 + public function setColumnIndex(int $columnIndex): void
170 200 {
171 - $this->formField->required = $required;
201 + $this->formField->columnIndex = $columnIndex;
172 202 }
173 203
174 204 /**
175 - * Get the placeholder of the form field.
205 + * Get the width percentage of the form field.
176 206 *
177 - * @return string The placeholder of the form field.
207 + * @return int The width percentage of the form field.
178 208 */
179 - public function getPlaceholder(): string
209 + public function getWidth(): int
180 210 {
181 - return $this->formField->getPlaceholder();
211 + return $this->formField->getWidth();
182 212 }
183 213
184 214 /**
185 - * Set the placeholder of the form field.
215 + * Set the width percentage of the form field.
186 216 *
187 - * @param string $placeholder The placeholder to set.
217 + * @param int $width The width percentage to set (20-100).
188 218 */
189 - public function setPlaceholder(string $placeholder): void
219 + public function setWidth(int $width): void
190 220 {
191 - $this->formField->placeholder = $placeholder;
221 + $this->formField->width = $width;
192 222 }
193 223
194 224 /**
195 - * Get the position of the form field.
225 + * Get the parent field ID.
196 226 *
197 - * @return int The position of the form field.
227 + * @return int|null The parent field ID.
198 228 */
199 - public function getPosition(): int
229 + public function getParentId(): ?int
200 230 {
201 - return $this->formField->getPosition();
231 + return $this->formField->getParentId();
202 232 }
203 233
204 234 /**
205 - * Set the position of the form field.
235 + * Set the parent field ID.
206 236 *
207 - * @param int $position The position to set.
237 + * @param int|null $parentId The parent field ID to set.
208 238 */
209 - public function setPosition(int $position): void
239 + public function setParentId(?int $parentId): void
210 240 {
211 - $this->formField->position = $position;
241 + $this->formField->parentId = $parentId;
212 242 }
213 243
214 244 /**
245 + * Get the page ID for multi-page forms.
246 + *
247 + * @return string|null The page ID.
248 + */
249 + public function getPageId(): ?string
250 + {
251 + return $this->formField->pageId;
252 + }
253 +
254 + /**
255 + * Set the page ID for multi-page forms.
256 + *
257 + * @param string|null $pageId The page ID to set.
258 + */
259 + public function setPageId(?string $pageId): void
260 + {
261 + $this->formField->pageId = $pageId;
262 + }
263 +
264 + /**
265 + * Set the number of rows (for textarea fields).
266 + *
267 + * @param int $rows
268 + */
269 + public function setRows(int $rows): void
270 + {
271 + $this->formField->setRows($rows);
272 + }
273 +
274 + /**
275 + * Get the FieldGeneralSettings entity.
276 + */
277 + public function getFieldGeneralSettings(): FieldGeneralSettings
278 + {
279 + return $this->fieldGeneralSettings;
280 + }
281 +
282 + /**
283 + * Get the FieldAdvancedSettings entity.
284 + */
285 + public function getFieldAdvancedSettings(): FieldAdvancedSettings
286 + {
287 + return $this->fieldAdvancedSettings;
288 + }
289 +
290 + /**
291 + * Set an additional property
292 + *
293 + * @param string $key
294 + * @param mixed $value
295 + *
296 + * @return void
297 + */
298 + public function setAdditionalProperty(string $key, $value): void
299 + {
300 + foreach ($this->additionalProperties as &$property) {
301 + if (array_key_exists($key, $property)) {
302 + $property[$key] = $value; // update existing
303 + return;
304 + }
305 + }
306 +
307 + // if not found, append new key-value pair
308 + $this->additionalProperties[] = [$key => $value];
309 + }
310 +
311 + /**
312 + * Get an additional property
313 + *
314 + * @param string $key
315 + * @return mixed|null The value of the additional property, or null if not found
316 + */
317 + public function getAdditionalProperty(string $key)
318 + {
319 + foreach ($this->additionalProperties as $property) {
320 + if (array_key_exists($key, $property)) {
321 + return $property[$key];
322 + }
323 + }
324 + return null;
325 + }
326 +
327 + /**
328 + * Get all additional properties
329 + *
330 + * @return array<string, mixed> The additional properties
331 + */
332 + public function getAllAdditionalProperties(): array
333 + {
334 + return $this->additionalProperties;
335 + }
336 +
337 + /**
215 338 * Convert the form field entity to an array.
216 339 *
217 340 * @return array<string, mixed> The form field entity as an array.
218 341 */
@@ -217,17 +340,37 @@
217 340 * @return array<string, mixed> The form field entity as an array.
218 341 */
219 342 public function toArray(): array
220 343 {
221 - return [
222 - 'id' => $this->getId(),
223 - 'formId' => $this->getFormId(),
224 - 'fieldIndex' => $this->getIndex(),
225 - 'type' => $this->getType(),
226 - 'label' => $this->getLabel(),
227 - 'defaultValue' => $this->getDefaultValue(),
228 - 'required' => $this->isRequired(),
229 - 'placeholder' => $this->getPlaceholder(),
230 - 'position' => $this->getPosition(),
231 - ];
344 + $data = array_merge(
345 + [
346 + 'id' => $this->getId(),
347 + 'formId' => $this->getFormId(),
348 + 'fieldIndex' => $this->getIndex(),
349 + 'type' => $this->getType(),
350 + 'position' => $this->getPosition(),
351 + 'rowIndex' => $this->getRowIndex(),
352 + 'columnIndex' => $this->getColumnIndex(),
353 + 'width' => $this->getWidth(),
354 + 'parentId' => $this->getParentId(),
355 + 'pageId' => $this->getPageId(),
356 + 'rows' => $this->formField->getRows()
357 + ],
358 + $this->getFieldGeneralSettings()->toArray(),
359 + $this->getFieldAdvancedSettings()->toArray()
360 + );
361 +
362 + foreach ($this->additionalProperties as $property) {
363 + $data = array_merge($data, $property);
364 + }
365 +
366 + /**
367 + * Allows modification of the array representation of the Field entity.
368 + * @since 0.1.0
369 + *
370 + * @param mixed[] $data The current array representation of the Field entity.
371 + * @param Field $field The Field entity instance.
372 + * @return mixed[] The modified array representation of the Field entity.
373 + */
374 + return apply_filters('ivyforms/field/entity/add_to_array_additional_properties', $data, $this);
232 375 }
233 376 }