| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © TMS-Plugins. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\ValueObjects\Field; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 16 |
use IvyForms\Common\Exceptions\ValidationException; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Field |
| 20 |
* |
| 21 |
* @package IvyForms\ValueObjects\Field |
| 22 |
*/ |
| 23 |
final class Field |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public int $id; |
| 29 |
/** |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
public int $formId; |
| 33 |
/** |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
public int $fieldIndex; |
| 37 |
/** |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public string $type; |
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public string $label; |
| 45 |
/** |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public string $defaultValue; |
| 49 |
/** |
| 50 |
* @var bool |
| 51 |
*/ |
| 52 |
public bool $required; |
| 53 |
/** |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public string $placeholder; |
| 57 |
/** |
| 58 |
* @var int |
| 59 |
*/ |
| 60 |
public int $position; |
| 61 |
|
| 62 |
/** |
| 63 |
* Field constructor. |
| 64 |
* |
| 65 |
* @param int $id |
| 66 |
* @param int $formId |
| 67 |
* @param int $fieldIndex |
| 68 |
* @param string $type |
| 69 |
* @param string $label |
| 70 |
* @param string $defaultValue |
| 71 |
* @param bool $required |
| 72 |
* @param string $placeholder |
| 73 |
* @param int $position |
| 74 |
* |
| 75 |
* @throws ValidationException |
| 76 |
*/ |
| 77 |
public function __construct( |
| 78 |
int $id, |
| 79 |
int $formId, |
| 80 |
int $fieldIndex, |
| 81 |
string $type, |
| 82 |
string $label, |
| 83 |
string $defaultValue, |
| 84 |
bool $required, |
| 85 |
string $placeholder, |
| 86 |
int $position |
| 87 |
) { |
| 88 |
$this->id = $this->validateId($id); |
| 89 |
$this->formId = $this->validateId($formId) ; |
| 90 |
$this->fieldIndex = $this->validateId($fieldIndex); |
| 91 |
$this->type = $this->validateString($type, 50, 'type'); |
| 92 |
$this->label = $this->validateString($label, 255, 'label'); |
| 93 |
$this->defaultValue = $this->validateString($defaultValue, 255, 'defaultValue'); |
| 94 |
$this->required = $this->validateRequired($required); |
| 95 |
$this->placeholder = $this->validateString($placeholder, 255, 'placeholder'); |
| 96 |
$this->position = $this->validatePosition($position); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Validates the ID. |
| 101 |
* |
| 102 |
* @param int $id |
| 103 |
* |
| 104 |
* @return int |
| 105 |
* @throws ValidationException |
| 106 |
*/ |
| 107 |
public function validateId(int $id): int |
| 108 |
{ |
| 109 |
if ($id < 0) { |
| 110 |
throw new ValidationException("Id must be a positive integer."); |
| 111 |
} |
| 112 |
return $id; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Validates a string value. |
| 117 |
* |
| 118 |
* @param string $value |
| 119 |
* @param int $maxLength |
| 120 |
* @param string $fieldName |
| 121 |
* |
| 122 |
* @return string |
| 123 |
* |
| 124 |
* @throws ValidationException |
| 125 |
*/ |
| 126 |
private function validateString(string $value, int $maxLength, string $fieldName): string |
| 127 |
{ |
| 128 |
if (strlen($value) > $maxLength) { |
| 129 |
throw new ValidationException( |
| 130 |
sprintf( |
| 131 |
/* translators: 1: String value, 2: String max length. */ |
| 132 |
esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 133 |
esc_html($fieldName), |
| 134 |
$maxLength |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
return $value; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Validates the required field. |
| 143 |
* |
| 144 |
* @param bool $value |
| 145 |
* |
| 146 |
* @return bool |
| 147 |
* |
| 148 |
*/ |
| 149 |
private function validateRequired(bool $value): bool |
| 150 |
{ |
| 151 |
return $value; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Validates the position. |
| 156 |
* |
| 157 |
* @param int $value |
| 158 |
* |
| 159 |
* @return int |
| 160 |
* |
| 161 |
* @throws ValidationException |
| 162 |
*/ |
| 163 |
private function validatePosition(int $value): int |
| 164 |
{ |
| 165 |
if ($value < 0) { |
| 166 |
throw new ValidationException("position must be a non-negative integer."); |
| 167 |
} |
| 168 |
return $value; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the form ID. |
| 173 |
* |
| 174 |
* @return int |
| 175 |
*/ |
| 176 |
public function getFormId(): int |
| 177 |
{ |
| 178 |
return $this->formId; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get the field ID. |
| 183 |
* |
| 184 |
* @return int |
| 185 |
*/ |
| 186 |
public function getId(): int |
| 187 |
{ |
| 188 |
return $this->id; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get the field index. |
| 193 |
* |
| 194 |
* @return int |
| 195 |
*/ |
| 196 |
public function getIndex(): int |
| 197 |
{ |
| 198 |
return $this->fieldIndex; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the field type. |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
public function getType(): string |
| 207 |
{ |
| 208 |
return $this->type; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get the field label. |
| 213 |
* |
| 214 |
* @return string |
| 215 |
*/ |
| 216 |
public function getLabel(): string |
| 217 |
{ |
| 218 |
return $this->label; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get the default value. |
| 223 |
* |
| 224 |
* @return string |
| 225 |
*/ |
| 226 |
public function getDefaultValue(): string |
| 227 |
{ |
| 228 |
return $this->defaultValue; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Check if the field is required. |
| 233 |
* |
| 234 |
* @return bool |
| 235 |
*/ |
| 236 |
public function isRequired(): bool |
| 237 |
{ |
| 238 |
return $this->required; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get the field placeholder. |
| 243 |
* |
| 244 |
* @return string |
| 245 |
*/ |
| 246 |
public function getPlaceholder(): string |
| 247 |
{ |
| 248 |
return $this->placeholder; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get the field position. |
| 253 |
* |
| 254 |
* @return int |
| 255 |
*/ |
| 256 |
public function getPosition(): int |
| 257 |
{ |
| 258 |
return $this->position; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Convert the field to an array. |
| 263 |
* |
| 264 |
* @return array<string, mixed> |
| 265 |
*/ |
| 266 |
public function toArray(): array |
| 267 |
{ |
| 268 |
return [ |
| 269 |
'id' => $this->getId(), |
| 270 |
'formId' => $this->getFormId(), |
| 271 |
'fieldIndex' => $this->getIndex(), |
| 272 |
'type' => $this->getType(), |
| 273 |
'label' => $this->getLabel(), |
| 274 |
'defaultValue' => $this->getDefaultValue(), |
| 275 |
'required' => $this->isRequired(), |
| 276 |
'placeholder' => $this->getPlaceholder(), |
| 277 |
'position' => $this->getPosition(), |
| 278 |
]; |
| 279 |
} |
| 280 |
} |
| 281 |
|