| @@ -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,205 +141,198 @@ | ||
| 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 | /** |
| 215 | - * Check if the field label should be hidden. | |
| 245 | + * Get the page ID for multi-page forms. | |
| 216 | 246 | * |
| 217 | - * @return bool True if the label should be hidden, false otherwise. | |
| 247 | + * @return string|null The page ID. | |
| 218 | 248 | */ |
| 219 | - public function isLabelHidden(): bool | |
| 249 | + public function getPageId(): ?string | |
| 220 | 250 | { |
| 221 | - return $this->formField->isLabelHidden(); | |
| 251 | + return $this->formField->pageId; | |
| 222 | 252 | } |
| 223 | 253 | |
| 224 | 254 | /** |
| 225 | - * Set the hideLabel setting. | |
| 255 | + * Set the page ID for multi-page forms. | |
| 226 | 256 | * |
| 227 | - * @param bool $hideLabel The hideLabel setting to set. | |
| 257 | + * @param string|null $pageId The page ID to set. | |
| 228 | 258 | */ |
| 229 | - public function setHideLabel(bool $hideLabel): void | |
| 259 | + public function setPageId(?string $pageId): void | |
| 230 | 260 | { |
| 231 | - $this->formField->setHideLabel($hideLabel); | |
| 261 | + $this->formField->pageId = $pageId; | |
| 232 | 262 | } |
| 233 | 263 | |
| 234 | 264 | /** |
| 235 | - * Check if the field is read-only. | |
| 265 | + * Set the number of rows (for textarea fields). | |
| 236 | 266 | * |
| 237 | - * @return bool True if the field is read-only, false otherwise. | |
| 267 | + * @param int $rows | |
| 238 | 268 | */ |
| 239 | - public function isReadOnly(): bool | |
| 269 | + public function setRows(int $rows): void | |
| 240 | 270 | { |
| 241 | - return $this->formField->isReadOnly(); | |
| 271 | + $this->formField->setRows($rows); | |
| 242 | 272 | } |
| 243 | 273 | |
| 244 | 274 | /** |
| 245 | - * Set the readOnly setting. | |
| 246 | - * | |
| 247 | - * @param bool $readOnly The readOnly setting to set. | |
| 275 | + * Get the FieldGeneralSettings entity. | |
| 248 | 276 | */ |
| 249 | - public function setReadOnly(bool $readOnly): void | |
| 277 | + public function getFieldGeneralSettings(): FieldGeneralSettings | |
| 250 | 278 | { |
| 251 | - $this->formField->setReadOnly($readOnly); | |
| 279 | + return $this->fieldGeneralSettings; | |
| 252 | 280 | } |
| 253 | 281 | |
| 254 | 282 | /** |
| 255 | - * Get the field description. | |
| 256 | - * | |
| 257 | - * @return string The description of the form field. | |
| 283 | + * Get the FieldAdvancedSettings entity. | |
| 258 | 284 | */ |
| 259 | - public function getDescription(): string | |
| 285 | + public function getFieldAdvancedSettings(): FieldAdvancedSettings | |
| 260 | 286 | { |
| 261 | - return $this->formField->getDescription(); | |
| 287 | + return $this->fieldAdvancedSettings; | |
| 262 | 288 | } |
| 263 | 289 | |
| 264 | 290 | /** |
| 265 | - * Set the description setting. | |
| 291 | + * Set an additional property | |
| 266 | 292 | * |
| 267 | - * @param string $description The description setting to set. | |
| 268 | - */ | |
| 269 | - public function setDescription(string $description): void | |
| 270 | - { | |
| 271 | - $this->formField->setDescription($description); | |
| 272 | - } | |
| 273 | - | |
| 274 | - /** | |
| 275 | - * Get the field required message. | |
| 293 | + * @param string $key | |
| 294 | + * @param mixed $value | |
| 276 | 295 | * |
| 277 | - * @return string The required message of the form field. | |
| 296 | + * @return void | |
| 278 | 297 | */ |
| 279 | - public function getRequiredMessage(): string | |
| 298 | + public function setAdditionalProperty(string $key, $value): void | |
| 280 | 299 | { |
| 281 | - return $this->formField->getRequiredMessage(); | |
| 282 | - } | |
| 300 | + foreach ($this->additionalProperties as &$property) { | |
| 301 | + if (array_key_exists($key, $property)) { | |
| 302 | + $property[$key] = $value; // update existing | |
| 303 | + return; | |
| 304 | + } | |
| 305 | + } | |
| 283 | 306 | |
| 284 | - /** | |
| 285 | - * Set the required message setting. | |
| 286 | - * | |
| 287 | - * @param string $requiredMessage The required message setting to set. | |
| 288 | - */ | |
| 289 | - public function setRequiredMessage(string $requiredMessage): void | |
| 290 | - { | |
| 291 | - $this->formField->setRequiredMessage($requiredMessage); | |
| 307 | + // if not found, append new key-value pair | |
| 308 | + $this->additionalProperties[] = [$key => $value]; | |
| 292 | 309 | } |
| 293 | 310 | |
| 294 | 311 | /** |
| 295 | - * Get the field CSS classes. | |
| 312 | + * Get an additional property | |
| 296 | 313 | * |
| 297 | - * @return string The CSS classes of the form field. | |
| 314 | + * @param string $key | |
| 315 | + * @return mixed|null The value of the additional property, or null if not found | |
| 298 | 316 | */ |
| 299 | - public function getCssClasses(): string | |
| 317 | + public function getAdditionalProperty(string $key) | |
| 300 | 318 | { |
| 301 | - return $this->formField->getCssClasses(); | |
| 319 | + foreach ($this->additionalProperties as $property) { | |
| 320 | + if (array_key_exists($key, $property)) { | |
| 321 | + return $property[$key]; | |
| 322 | + } | |
| 323 | + } | |
| 324 | + return null; | |
| 302 | 325 | } |
| 303 | 326 | |
| 304 | 327 | /** |
| 305 | - * Set the CSS classes setting. | |
| 328 | + * Get all additional properties | |
| 306 | 329 | * |
| 307 | - * @param string $cssClasses The CSS classes setting to set. | |
| 330 | + * @return array<string, mixed> The additional properties | |
| 308 | 331 | */ |
| 309 | - public function setCssClasses(string $cssClasses): void | |
| 332 | + public function getAllAdditionalProperties(): array | |
| 310 | 333 | { |
| 311 | - $this->formField->setCssClasses($cssClasses); | |
| 334 | + return $this->additionalProperties; | |
| 312 | 335 | } |
| 313 | 336 | |
| 314 | 337 | /** |
| 315 | 338 | * Convert the form field entity to an array. |
| @@ -317,22 +340,37 @@ | ||
| 317 | 340 | * @return array<string, mixed> The form field entity as an array. |
| 318 | 341 | */ |
| 319 | 342 | public function toArray(): array |
| 320 | 343 | { |
| 321 | - return [ | |
| 322 | - 'id' => $this->getId(), | |
| 323 | - 'formId' => $this->getFormId(), | |
| 324 | - 'fieldIndex' => $this->getIndex(), | |
| 325 | - 'type' => $this->getType(), | |
| 326 | - 'label' => $this->getLabel(), | |
| 327 | - 'defaultValue' => $this->getDefaultValue(), | |
| 328 | - 'required' => $this->isRequired(), | |
| 329 | - 'placeholder' => $this->getPlaceholder(), | |
| 330 | - 'position' => $this->getPosition(), | |
| 331 | - 'hideLabel' => $this->isLabelHidden(), | |
| 332 | - 'readOnly' => $this->isReadOnly(), | |
| 333 | - 'description' => $this->getDescription(), | |
| 334 | - 'requiredMessage' => $this->getRequiredMessage(), | |
| 335 | - 'cssClasses' => $this->getCssClasses(), | |
| 336 | - ]; | |
| 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); | |
| 337 | 375 | } |
| 338 | 376 | } |