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