| @@ -1,21 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\ValueObjects\Form; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\ValidationException; |
| 6 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 7 | +use IvyForms\Services\Template\TemplateDefaults; | |
| 11 | 8 | use IvyForms\Services\Translations\BackendStrings; |
| 12 | -use IvyForms\ValueObjects\Form\IntegrationSettings; | |
| 13 | 9 | |
| 14 | 10 | /** |
| 15 | - * Class Form | |
| 11 | + * Class Form - Core form value object with composed metadata and settings | |
| 16 | 12 | * |
| 17 | 13 | * @package IvyForms\ValueObjects\Form |
| 14 | + * @SuppressWarnings(PHPMD.TooManyFields) | |
| 18 | 15 | */ |
| 19 | 16 | final class Form |
| 20 | 17 | { |
| 21 | 18 | /** |
| @@ -21,56 +18,70 @@ | ||
| 21 | 18 | /** |
| 22 | 19 | * @var int |
| 23 | 20 | */ |
| 24 | 21 | public int $id; |
| 22 | + | |
| 25 | 23 | /** |
| 26 | 24 | * @var string |
| 27 | 25 | */ |
| 28 | 26 | public string $name; |
| 27 | + | |
| 29 | 28 | /** |
| 30 | 29 | * @var string |
| 31 | 30 | */ |
| 32 | - public string $author; | |
| 31 | + public string $description; | |
| 32 | + | |
| 33 | 33 | /** |
| 34 | - * @var bool | |
| 34 | + * @var FormMetadata | |
| 35 | 35 | */ |
| 36 | - public bool $published; | |
| 36 | + public FormMetadata $metadata; | |
| 37 | + | |
| 37 | 38 | /** |
| 38 | - * @var bool | |
| 39 | + * @var FormSettings | |
| 39 | 40 | */ |
| 40 | - public bool $starred; | |
| 41 | + public FormSettings $settings; | |
| 42 | + | |
| 41 | 43 | /** |
| 42 | - * @var string | |
| 44 | + * @var array<int, array<string, mixed>>|null | |
| 43 | 45 | */ |
| 44 | - public string $dateCreated; | |
| 46 | + public ?array $fields; | |
| 47 | + | |
| 45 | 48 | /** |
| 46 | - * @var string | |
| 49 | + * @var IntegrationSettings|null | |
| 47 | 50 | */ |
| 48 | - public string $dateEdited; | |
| 51 | + public ?IntegrationSettings $integrationSettings; | |
| 49 | 52 | /** |
| 50 | - * @var array<int, array<string, mixed>>|null | |
| 53 | + * @var StyleSettings|null | |
| 51 | 54 | */ |
| 52 | - public ?array $fields; | |
| 55 | + public ?StyleSettings $styleSettings; | |
| 56 | + | |
| 53 | 57 | /** |
| 54 | - * @var string | |
| 58 | + * @var array<int, array<string, mixed>>|null | |
| 55 | 59 | */ |
| 56 | - public string $description; | |
| 60 | + public ?array $pages; | |
| 61 | + | |
| 57 | 62 | /** |
| 58 | - * @var bool | |
| 63 | + * @var array<string, mixed>|null | |
| 59 | 64 | */ |
| 60 | - public bool $showTitle; | |
| 65 | + public ?array $progressIndicator; | |
| 66 | + | |
| 61 | 67 | /** |
| 62 | - * @var bool | |
| 68 | + * @var array<string, mixed> | |
| 63 | 69 | */ |
| 64 | - public bool $showDescription; | |
| 70 | + public array $formActionButtons; | |
| 71 | + | |
| 65 | 72 | /** |
| 66 | - * @var bool | |
| 73 | + * Lowercase presentation type (classic, conversational, or plugin-defined via | |
| 74 | + * ivyforms/form/allowed_form_types). Always matches Sanitizer::sanitizeFormType(). | |
| 75 | + * | |
| 76 | + * @var string | |
| 67 | 77 | */ |
| 68 | - public bool $storeEntries; | |
| 78 | + public string $formType; | |
| 79 | + | |
| 69 | 80 | /** |
| 70 | - * @var IntegrationSettings|null | |
| 81 | + * @var PaymentSettings | |
| 71 | 82 | */ |
| 72 | - public ?IntegrationSettings $integrationSettings; | |
| 83 | + public PaymentSettings $paymentSettings; | |
| 73 | 84 | |
| 74 | 85 | /** |
| 75 | 86 | * Form constructor. |
| 76 | 87 | * |
| @@ -75,49 +86,70 @@ | ||
| 75 | 86 | * Form constructor. |
| 76 | 87 | * |
| 77 | 88 | * @param int $id |
| 78 | 89 | * @param string $name |
| 79 | - * @param string $author | |
| 80 | - * @param bool $starred | |
| 81 | - * @param bool $published | |
| 82 | 90 | * @param string $description |
| 83 | - * @param bool $showTitle | |
| 84 | - * @param bool $showDescription | |
| 85 | - * @param bool $storeEntries | |
| 91 | + * @param FormMetadata $metadata | |
| 92 | + * @param FormSettings $settings | |
| 86 | 93 | * @param array<int, array<string, mixed>> $fields |
| 87 | 94 | * @param IntegrationSettings|null $integrationSettings |
| 95 | + * @param array<int, array<string, mixed>>|null $pages | |
| 96 | + * @param array<string, mixed>|null $progressIndicator | |
| 97 | + * @param StyleSettings|null $styleSettings | |
| 98 | + * @param array<string, mixed>|null $formActionButtons | |
| 99 | + * @param string $formType presentation type (sanitized to the allowed allowlist) | |
| 100 | + * @param PaymentSettings|array<string, mixed>|null $paymentSettings | |
| 101 | + * @SuppressWarnings("ExcessiveParameterList") | |
| 88 | 102 | * @throws ValidationException |
| 89 | - * @SuppressWarnings("ExcessiveParameterList") | |
| 90 | 103 | */ |
| 91 | 104 | public function __construct( |
| 92 | 105 | int $id, |
| 93 | 106 | string $name, |
| 94 | - string $author, | |
| 95 | - bool $starred, | |
| 96 | - bool $published, | |
| 97 | 107 | string $description, |
| 98 | - bool $showTitle, | |
| 99 | - bool $showDescription, | |
| 100 | - bool $storeEntries, | |
| 108 | + FormMetadata $metadata, | |
| 109 | + FormSettings $settings, | |
| 101 | 110 | array $fields = [], |
| 102 | - IntegrationSettings $integrationSettings = null | |
| 111 | + ?IntegrationSettings $integrationSettings = null, | |
| 112 | + ?array $pages = null, | |
| 113 | + ?array $progressIndicator = null, | |
| 114 | + ?StyleSettings $styleSettings = null, | |
| 115 | + ?array $formActionButtons = null, | |
| 116 | + string $formType = Sanitizer::FORM_TYPE_CLASSIC, | |
| 117 | + $paymentSettings = null | |
| 103 | 118 | ) { |
| 104 | - $this->id = $this->validateId($id); | |
| 105 | - $this->name = $this->validateString($name, 255, 'name'); | |
| 106 | - $this->author = $this->validateString($author, 1000, 'author'); | |
| 107 | - $this->starred = $this->validateBool($starred); | |
| 108 | - $this->published = $this->validateBool($published); | |
| 109 | - $this->dateCreated = gmdate('Y-m-d H:i:s'); | |
| 110 | - $this->dateEdited = gmdate('Y-m-d H:i:s'); | |
| 111 | - $this->fields = $fields; | |
| 112 | - $this->description = $this->validateString($description, 1000, 'description'); | |
| 113 | - $this->showTitle = $this->validateBool($showTitle); | |
| 114 | - $this->showDescription = $this->validateBool($showDescription); | |
| 115 | - $this->storeEntries = $this->validateBool($storeEntries); | |
| 116 | - $this->integrationSettings = $integrationSettings; | |
| 119 | + $this->id = $this->validateId($id); | |
| 120 | + $this->name = $this->validateString($name, 255, 'name'); | |
| 121 | + $this->description = $this->validateString($description, 1000, 'description'); | |
| 122 | + $this->metadata = $metadata; | |
| 123 | + $this->settings = $settings; | |
| 124 | + $this->fields = $fields; | |
| 125 | + $this->integrationSettings = $integrationSettings; | |
| 126 | + $this->pages = $pages; | |
| 127 | + $this->progressIndicator = $progressIndicator; | |
| 128 | + $this->formActionButtons = $formActionButtons ?? [ | |
| 129 | + 'submitButtonSettings' => [ | |
| 130 | + 'label' => BackendStrings::getCommonStrings()['submit'], | |
| 131 | + 'position' => 'default', | |
| 132 | + ], | |
| 133 | + ]; | |
| 134 | + $this->styleSettings = $styleSettings; | |
| 135 | + $this->formType = $this->normalizeStoredFormType($formType); | |
| 136 | + | |
| 137 | + $this->paymentSettings = $paymentSettings instanceof PaymentSettings | |
| 138 | + ? $paymentSettings | |
| 139 | + : new PaymentSettings(is_array($paymentSettings) ? $paymentSettings : null); | |
| 117 | 140 | } |
| 118 | 141 | |
| 119 | 142 | /** |
| 143 | + * @param string $formType | |
| 144 | + * @return string | |
| 145 | + */ | |
| 146 | + private function normalizeStoredFormType(string $formType): string | |
| 147 | + { | |
| 148 | + return Sanitizer::sanitizeFormType($formType); | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 120 | 152 | * Validates the ID. |
| 121 | 153 | * |
| 122 | 154 | * @param int $id |
| 123 | 155 | * |
| @@ -157,19 +189,8 @@ | ||
| 157 | 189 | } |
| 158 | 190 | return $value; |
| 159 | 191 | } |
| 160 | 192 | |
| 161 | - /** | |
| 162 | - * Validates a boolean field. | |
| 163 | - * | |
| 164 | - * @param bool $value | |
| 165 | - * | |
| 166 | - * @return bool | |
| 167 | - */ | |
| 168 | - private function validateBool(bool $value): bool | |
| 169 | - { | |
| 170 | - return $value; | |
| 171 | - } | |
| 172 | 193 | |
| 173 | 194 | /** |
| 174 | 195 | * Get the form ID. |
| 175 | 196 | * |
| @@ -190,54 +211,34 @@ | ||
| 190 | 211 | return $this->name; |
| 191 | 212 | } |
| 192 | 213 | |
| 193 | 214 | /** |
| 194 | - * Get the form author. | |
| 195 | - * | |
| 196 | 215 | * @return string |
| 197 | 216 | */ |
| 198 | - public function getAuthor(): string | |
| 217 | + public function getFormType(): string | |
| 199 | 218 | { |
| 200 | - return $this->author; | |
| 219 | + return $this->formType; | |
| 201 | 220 | } |
| 202 | 221 | |
| 203 | 222 | /** |
| 204 | - * Get the form starred status. | |
| 205 | - * | |
| 206 | - * @return bool | |
| 223 | + * @param string $formType | |
| 207 | 224 | */ |
| 208 | - public function isStarred(): bool | |
| 225 | + public function setFormType(string $formType): void | |
| 209 | 226 | { |
| 210 | - return $this->starred; | |
| 227 | + $this->formType = $this->normalizeStoredFormType($formType); | |
| 211 | 228 | } |
| 229 | + | |
| 212 | 230 | /** |
| 213 | - * Get the form published status. | |
| 231 | + * Get the form description. | |
| 214 | 232 | * |
| 215 | - * @return bool | |
| 216 | - */ | |
| 217 | - public function isPublished(): bool | |
| 218 | - { | |
| 219 | - return $this->published; | |
| 220 | - } | |
| 221 | - /** | |
| 222 | - * Get the date of form creation. | |
| 223 | - * | |
| 224 | 233 | * @return string |
| 225 | 234 | */ |
| 226 | - public function getDateCreated(): string | |
| 235 | + public function getDescription(): string | |
| 227 | 236 | { |
| 228 | - return $this->dateCreated; | |
| 237 | + return $this->description; | |
| 229 | 238 | } |
| 239 | + | |
| 230 | 240 | /** |
| 231 | - * Get the form date edited. | |
| 232 | - * | |
| 233 | - * @return string | |
| 234 | - */ | |
| 235 | - public function getDateEdited(): string | |
| 236 | - { | |
| 237 | - return $this->dateEdited; | |
| 238 | - } | |
| 239 | - /** | |
| 240 | 241 | * Get the form fields. |
| 241 | 242 | * |
| 242 | 243 | * @return array<int, array<string, mixed>>|null |
| 243 | 244 | */ |
| @@ -244,61 +245,55 @@ | ||
| 244 | 245 | public function getFields(): ?array |
| 245 | 246 | { |
| 246 | 247 | return $this->fields; |
| 247 | 248 | } |
| 249 | + | |
| 248 | 250 | /** |
| 249 | - * Get the form description. | |
| 250 | - * | |
| 251 | - * @return string | |
| 251 | + * Get the integration settings. | |
| 252 | + * @return IntegrationSettings|null | |
| 252 | 253 | */ |
| 253 | - public function getDescription(): string | |
| 254 | + public function getIntegrationSettings(): ?IntegrationSettings | |
| 254 | 255 | { |
| 255 | - return $this->description; | |
| 256 | + return $this->integrationSettings; | |
| 256 | 257 | } |
| 258 | + | |
| 257 | 259 | /** |
| 258 | - * Get the show description of the form. | |
| 259 | - * | |
| 260 | - * @return bool The form show description status. | |
| 260 | + * Set integration settings. | |
| 261 | + * @param IntegrationSettings $settings | |
| 261 | 262 | */ |
| 262 | - public function isDescriptionVisible(): bool | |
| 263 | + public function setIntegrationSettings(IntegrationSettings $settings): void | |
| 263 | 264 | { |
| 264 | - return $this->showDescription; | |
| 265 | + $this->integrationSettings = $settings; | |
| 265 | 266 | } |
| 266 | - /** | |
| 267 | - * Get the form store entries' status. | |
| 268 | - * @return bool | |
| 269 | - */ | |
| 270 | - public function isStoreEntries(): bool | |
| 267 | + | |
| 268 | + public function getPaymentSettings(): PaymentSettings | |
| 271 | 269 | { |
| 272 | - return $this->storeEntries; | |
| 270 | + return $this->paymentSettings; | |
| 273 | 271 | } |
| 274 | 272 | |
| 275 | - /** | |
| 276 | - * Get integration settings. | |
| 277 | - * @return IntegrationSettings|null | |
| 278 | - */ | |
| 279 | - public function getIntegrationSettings(): ?IntegrationSettings | |
| 273 | + public function setPaymentSettings(PaymentSettings $paymentSettings): void | |
| 280 | 274 | { |
| 281 | - return $this->integrationSettings; | |
| 275 | + $this->paymentSettings = $paymentSettings; | |
| 282 | 276 | } |
| 283 | 277 | |
| 284 | 278 | /** |
| 285 | - * Set integration settings. | |
| 286 | - * @param IntegrationSettings $settings | |
| 279 | + * Get style settings value object. | |
| 280 | + * | |
| 281 | + * @return StyleSettings|null | |
| 287 | 282 | */ |
| 288 | - public function setIntegrationSettings(IntegrationSettings $settings): void | |
| 283 | + public function getStyleSettings(): ?StyleSettings | |
| 289 | 284 | { |
| 290 | - $this->integrationSettings = $settings; | |
| 285 | + return $this->styleSettings; | |
| 291 | 286 | } |
| 292 | 287 | |
| 293 | 288 | /** |
| 294 | - * Get the show title of the form. | |
| 289 | + * Set style settings. | |
| 295 | 290 | * |
| 296 | - * @return bool The form show title status. | |
| 291 | + * @param StyleSettings|null $settings Style settings or null. | |
| 297 | 292 | */ |
| 298 | - public function isTitleVisible(): bool | |
| 293 | + public function setStyleSettings(?StyleSettings $settings): void | |
| 299 | 294 | { |
| 300 | - return $this->showTitle; | |
| 295 | + $this->styleSettings = $settings; | |
| 301 | 296 | } |
| 302 | 297 | |
| 303 | 298 | /** |
| 304 | 299 | * Convert the form to an array. |
| @@ -306,24 +301,31 @@ | ||
| 306 | 301 | * @return array<string, mixed> |
| 307 | 302 | */ |
| 308 | 303 | public function toArray(): array |
| 309 | 304 | { |
| 310 | - $data = array_merge( | |
| 305 | + $integrationPayload = $this->integrationSettings !== null | |
| 306 | + ? $this->integrationSettings->toArray() | |
| 307 | + : ['integrationSettings' => TemplateDefaults::getDefaultIntegrationSettings()]; | |
| 308 | + | |
| 309 | + $stylePayload = $this->styleSettings !== null | |
| 310 | + ? $this->styleSettings->toArray() | |
| 311 | + : ['styleSettings' => TemplateDefaults::getDefaultFormStylesSafe()]; | |
| 312 | + | |
| 313 | + return array_merge( | |
| 311 | 314 | [ |
| 312 | - 'id' => $this->getId(), | |
| 313 | - 'name' => $this->getName(), | |
| 314 | - 'author' => $this->getAuthor(), | |
| 315 | - 'starred' => $this->isStarred(), | |
| 316 | - 'published' => $this->isPublished(), | |
| 317 | - 'dateCreated' => $this->getDateCreated(), | |
| 318 | - 'dateEdited' => $this->getDateEdited(), | |
| 319 | - 'fields' => $this->getFields(), | |
| 320 | - 'description' => $this->getDescription(), | |
| 321 | - 'showTitle' => $this->isTitleVisible(), | |
| 322 | - 'showDescription' => $this->isDescriptionVisible(), | |
| 323 | - 'storeEntries' => $this->isStoreEntries(), | |
| 315 | + 'id' => $this->getId(), | |
| 316 | + 'name' => $this->getName(), | |
| 317 | + 'description' => $this->getDescription(), | |
| 318 | + 'formType' => $this->getFormType(), | |
| 319 | + 'fields' => $this->getFields(), | |
| 320 | + 'pages' => $this->pages, | |
| 321 | + 'progressIndicator' => $this->progressIndicator, | |
| 322 | + 'formActionButtons' => $this->formActionButtons, | |
| 324 | 323 | ], |
| 325 | - $this->getIntegrationSettings()->toArray() | |
| 324 | + $this->metadata->toArray(), | |
| 325 | + $this->settings->toArray(), | |
| 326 | + $integrationPayload, | |
| 327 | + $stylePayload, | |
| 328 | + $this->getPaymentSettings()->toArray() | |
| 326 | 329 | ); |
| 327 | - return $data; | |
| 328 | 330 | } |
| 329 | 331 | } |