| @@ -1,0 +1,331 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\ValueObjects\Form; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 6 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 7 | +use IvyForms\Services\Template\TemplateDefaults; | |
| 8 | +use IvyForms\Services\Translations\BackendStrings; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Class Form - Core form value object with composed metadata and settings | |
| 12 | + * | |
| 13 | + * @package IvyForms\ValueObjects\Form | |
| 14 | + * @SuppressWarnings(PHPMD.TooManyFields) | |
| 15 | + */ | |
| 16 | +final class Form | |
| 17 | +{ | |
| 18 | + /** | |
| 19 | + * @var int | |
| 20 | + */ | |
| 21 | + public int $id; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + public string $name; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + public string $description; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @var FormMetadata | |
| 35 | + */ | |
| 36 | + public FormMetadata $metadata; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var FormSettings | |
| 40 | + */ | |
| 41 | + public FormSettings $settings; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @var array<int, array<string, mixed>>|null | |
| 45 | + */ | |
| 46 | + public ?array $fields; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @var IntegrationSettings|null | |
| 50 | + */ | |
| 51 | + public ?IntegrationSettings $integrationSettings; | |
| 52 | + /** | |
| 53 | + * @var StyleSettings|null | |
| 54 | + */ | |
| 55 | + public ?StyleSettings $styleSettings; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var array<int, array<string, mixed>>|null | |
| 59 | + */ | |
| 60 | + public ?array $pages; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @var array<string, mixed>|null | |
| 64 | + */ | |
| 65 | + public ?array $progressIndicator; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * @var array<string, mixed> | |
| 69 | + */ | |
| 70 | + public array $formActionButtons; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Lowercase presentation type (classic, conversational, or plugin-defined via | |
| 74 | + * ivyforms/form/allowed_form_types). Always matches Sanitizer::sanitizeFormType(). | |
| 75 | + * | |
| 76 | + * @var string | |
| 77 | + */ | |
| 78 | + public string $formType; | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * @var PaymentSettings | |
| 82 | + */ | |
| 83 | + public PaymentSettings $paymentSettings; | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Form constructor. | |
| 87 | + * | |
| 88 | + * @param int $id | |
| 89 | + * @param string $name | |
| 90 | + * @param string $description | |
| 91 | + * @param FormMetadata $metadata | |
| 92 | + * @param FormSettings $settings | |
| 93 | + * @param array<int, array<string, mixed>> $fields | |
| 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") | |
| 102 | + * @throws ValidationException | |
| 103 | + */ | |
| 104 | + public function __construct( | |
| 105 | + int $id, | |
| 106 | + string $name, | |
| 107 | + string $description, | |
| 108 | + FormMetadata $metadata, | |
| 109 | + FormSettings $settings, | |
| 110 | + array $fields = [], | |
| 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 | |
| 118 | + ) { | |
| 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); | |
| 140 | + } | |
| 141 | + | |
| 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 | + /** | |
| 152 | + * Validates the ID. | |
| 153 | + * | |
| 154 | + * @param int $id | |
| 155 | + * | |
| 156 | + * @return int | |
| 157 | + * @throws ValidationException | |
| 158 | + */ | |
| 159 | + private function validateId(int $id): int | |
| 160 | + { | |
| 161 | + if ($id < 0) { | |
| 162 | + throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); | |
| 163 | + } | |
| 164 | + return $id; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Validates a string value. | |
| 169 | + * | |
| 170 | + * @param string $value | |
| 171 | + * @param int $maxLength | |
| 172 | + * @param string $fieldName | |
| 173 | + * | |
| 174 | + * @return string | |
| 175 | + * | |
| 176 | + * @throws ValidationException | |
| 177 | + */ | |
| 178 | + private function validateString(string $value, int $maxLength, string $fieldName): string | |
| 179 | + { | |
| 180 | + if (strlen($value) > $maxLength) { | |
| 181 | + throw new ValidationException( | |
| 182 | + sprintf( | |
| 183 | + /* translators: 1: String value, 2: String max length. */ | |
| 184 | + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), | |
| 185 | + esc_html($fieldName), | |
| 186 | + $maxLength | |
| 187 | + ) | |
| 188 | + ); | |
| 189 | + } | |
| 190 | + return $value; | |
| 191 | + } | |
| 192 | + | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * Get the form ID. | |
| 196 | + * | |
| 197 | + * @return int | |
| 198 | + */ | |
| 199 | + public function getId(): int | |
| 200 | + { | |
| 201 | + return $this->id; | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Get the form name. | |
| 206 | + * | |
| 207 | + * @return string | |
| 208 | + */ | |
| 209 | + public function getName(): string | |
| 210 | + { | |
| 211 | + return $this->name; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * @return string | |
| 216 | + */ | |
| 217 | + public function getFormType(): string | |
| 218 | + { | |
| 219 | + return $this->formType; | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * @param string $formType | |
| 224 | + */ | |
| 225 | + public function setFormType(string $formType): void | |
| 226 | + { | |
| 227 | + $this->formType = $this->normalizeStoredFormType($formType); | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Get the form description. | |
| 232 | + * | |
| 233 | + * @return string | |
| 234 | + */ | |
| 235 | + public function getDescription(): string | |
| 236 | + { | |
| 237 | + return $this->description; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Get the form fields. | |
| 242 | + * | |
| 243 | + * @return array<int, array<string, mixed>>|null | |
| 244 | + */ | |
| 245 | + public function getFields(): ?array | |
| 246 | + { | |
| 247 | + return $this->fields; | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Get the integration settings. | |
| 252 | + * @return IntegrationSettings|null | |
| 253 | + */ | |
| 254 | + public function getIntegrationSettings(): ?IntegrationSettings | |
| 255 | + { | |
| 256 | + return $this->integrationSettings; | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Set integration settings. | |
| 261 | + * @param IntegrationSettings $settings | |
| 262 | + */ | |
| 263 | + public function setIntegrationSettings(IntegrationSettings $settings): void | |
| 264 | + { | |
| 265 | + $this->integrationSettings = $settings; | |
| 266 | + } | |
| 267 | + | |
| 268 | + public function getPaymentSettings(): PaymentSettings | |
| 269 | + { | |
| 270 | + return $this->paymentSettings; | |
| 271 | + } | |
| 272 | + | |
| 273 | + public function setPaymentSettings(PaymentSettings $paymentSettings): void | |
| 274 | + { | |
| 275 | + $this->paymentSettings = $paymentSettings; | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Get style settings value object. | |
| 280 | + * | |
| 281 | + * @return StyleSettings|null | |
| 282 | + */ | |
| 283 | + public function getStyleSettings(): ?StyleSettings | |
| 284 | + { | |
| 285 | + return $this->styleSettings; | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Set style settings. | |
| 290 | + * | |
| 291 | + * @param StyleSettings|null $settings Style settings or null. | |
| 292 | + */ | |
| 293 | + public function setStyleSettings(?StyleSettings $settings): void | |
| 294 | + { | |
| 295 | + $this->styleSettings = $settings; | |
| 296 | + } | |
| 297 | + | |
| 298 | + /** | |
| 299 | + * Convert the form to an array. | |
| 300 | + * | |
| 301 | + * @return array<string, mixed> | |
| 302 | + */ | |
| 303 | + public function toArray(): array | |
| 304 | + { | |
| 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( | |
| 314 | + [ | |
| 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, | |
| 323 | + ], | |
| 324 | + $this->metadata->toArray(), | |
| 325 | + $this->settings->toArray(), | |
| 326 | + $integrationPayload, | |
| 327 | + $stylePayload, | |
| 328 | + $this->getPaymentSettings()->toArray() | |
| 329 | + ); | |
| 330 | + } | |
| 331 | +} | |