| @@ -1,0 +1,376 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Entity\Field; | |
| 4 | + | |
| 5 | +use IvyForms\ValueObjects\Field\Field as FieldValueObject; | |
| 6 | +use IvyForms\Entity\Field\FieldGeneralSettings; | |
| 7 | +use IvyForms\Entity\Field\FieldAdvancedSettings; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Class Field | |
| 11 | + * | |
| 12 | + * @package IvyForms\Entity\Field | |
| 13 | + */ | |
| 14 | +class Field | |
| 15 | +{ | |
| 16 | + /** | |
| 17 | + * @var FieldGeneralSettings | |
| 18 | + */ | |
| 19 | + private FieldGeneralSettings $fieldGeneralSettings; | |
| 20 | + /** | |
| 21 | + * @var FieldAdvancedSettings | |
| 22 | + */ | |
| 23 | + private FieldAdvancedSettings $fieldAdvancedSettings; | |
| 24 | + /** | |
| 25 | + * @var FieldValueObject | |
| 26 | + */ | |
| 27 | + private FieldValueObject $formField; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @var array<string, mixed> Additional properties | |
| 31 | + */ | |
| 32 | + private array $additionalProperties = []; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Field constructor. | |
| 36 | + * | |
| 37 | + * @param FieldValueObject $formField The form field object. | |
| 38 | + */ | |
| 39 | + public function __construct(FieldValueObject $formField) | |
| 40 | + { | |
| 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); | |
| 54 | + } | |
| 55 | + | |
| 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 | + /** | |
| 65 | + * Get the ID of the form to which this field belongs | |
| 66 | + * | |
| 67 | + * @return int The form ID. | |
| 68 | + */ | |
| 69 | + public function getFormId(): int | |
| 70 | + { | |
| 71 | + return $this->formField->getFormId(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Set the ID of the form to which this field belongs. | |
| 76 | + * | |
| 77 | + * @param int $formId The form ID to set. | |
| 78 | + */ | |
| 79 | + public function setFormId(int $formId): void | |
| 80 | + { | |
| 81 | + $this->formField->formId = $formId; | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Get ID of the form field. | |
| 86 | + * | |
| 87 | + * @return int The form field id. | |
| 88 | + */ | |
| 89 | + public function getId(): int | |
| 90 | + { | |
| 91 | + return $this->formField->getId(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Set the ID of the form field. | |
| 96 | + * | |
| 97 | + * @param int $id The ID to set. | |
| 98 | + */ | |
| 99 | + public function setId(int $id): void | |
| 100 | + { | |
| 101 | + $this->formField->id = $id; | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Get the index of the form field. | |
| 106 | + * | |
| 107 | + * @return int The index of the form field. | |
| 108 | + */ | |
| 109 | + public function getIndex(): int | |
| 110 | + { | |
| 111 | + return $this->formField->getIndex(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Set the index of the form field. | |
| 116 | + * | |
| 117 | + * @param int $index The index to set. | |
| 118 | + */ | |
| 119 | + public function setIndex(int $index): void | |
| 120 | + { | |
| 121 | + $this->formField->fieldIndex = $index; | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Get the type of the form field. | |
| 126 | + * | |
| 127 | + * @return string The type of the form field. | |
| 128 | + */ | |
| 129 | + public function getType(): string | |
| 130 | + { | |
| 131 | + return $this->formField->getType(); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Set the type of the form field. | |
| 136 | + * | |
| 137 | + * @param string $type The type to set. | |
| 138 | + */ | |
| 139 | + public function setType(string $type): void | |
| 140 | + { | |
| 141 | + $this->formField->type = $type; | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Get the position of the form field. | |
| 146 | + * | |
| 147 | + * @return int The position of the form field. | |
| 148 | + */ | |
| 149 | + public function getPosition(): int | |
| 150 | + { | |
| 151 | + return $this->formField->getPosition(); | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Set the position of the form field. | |
| 156 | + * | |
| 157 | + * @param int $position The position to set. | |
| 158 | + */ | |
| 159 | + public function setPosition(int $position): void | |
| 160 | + { | |
| 161 | + $this->formField->position = $position; | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Get the row index of the form field. | |
| 166 | + * | |
| 167 | + * @return int The row index of the form field. | |
| 168 | + */ | |
| 169 | + public function getRowIndex(): int | |
| 170 | + { | |
| 171 | + return $this->formField->getRowIndex(); | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Set the row index of the form field. | |
| 176 | + * | |
| 177 | + * @param int $rowIndex The row index to set. | |
| 178 | + */ | |
| 179 | + public function setRowIndex(int $rowIndex): void | |
| 180 | + { | |
| 181 | + $this->formField->rowIndex = $rowIndex; | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Get the column index of the form field. | |
| 186 | + * | |
| 187 | + * @return int The column index of the form field. | |
| 188 | + */ | |
| 189 | + public function getColumnIndex(): int | |
| 190 | + { | |
| 191 | + return $this->formField->getColumnIndex(); | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * Set the column index of the form field. | |
| 196 | + * | |
| 197 | + * @param int $columnIndex The column index to set. | |
| 198 | + */ | |
| 199 | + public function setColumnIndex(int $columnIndex): void | |
| 200 | + { | |
| 201 | + $this->formField->columnIndex = $columnIndex; | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Get the width percentage of the form field. | |
| 206 | + * | |
| 207 | + * @return int The width percentage of the form field. | |
| 208 | + */ | |
| 209 | + public function getWidth(): int | |
| 210 | + { | |
| 211 | + return $this->formField->getWidth(); | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Set the width percentage of the form field. | |
| 216 | + * | |
| 217 | + * @param int $width The width percentage to set (20-100). | |
| 218 | + */ | |
| 219 | + public function setWidth(int $width): void | |
| 220 | + { | |
| 221 | + $this->formField->width = $width; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Get the parent field ID. | |
| 226 | + * | |
| 227 | + * @return int|null The parent field ID. | |
| 228 | + */ | |
| 229 | + public function getParentId(): ?int | |
| 230 | + { | |
| 231 | + return $this->formField->getParentId(); | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Set the parent field ID. | |
| 236 | + * | |
| 237 | + * @param int|null $parentId The parent field ID to set. | |
| 238 | + */ | |
| 239 | + public function setParentId(?int $parentId): void | |
| 240 | + { | |
| 241 | + $this->formField->parentId = $parentId; | |
| 242 | + } | |
| 243 | + | |
| 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 | + /** | |
| 338 | + * Convert the form field entity to an array. | |
| 339 | + * | |
| 340 | + * @return array<string, mixed> The form field entity as an array. | |
| 341 | + */ | |
| 342 | + public function toArray(): array | |
| 343 | + { | |
| 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); | |
| 375 | + } | |
| 376 | +} | |