| @@ -1,19 +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; | |
| 8 | +use IvyForms\Services\Translations\BackendStrings; | |
| 11 | 9 | |
| 12 | 10 | /** |
| 13 | - * Class Form | |
| 11 | + * Class Form - Core form value object with composed metadata and settings | |
| 14 | 12 | * |
| 15 | 13 | * @package IvyForms\ValueObjects\Form |
| 14 | + * @SuppressWarnings(PHPMD.TooManyFields) | |
| 16 | 15 | */ |
| 17 | 16 | final class Form |
| 18 | 17 | { |
| 19 | 18 | /** |
| @@ -19,89 +18,138 @@ | ||
| 19 | 18 | /** |
| 20 | 19 | * @var int |
| 21 | 20 | */ |
| 22 | 21 | public int $id; |
| 22 | + | |
| 23 | 23 | /** |
| 24 | 24 | * @var string |
| 25 | 25 | */ |
| 26 | 26 | public string $name; |
| 27 | + | |
| 27 | 28 | /** |
| 28 | 29 | * @var string |
| 29 | 30 | */ |
| 30 | - public string $author; | |
| 31 | + public string $description; | |
| 32 | + | |
| 31 | 33 | /** |
| 32 | - * @var bool | |
| 34 | + * @var FormMetadata | |
| 33 | 35 | */ |
| 34 | - public bool $published; | |
| 36 | + public FormMetadata $metadata; | |
| 37 | + | |
| 35 | 38 | /** |
| 36 | - * @var bool | |
| 39 | + * @var FormSettings | |
| 37 | 40 | */ |
| 38 | - public bool $starred; | |
| 41 | + public FormSettings $settings; | |
| 42 | + | |
| 39 | 43 | /** |
| 40 | - * @var string | |
| 44 | + * @var array<int, array<string, mixed>>|null | |
| 41 | 45 | */ |
| 42 | - public string $dateCreated; | |
| 46 | + public ?array $fields; | |
| 47 | + | |
| 43 | 48 | /** |
| 44 | - * @var string | |
| 49 | + * @var IntegrationSettings|null | |
| 45 | 50 | */ |
| 46 | - public string $dateEdited; | |
| 51 | + public ?IntegrationSettings $integrationSettings; | |
| 47 | 52 | /** |
| 48 | - * @var mixed[]|null | |
| 53 | + * @var StyleSettings|null | |
| 49 | 54 | */ |
| 50 | - public ?array $fields; | |
| 55 | + public ?StyleSettings $styleSettings; | |
| 56 | + | |
| 51 | 57 | /** |
| 52 | - * @var string | |
| 58 | + * @var array<int, array<string, mixed>>|null | |
| 53 | 59 | */ |
| 54 | - public string $description; | |
| 60 | + public ?array $pages; | |
| 61 | + | |
| 55 | 62 | /** |
| 56 | - * @var bool | |
| 63 | + * @var array<string, mixed>|null | |
| 57 | 64 | */ |
| 58 | - public bool $showTitle; | |
| 65 | + public ?array $progressIndicator; | |
| 66 | + | |
| 59 | 67 | /** |
| 60 | - * @var bool | |
| 68 | + * @var array<string, mixed> | |
| 61 | 69 | */ |
| 62 | - public bool $showDescription; | |
| 70 | + public array $formActionButtons; | |
| 63 | 71 | |
| 64 | 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 | + /** | |
| 65 | 86 | * Form constructor. |
| 66 | 87 | * |
| 67 | - * @param int $id | |
| 68 | - * @param string $name | |
| 69 | - * @param string $author | |
| 70 | - * @param bool $starred | |
| 71 | - * @param bool $published | |
| 72 | - * @param string $description | |
| 73 | - * @param bool $showTitle | |
| 74 | - * @param bool $showDescription | |
| 75 | - * @param mixed[]|null $fields | |
| 76 | - * | |
| 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") | |
| 77 | 102 | * @throws ValidationException |
| 78 | 103 | */ |
| 79 | 104 | public function __construct( |
| 80 | 105 | int $id, |
| 81 | 106 | string $name, |
| 82 | - string $author, | |
| 83 | - bool $starred, | |
| 84 | - bool $published, | |
| 85 | 107 | string $description, |
| 86 | - bool $showTitle, | |
| 87 | - bool $showDescription, | |
| 88 | - array $fields = null | |
| 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 | |
| 89 | 118 | ) { |
| 90 | - $this->id = $this->validateId($id); | |
| 91 | - $this->name = $this->validateString($name, 255, 'name'); | |
| 92 | - $this->author = $this->validateString($author, 1000, 'author'); | |
| 93 | - $this->starred = $this->validateBool($starred); | |
| 94 | - $this->published = $this->validateBool($published); | |
| 95 | - $this->dateCreated = gmdate('Y-m-d H:i:s'); | |
| 96 | - $this->dateEdited = gmdate('Y-m-d H:i:s'); | |
| 97 | - $this->fields = $fields ?? []; | |
| 98 | - $this->description = $this->validateString($description, 1000, 'description'); | |
| 99 | - $this->showTitle = $this->validateBool($showTitle); | |
| 100 | - $this->showDescription = $this->validateBool($showDescription); | |
| 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); | |
| 101 | 140 | } |
| 102 | 141 | |
| 103 | 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 | + /** | |
| 104 | 152 | * Validates the ID. |
| 105 | 153 | * |
| 106 | 154 | * @param int $id |
| 107 | 155 | * |
| @@ -110,9 +158,9 @@ | ||
| 110 | 158 | */ |
| 111 | 159 | private function validateId(int $id): int |
| 112 | 160 | { |
| 113 | 161 | if ($id < 0) { |
| 114 | - throw new ValidationException("Id must be a positive integer."); | |
| 162 | + throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); | |
| 115 | 163 | } |
| 116 | 164 | return $id; |
| 117 | 165 | } |
| 118 | 166 | |
| @@ -131,9 +179,9 @@ | ||
| 131 | 179 | { |
| 132 | 180 | if (strlen($value) > $maxLength) { |
| 133 | 181 | throw new ValidationException( |
| 134 | 182 | sprintf( |
| 135 | - /* translators: 1: String value, 2: String max length. */ | |
| 183 | + /* translators: 1: String value, 2: String max length. */ | |
| 136 | 184 | esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 137 | 185 | esc_html($fieldName), |
| 138 | 186 | $maxLength |
| 139 | 187 | ) |
| @@ -141,19 +189,8 @@ | ||
| 141 | 189 | } |
| 142 | 190 | return $value; |
| 143 | 191 | } |
| 144 | 192 | |
| 145 | - /** | |
| 146 | - * Validates a boolean field. | |
| 147 | - * | |
| 148 | - * @param bool $value | |
| 149 | - * | |
| 150 | - * @return bool | |
| 151 | - */ | |
| 152 | - private function validateBool(bool $value): bool | |
| 153 | - { | |
| 154 | - return $value; | |
| 155 | - } | |
| 156 | 193 | |
| 157 | 194 | /** |
| 158 | 195 | * Get the form ID. |
| 159 | 196 | * |
| @@ -174,88 +211,89 @@ | ||
| 174 | 211 | return $this->name; |
| 175 | 212 | } |
| 176 | 213 | |
| 177 | 214 | /** |
| 178 | - * Get the form author. | |
| 179 | - * | |
| 180 | 215 | * @return string |
| 181 | 216 | */ |
| 182 | - public function getAuthor(): string | |
| 217 | + public function getFormType(): string | |
| 183 | 218 | { |
| 184 | - return $this->author; | |
| 219 | + return $this->formType; | |
| 185 | 220 | } |
| 186 | 221 | |
| 187 | 222 | /** |
| 188 | - * Get the form starred status. | |
| 189 | - * | |
| 190 | - * @return bool | |
| 223 | + * @param string $formType | |
| 191 | 224 | */ |
| 192 | - public function isStarred(): bool | |
| 225 | + public function setFormType(string $formType): void | |
| 193 | 226 | { |
| 194 | - return $this->starred; | |
| 227 | + $this->formType = $this->normalizeStoredFormType($formType); | |
| 195 | 228 | } |
| 229 | + | |
| 196 | 230 | /** |
| 197 | - * Get the form published status. | |
| 231 | + * Get the form description. | |
| 198 | 232 | * |
| 199 | - * @return bool | |
| 233 | + * @return string | |
| 200 | 234 | */ |
| 201 | - public function isPublished(): bool | |
| 235 | + public function getDescription(): string | |
| 202 | 236 | { |
| 203 | - return $this->published; | |
| 237 | + return $this->description; | |
| 204 | 238 | } |
| 239 | + | |
| 205 | 240 | /** |
| 206 | - * Get the date of form creation. | |
| 241 | + * Get the form fields. | |
| 207 | 242 | * |
| 208 | - * @return string | |
| 243 | + * @return array<int, array<string, mixed>>|null | |
| 209 | 244 | */ |
| 210 | - public function getDateCreated(): string | |
| 245 | + public function getFields(): ?array | |
| 211 | 246 | { |
| 212 | - return $this->dateCreated; | |
| 247 | + return $this->fields; | |
| 213 | 248 | } |
| 249 | + | |
| 214 | 250 | /** |
| 215 | - * Get the form date edited. | |
| 216 | - * | |
| 217 | - * @return string | |
| 251 | + * Get the integration settings. | |
| 252 | + * @return IntegrationSettings|null | |
| 218 | 253 | */ |
| 219 | - public function getDateEdited(): string | |
| 254 | + public function getIntegrationSettings(): ?IntegrationSettings | |
| 220 | 255 | { |
| 221 | - return $this->dateEdited; | |
| 256 | + return $this->integrationSettings; | |
| 222 | 257 | } |
| 258 | + | |
| 223 | 259 | /** |
| 224 | - * Get the form fields. | |
| 225 | - * | |
| 226 | - * @return mixed[]|null | |
| 260 | + * Set integration settings. | |
| 261 | + * @param IntegrationSettings $settings | |
| 227 | 262 | */ |
| 228 | - public function getFields(): ?array | |
| 263 | + public function setIntegrationSettings(IntegrationSettings $settings): void | |
| 229 | 264 | { |
| 230 | - return $this->fields; | |
| 265 | + $this->integrationSettings = $settings; | |
| 231 | 266 | } |
| 232 | - /** | |
| 233 | - * Get the form description. | |
| 234 | - * | |
| 235 | - * @return string | |
| 236 | - */ | |
| 237 | - public function getDescription(): string | |
| 267 | + | |
| 268 | + public function getPaymentSettings(): PaymentSettings | |
| 238 | 269 | { |
| 239 | - return $this->description; | |
| 270 | + return $this->paymentSettings; | |
| 240 | 271 | } |
| 272 | + | |
| 273 | + public function setPaymentSettings(PaymentSettings $paymentSettings): void | |
| 274 | + { | |
| 275 | + $this->paymentSettings = $paymentSettings; | |
| 276 | + } | |
| 277 | + | |
| 241 | 278 | /** |
| 242 | - * Get the show description of the form. | |
| 279 | + * Get style settings value object. | |
| 243 | 280 | * |
| 244 | - * @return bool The form show description status. | |
| 281 | + * @return StyleSettings|null | |
| 245 | 282 | */ |
| 246 | - public function isDescriptionVisible(): bool | |
| 283 | + public function getStyleSettings(): ?StyleSettings | |
| 247 | 284 | { |
| 248 | - return $this->showDescription; | |
| 285 | + return $this->styleSettings; | |
| 249 | 286 | } |
| 287 | + | |
| 250 | 288 | /** |
| 251 | - * Get the show title of the form. | |
| 289 | + * Set style settings. | |
| 252 | 290 | * |
| 253 | - * @return bool The form show title status. | |
| 291 | + * @param StyleSettings|null $settings Style settings or null. | |
| 254 | 292 | */ |
| 255 | - public function isTitleVisible(): bool | |
| 293 | + public function setStyleSettings(?StyleSettings $settings): void | |
| 256 | 294 | { |
| 257 | - return $this->showTitle; | |
| 295 | + $this->styleSettings = $settings; | |
| 258 | 296 | } |
| 259 | 297 | |
| 260 | 298 | /** |
| 261 | 299 | * Convert the form to an array. |
| @@ -263,17 +301,31 @@ | ||
| 263 | 301 | * @return array<string, mixed> |
| 264 | 302 | */ |
| 265 | 303 | public function toArray(): array |
| 266 | 304 | { |
| 267 | - return [ | |
| 268 | - 'id' => $this->getId(), | |
| 269 | - 'name' => $this->getName(), | |
| 270 | - 'author' => $this->getAuthor(), | |
| 271 | - 'starred' => $this->isStarred(), | |
| 272 | - 'published' => $this->isPublished(), | |
| 273 | - 'fields' => $this->getFields(), | |
| 274 | - 'description' => $this->getDescription(), | |
| 275 | - 'showTitle' => $this->isTitleVisible(), | |
| 276 | - 'showDescription' => $this->isDescriptionVisible(), | |
| 277 | - ]; | |
| 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 | + ); | |
| 278 | 330 | } |
| 279 | 331 | } |