← All changes
|
backend/src/ValueObjects/Confirmation/Confirmation.php
+322
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,322 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\ValueObjects\Confirmation; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 6 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 7 | +use IvyForms\Services\Translations\BackendStrings; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Class Confirmation | |
| 11 | + * | |
| 12 | + * @package IvyForms\ValueObjects\Confirmation | |
| 13 | + * | |
| 14 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) | |
| 15 | + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) | |
| 16 | + */ | |
| 17 | +final class Confirmation | |
| 18 | +{ | |
| 19 | + /** | |
| 20 | + * @var int | |
| 21 | + */ | |
| 22 | + public int $id; | |
| 23 | + /** | |
| 24 | + * @var int | |
| 25 | + */ | |
| 26 | + public int $formId; | |
| 27 | + /** | |
| 28 | + * @var string | |
| 29 | + */ | |
| 30 | + public string $name; | |
| 31 | + /** | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + public string $type; | |
| 35 | + /** | |
| 36 | + * @var bool | |
| 37 | + */ | |
| 38 | + public bool $enabled; | |
| 39 | + /** | |
| 40 | + * @var bool | |
| 41 | + */ | |
| 42 | + public bool $showForm; | |
| 43 | + /** | |
| 44 | + * @var string | |
| 45 | + */ | |
| 46 | + public string $message; | |
| 47 | + /** | |
| 48 | + * @var string | |
| 49 | + */ | |
| 50 | + public string $url; | |
| 51 | + /** | |
| 52 | + * @var string | |
| 53 | + */ | |
| 54 | + public string $page; | |
| 55 | + /** | |
| 56 | + * @var int | |
| 57 | + */ | |
| 58 | + public int $position; | |
| 59 | + /** | |
| 60 | + * @var bool | |
| 61 | + */ | |
| 62 | + public bool $isDefault; | |
| 63 | + /** | |
| 64 | + * @var array<string, mixed> | |
| 65 | + */ | |
| 66 | + public array $smartLogic; | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Confirmation constructor. | |
| 70 | + * | |
| 71 | + * @param int $id | |
| 72 | + * @param int $formId | |
| 73 | + * @param string $name | |
| 74 | + * @param string $type | |
| 75 | + * @param bool $enabled | |
| 76 | + * @param bool $showForm | |
| 77 | + * @param string $message | |
| 78 | + * @param string $url | |
| 79 | + * @param string $page | |
| 80 | + * @param int $position | |
| 81 | + * @param bool $isDefault | |
| 82 | + * @param array<string, mixed> $smartLogic | |
| 83 | + * @throws InvalidArgumentException|ValidationException | |
| 84 | + */ | |
| 85 | + public function __construct( | |
| 86 | + int $id, | |
| 87 | + int $formId, | |
| 88 | + string $name, | |
| 89 | + string $type, | |
| 90 | + bool $enabled, | |
| 91 | + bool $showForm, | |
| 92 | + string $message, | |
| 93 | + string $url, | |
| 94 | + string $page, | |
| 95 | + int $position = 0, | |
| 96 | + bool $isDefault = false, | |
| 97 | + array $smartLogic = [] | |
| 98 | + ) { | |
| 99 | + $this->id = $this->validateId($id); | |
| 100 | + $this->formId = $this->validateId($formId); | |
| 101 | + $this->name = $this->validateString($name, 255, 'name'); | |
| 102 | + $this->type = $this->validateString($type, 255, 'type'); | |
| 103 | + $this->enabled = $this->validateBool($enabled); | |
| 104 | + $this->showForm = $this->validateBool($showForm); | |
| 105 | + $this->message = $this->validateString($message, 4294967295, 'message'); | |
| 106 | + $this->url = $this->validateString($url, 255, 'url'); | |
| 107 | + $this->page = $this->validateString($page, 255, 'page'); | |
| 108 | + $this->position = $position >= 0 ? $position : 0; | |
| 109 | + $this->isDefault = $this->validateBool($isDefault); | |
| 110 | + $this->smartLogic = $this->validateSmartLogic($smartLogic); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Validates the ID. | |
| 115 | + * | |
| 116 | + * @param int $id | |
| 117 | + * | |
| 118 | + * @return int | |
| 119 | + * @throws InvalidArgumentException | |
| 120 | + */ | |
| 121 | + private function validateId(int $id): int | |
| 122 | + { | |
| 123 | + if ($id < 0) { | |
| 124 | + throw new InvalidArgumentException(BackendStrings::getExceptionStrings()['id_positive_integer']); | |
| 125 | + } | |
| 126 | + return $id; | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * Validates a string value. | |
| 131 | + * | |
| 132 | + * @param string $value | |
| 133 | + * @param int $maxLength | |
| 134 | + * @param string $fieldName | |
| 135 | + * | |
| 136 | + * @return string | |
| 137 | + * | |
| 138 | + * @throws ValidationException | |
| 139 | + */ | |
| 140 | + private function validateString(string $value, int $maxLength, string $fieldName): string | |
| 141 | + { | |
| 142 | + if (strlen($value) > $maxLength) { | |
| 143 | + throw new ValidationException( | |
| 144 | + sprintf( | |
| 145 | + /* translators: 1: String value, 2: String max length. */ | |
| 146 | + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), | |
| 147 | + esc_html($fieldName), | |
| 148 | + $maxLength | |
| 149 | + ) | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + return $value; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Validates a boolean field. | |
| 157 | + * | |
| 158 | + * @param bool $value | |
| 159 | + * | |
| 160 | + * @return bool | |
| 161 | + */ | |
| 162 | + private function validateBool(bool $value): bool | |
| 163 | + { | |
| 164 | + return $value; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Get the confirmation ID. | |
| 169 | + * | |
| 170 | + * @return int | |
| 171 | + */ | |
| 172 | + public function getId(): int | |
| 173 | + { | |
| 174 | + return $this->id; | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 178 | + * Get the confirmation name. | |
| 179 | + * | |
| 180 | + * @return string | |
| 181 | + */ | |
| 182 | + public function getName(): string | |
| 183 | + { | |
| 184 | + return $this->name; | |
| 185 | + } | |
| 186 | + | |
| 187 | + /** | |
| 188 | + * Get the form ID. | |
| 189 | + * | |
| 190 | + * @return int | |
| 191 | + */ | |
| 192 | + public function getFormId(): int | |
| 193 | + { | |
| 194 | + return $this->formId; | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * Get the confirmation type. | |
| 199 | + * | |
| 200 | + * @return string | |
| 201 | + */ | |
| 202 | + public function getType(): string | |
| 203 | + { | |
| 204 | + return $this->type; | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Get the confirmation status. | |
| 209 | + * | |
| 210 | + * @return bool | |
| 211 | + */ | |
| 212 | + public function isEnabled(): bool | |
| 213 | + { | |
| 214 | + return $this->enabled; | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Get the show form flag. | |
| 219 | + * | |
| 220 | + * @return bool | |
| 221 | + */ | |
| 222 | + public function isShowForm(): bool | |
| 223 | + { | |
| 224 | + return $this->showForm; | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Get the confirmation message. | |
| 229 | + * | |
| 230 | + * @return string | |
| 231 | + */ | |
| 232 | + public function getMessage(): string | |
| 233 | + { | |
| 234 | + return $this->message; | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Get the confirmation URL. | |
| 239 | + * | |
| 240 | + * @return string | |
| 241 | + */ | |
| 242 | + public function getUrl(): string | |
| 243 | + { | |
| 244 | + return $this->url; | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Get the confirmation page. | |
| 249 | + * | |
| 250 | + * @return string | |
| 251 | + */ | |
| 252 | + public function getPage(): string | |
| 253 | + { | |
| 254 | + return $this->page; | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Get the confirmation list position. | |
| 259 | + * | |
| 260 | + * @return int | |
| 261 | + */ | |
| 262 | + public function getPosition(): int | |
| 263 | + { | |
| 264 | + return $this->position; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Whether this confirmation is the default for the form. | |
| 269 | + * | |
| 270 | + * @return bool | |
| 271 | + */ | |
| 272 | + public function isDefault(): bool | |
| 273 | + { | |
| 274 | + return $this->isDefault; | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * @return array<string, mixed> | |
| 279 | + */ | |
| 280 | + public function getSmartLogic(): array | |
| 281 | + { | |
| 282 | + return $this->smartLogic; | |
| 283 | + } | |
| 284 | + | |
| 285 | + /** | |
| 286 | + * @param array<string, mixed> $smartLogic | |
| 287 | + * @return array<string, mixed> | |
| 288 | + */ | |
| 289 | + private function validateSmartLogic(array $smartLogic): array | |
| 290 | + { | |
| 291 | + $match = $smartLogic['match'] ?? 'any'; | |
| 292 | + | |
| 293 | + return [ | |
| 294 | + 'enabled' => (bool)($smartLogic['enabled'] ?? false), | |
| 295 | + 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any', | |
| 296 | + 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [], | |
| 297 | + ]; | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Convert the confirmation to an array. | |
| 302 | + * | |
| 303 | + * @return array<string, mixed> | |
| 304 | + */ | |
| 305 | + public function toArray(): array | |
| 306 | + { | |
| 307 | + return [ | |
| 308 | + 'id' => $this->getId(), | |
| 309 | + 'formId' => $this->getFormId(), | |
| 310 | + 'name' => $this->getName(), | |
| 311 | + 'type' => $this->getType(), | |
| 312 | + 'enabled' => $this->isEnabled(), | |
| 313 | + 'showForm' => $this->isShowForm(), | |
| 314 | + 'message' => $this->getMessage(), | |
| 315 | + 'url' => $this->getUrl(), | |
| 316 | + 'page' => $this->getPage(), | |
| 317 | + 'position' => $this->getPosition(), | |
| 318 | + 'isDefault' => $this->isDefault(), | |
| 319 | + 'smartLogic' => $this->getSmartLogic(), | |
| 320 | + ]; | |
| 321 | + } | |
| 322 | +} | |