← All changes
|
backend/src/ValueObjects/FieldOptions/FieldOptions.php
+179
-0
0.6.1
→
1.5.1
View file →
| @@ -1,0 +1,179 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @copyright © Melograno Venture Studio. All rights. | |
| 5 | + * @licence See LICENCE.md for license details. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace IvyForms\ValueObjects\FieldOptions; | |
| 9 | + | |
| 10 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 11 | +use IvyForms\Services\Translations\BackendStrings; | |
| 12 | + | |
| 13 | +final class FieldOptions | |
| 14 | +{ | |
| 15 | + /** | |
| 16 | + * @var int | |
| 17 | + */ | |
| 18 | + public int $id; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * @var int | |
| 22 | + */ | |
| 23 | + public int $fieldId; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @var string | |
| 27 | + */ | |
| 28 | + public string $label; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @var string | |
| 32 | + */ | |
| 33 | + public string $value; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * @var bool | |
| 37 | + */ | |
| 38 | + public bool $isDefault; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * @var int | |
| 42 | + */ | |
| 43 | + public int $position; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * @throws ValidationException | |
| 47 | + */ | |
| 48 | + public function __construct( | |
| 49 | + int $id, | |
| 50 | + int $fieldId, | |
| 51 | + string $label, | |
| 52 | + string $value, | |
| 53 | + bool $isDefault, | |
| 54 | + int $position | |
| 55 | + ) { | |
| 56 | + $this->id = $this->validatePositiveInteger($id); | |
| 57 | + $this->fieldId = $this->validatePositiveInteger($fieldId); | |
| 58 | + $this->label = $this->validateString($label, 255, 'label'); | |
| 59 | + $this->value = $this->validateString($value, 255, 'value'); | |
| 60 | + $this->isDefault = $this->validateBool($isDefault); | |
| 61 | + $this->position = $this->validatePositiveInteger($position); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Validates the ID. | |
| 66 | + * | |
| 67 | + * @param int $id | |
| 68 | + * | |
| 69 | + * @return int | |
| 70 | + * @throws ValidationException | |
| 71 | + */ | |
| 72 | + private function validatePositiveInteger(int $id): int | |
| 73 | + { | |
| 74 | + if ($id < 0) { | |
| 75 | + throw new ValidationException(BackendStrings::getExceptionStrings()['property_positive_integer']); | |
| 76 | + } | |
| 77 | + return $id; | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Validates a string value. | |
| 82 | + * | |
| 83 | + * @param string $value | |
| 84 | + * @param int $maxLength | |
| 85 | + * @param string $fieldName | |
| 86 | + * | |
| 87 | + * @return string | |
| 88 | + * | |
| 89 | + * @throws ValidationException | |
| 90 | + */ | |
| 91 | + private function validateString(string $value, int $maxLength, string $fieldName): string | |
| 92 | + { | |
| 93 | + if (strlen($value) > $maxLength) { | |
| 94 | + throw new ValidationException( | |
| 95 | + sprintf( | |
| 96 | + /* translators: 1: String value, 2: String max length. */ | |
| 97 | + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), | |
| 98 | + esc_html($fieldName), | |
| 99 | + $maxLength | |
| 100 | + ) | |
| 101 | + ); | |
| 102 | + } | |
| 103 | + return $value; | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Validates a boolean field. | |
| 108 | + * | |
| 109 | + * @param bool $value | |
| 110 | + * | |
| 111 | + * @return bool | |
| 112 | + */ | |
| 113 | + private function validateBool(bool $value): bool | |
| 114 | + { | |
| 115 | + return $value; | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * @return int | |
| 120 | + */ | |
| 121 | + public function getId(): int | |
| 122 | + { | |
| 123 | + return $this->id; | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * @return int | |
| 128 | + */ | |
| 129 | + public function getFieldId(): int | |
| 130 | + { | |
| 131 | + return $this->fieldId; | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * @return string | |
| 136 | + */ | |
| 137 | + public function getLabel(): string | |
| 138 | + { | |
| 139 | + return $this->label; | |
| 140 | + } | |
| 141 | + | |
| 142 | + /** | |
| 143 | + * @return string | |
| 144 | + */ | |
| 145 | + public function getValue(): string | |
| 146 | + { | |
| 147 | + return $this->value; | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * @return bool | |
| 152 | + */ | |
| 153 | + public function isDefault(): bool | |
| 154 | + { | |
| 155 | + return $this->isDefault; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * @return int | |
| 160 | + */ | |
| 161 | + public function getPosition(): int | |
| 162 | + { | |
| 163 | + return $this->position; | |
| 164 | + } | |
| 165 | + /** | |
| 166 | + * @return array<string, int|string|bool> | |
| 167 | + */ | |
| 168 | + public function toArray(): array | |
| 169 | + { | |
| 170 | + return [ | |
| 171 | + 'id' => $this->getId(), | |
| 172 | + 'fieldId' => $this->getFieldId(), | |
| 173 | + 'label' => $this->getLabel(), | |
| 174 | + 'value' => $this->getValue(), | |
| 175 | + 'isDefault' => $this->isDefault(), | |
| 176 | + 'position' => $this->getPosition(), | |
| 177 | + ]; | |
| 178 | + } | |
| 179 | +} | |