| @@ -1,13 +1,8 @@ | ||
| 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; |
| 11 | 6 | use IvyForms\Entity\Field\FieldGeneralSettings; |
| 12 | 7 | use IvyForms\Entity\Field\FieldAdvancedSettings; |
| 13 | 8 | |
| @@ -31,8 +26,13 @@ | ||
| 31 | 26 | */ |
| 32 | 27 | private FieldValueObject $formField; |
| 33 | 28 | |
| 34 | 29 | /** |
| 30 | + * @var array<string, mixed> Additional properties | |
| 31 | + */ | |
| 32 | + private array $additionalProperties = []; | |
| 33 | + | |
| 34 | + /** | |
| 35 | 35 | * Field constructor. |
| 36 | 36 | * |
| 37 | 37 | * @param FieldValueObject $formField The form field object. |
| 38 | 38 | */ |
| @@ -40,11 +40,29 @@ | ||
| 40 | 40 | { |
| 41 | 41 | $this->formField = $formField; |
| 42 | 42 | $this->fieldGeneralSettings = new FieldGeneralSettings($formField->getGeneralSettings()); |
| 43 | 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); | |
| 44 | 54 | } |
| 45 | 55 | |
| 46 | 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 | + /** | |
| 47 | 65 | * Get the ID of the form to which this field belongs |
| 48 | 66 | * |
| 49 | 67 | * @return int The form ID. |
| 50 | 68 | */ |
| @@ -143,105 +161,105 @@ | ||
| 143 | 161 | $this->formField->position = $position; |
| 144 | 162 | } |
| 145 | 163 | |
| 146 | 164 | /** |
| 147 | - * Get the parent field ID. | |
| 165 | + * Get the row index of the form field. | |
| 148 | 166 | * |
| 149 | - * @return int|null The parent field ID. | |
| 167 | + * @return int The row index of the form field. | |
| 150 | 168 | */ |
| 151 | - public function getParentId(): ?int | |
| 169 | + public function getRowIndex(): int | |
| 152 | 170 | { |
| 153 | - return $this->formField->getParentId(); | |
| 171 | + return $this->formField->getRowIndex(); | |
| 154 | 172 | } |
| 155 | 173 | |
| 156 | 174 | /** |
| 157 | - * Set the parent field ID. | |
| 175 | + * Set the row index of the form field. | |
| 158 | 176 | * |
| 159 | - * @param int|null $parentId The parent field ID to set. | |
| 177 | + * @param int $rowIndex The row index to set. | |
| 160 | 178 | */ |
| 161 | - public function setParentId(?int $parentId): void | |
| 179 | + public function setRowIndex(int $rowIndex): void | |
| 162 | 180 | { |
| 163 | - $this->formField->parentId = $parentId; | |
| 181 | + $this->formField->rowIndex = $rowIndex; | |
| 164 | 182 | } |
| 165 | 183 | |
| 166 | 184 | /** |
| 167 | - * Check if the confirm field feature is enabled. | |
| 185 | + * Get the column index of the form field. | |
| 168 | 186 | * |
| 169 | - * @return bool True if the confirm field feature is enabled, false otherwise. | |
| 187 | + * @return int The column index of the form field. | |
| 170 | 188 | */ |
| 171 | - public function isConfirmFieldEnabled(): bool | |
| 189 | + public function getColumnIndex(): int | |
| 172 | 190 | { |
| 173 | - return $this->formField->isConfirmFieldEnabled(); | |
| 191 | + return $this->formField->getColumnIndex(); | |
| 174 | 192 | } |
| 175 | 193 | |
| 176 | 194 | /** |
| 177 | - * Get the label for the confirm field field. | |
| 195 | + * Set the column index of the form field. | |
| 178 | 196 | * |
| 179 | - * @return string The label for the confirm field field. | |
| 197 | + * @param int $columnIndex The column index to set. | |
| 180 | 198 | */ |
| 181 | - public function getConfirmFieldLabel(): string | |
| 199 | + public function setColumnIndex(int $columnIndex): void | |
| 182 | 200 | { |
| 183 | - return $this->formField->getConfirmFieldLabel(); | |
| 201 | + $this->formField->columnIndex = $columnIndex; | |
| 184 | 202 | } |
| 185 | 203 | |
| 186 | 204 | /** |
| 187 | - * Get the placeholder for the confirm field field. | |
| 205 | + * Get the width percentage of the form field. | |
| 188 | 206 | * |
| 189 | - * @return string The placeholder for the confirm field field. | |
| 207 | + * @return int The width percentage of the form field. | |
| 190 | 208 | */ |
| 191 | - public function getConfirmFieldPlaceholder(): string | |
| 209 | + public function getWidth(): int | |
| 192 | 210 | { |
| 193 | - return $this->formField->getConfirmFieldPlaceholder(); | |
| 211 | + return $this->formField->getWidth(); | |
| 194 | 212 | } |
| 195 | 213 | |
| 196 | 214 | /** |
| 197 | - * Check if the confirm field label should be hidden. | |
| 215 | + * Set the width percentage of the form field. | |
| 198 | 216 | * |
| 199 | - * @return bool True if the confirm label should be hidden, false otherwise. | |
| 217 | + * @param int $width The width percentage to set (20-100). | |
| 200 | 218 | */ |
| 201 | - public function isConfirmFieldHideLabel(): bool | |
| 219 | + public function setWidth(int $width): void | |
| 202 | 220 | { |
| 203 | - return $this->formField->isConfirmFieldHideLabel(); | |
| 221 | + $this->formField->width = $width; | |
| 204 | 222 | } |
| 205 | 223 | |
| 206 | 224 | /** |
| 207 | - * Set the enabled status of the confirm field feature. | |
| 225 | + * Get the parent field ID. | |
| 208 | 226 | * |
| 209 | - * @param bool $enabled The enabled status to set. | |
| 227 | + * @return int|null The parent field ID. | |
| 210 | 228 | */ |
| 211 | - public function setConfirmFieldEnabled(bool $enabled): void | |
| 229 | + public function getParentId(): ?int | |
| 212 | 230 | { |
| 213 | - $this->formField->setConfirmFieldEnabled($enabled); | |
| 231 | + return $this->formField->getParentId(); | |
| 214 | 232 | } |
| 215 | 233 | |
| 216 | 234 | /** |
| 217 | - * Set the label for the confirm field. | |
| 235 | + * Set the parent field ID. | |
| 218 | 236 | * |
| 219 | - * @param string $label The label to set. | |
| 237 | + * @param int|null $parentId The parent field ID to set. | |
| 220 | 238 | */ |
| 221 | - public function setConfirmFieldLabel(string $label): void | |
| 239 | + public function setParentId(?int $parentId): void | |
| 222 | 240 | { |
| 223 | - $this->formField->setConfirmFieldLabel($label); | |
| 241 | + $this->formField->parentId = $parentId; | |
| 224 | 242 | } |
| 225 | 243 | |
| 226 | 244 | /** |
| 227 | - * Set the placeholder for the confirm field. | |
| 245 | + * Get the page ID for multi-page forms. | |
| 228 | 246 | * |
| 229 | - * @param string $ph The placeholder to set. | |
| 247 | + * @return string|null The page ID. | |
| 230 | 248 | */ |
| 231 | - public function setConfirmFieldPlaceholder(string $ph): void | |
| 249 | + public function getPageId(): ?string | |
| 232 | 250 | { |
| 233 | - $this->formField->setConfirmFieldPlaceholder($ph); | |
| 251 | + return $this->formField->pageId; | |
| 234 | 252 | } |
| 235 | 253 | |
| 236 | 254 | /** |
| 237 | - * Set the hideLabel setting for the confirm field. | |
| 255 | + * Set the page ID for multi-page forms. | |
| 238 | 256 | * |
| 239 | - * @param bool $hide The hideLabel setting to set. | |
| 257 | + * @param string|null $pageId The page ID to set. | |
| 240 | 258 | */ |
| 241 | - public function setConfirmFieldHideLabel(bool $hide): void | |
| 259 | + public function setPageId(?string $pageId): void | |
| 242 | 260 | { |
| 243 | - $this->formField->setConfirmFieldHideLabel($hide); | |
| 261 | + $this->formField->pageId = $pageId; | |
| 244 | 262 | } |
| 245 | 263 | |
| 246 | 264 | /** |
| 247 | 265 | * Set the number of rows (for textarea fields). |
| @@ -268,10 +286,56 @@ | ||
| 268 | 286 | { |
| 269 | 287 | return $this->fieldAdvancedSettings; |
| 270 | 288 | } |
| 271 | 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 | + } | |
| 272 | 306 | |
| 307 | + // if not found, append new key-value pair | |
| 308 | + $this->additionalProperties[] = [$key => $value]; | |
| 309 | + } | |
| 310 | + | |
| 273 | 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 | + /** | |
| 274 | 338 | * Convert the form field entity to an array. |
| 275 | 339 | * |
| 276 | 340 | * @return array<string, mixed> The form field entity as an array. |
| 277 | 341 | */ |
| @@ -276,9 +340,9 @@ | ||
| 276 | 340 | * @return array<string, mixed> The form field entity as an array. |
| 277 | 341 | */ |
| 278 | 342 | public function toArray(): array |
| 279 | 343 | { |
| 280 | - return array_merge( | |
| 344 | + $data = array_merge( | |
| 281 | 345 | [ |
| 282 | 346 | 'id' => $this->getId(), |
| 283 | 347 | 'formId' => $this->getFormId(), |
| 284 | 348 | 'fieldIndex' => $this->getIndex(), |
| @@ -283,16 +347,30 @@ | ||
| 283 | 347 | 'formId' => $this->getFormId(), |
| 284 | 348 | 'fieldIndex' => $this->getIndex(), |
| 285 | 349 | 'type' => $this->getType(), |
| 286 | 350 | 'position' => $this->getPosition(), |
| 351 | + 'rowIndex' => $this->getRowIndex(), | |
| 352 | + 'columnIndex' => $this->getColumnIndex(), | |
| 353 | + 'width' => $this->getWidth(), | |
| 287 | 354 | 'parentId' => $this->getParentId(), |
| 288 | - 'rows' => $this->formField->getRows(), | |
| 289 | - 'confirmFieldEnabled' => $this->isConfirmFieldEnabled(), | |
| 290 | - 'confirmFieldLabel' => $this->getConfirmFieldLabel(), | |
| 291 | - 'confirmFieldPlaceholder' => $this->getConfirmFieldPlaceholder(), | |
| 292 | - 'confirmFieldHideLabel' => $this->isConfirmFieldHideLabel(), | |
| 355 | + 'pageId' => $this->getPageId(), | |
| 356 | + 'rows' => $this->formField->getRows() | |
| 293 | 357 | ], |
| 294 | 358 | $this->getFieldGeneralSettings()->toArray(), |
| 295 | 359 | $this->getFieldAdvancedSettings()->toArray() |
| 296 | 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); | |
| 297 | 375 | } |
| 298 | 376 | } |