| @@ -1,0 +1,149 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\ValueObjects\Entry; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 6 | +use IvyForms\Services\Translations\BackendStrings; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Class EntryField | |
| 10 | + * | |
| 11 | + * Represents a single field value in a form entry. | |
| 12 | + */ | |
| 13 | +final class EntryField | |
| 14 | +{ | |
| 15 | + /** | |
| 16 | + * @var int|null | |
| 17 | + */ | |
| 18 | + public ?int $id; | |
| 19 | + /** | |
| 20 | + * @var int | |
| 21 | + */ | |
| 22 | + public int $fieldId; | |
| 23 | + /** | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + public string $fieldValue; | |
| 27 | + /** | |
| 28 | + * @var int | |
| 29 | + */ | |
| 30 | + public int $entryId; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * EntryField constructor. | |
| 34 | + * | |
| 35 | + * @param int|null $id | |
| 36 | + * @param int $entryId | |
| 37 | + * @param int $fieldId | |
| 38 | + * @param string $fieldValue | |
| 39 | + * @throws ValidationException | |
| 40 | + */ | |
| 41 | + public function __construct( | |
| 42 | + ?int $id, | |
| 43 | + int $entryId, | |
| 44 | + int $fieldId, | |
| 45 | + string $fieldValue | |
| 46 | + ) { | |
| 47 | + $this->id = $id ? $this->validateId($id) : null; | |
| 48 | + $this->entryId = $this->validateId($entryId); | |
| 49 | + $this->fieldId = $this->validateId($fieldId); | |
| 50 | + $this->fieldValue = $this->validateString($fieldValue, 65535, 'fieldValue'); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Get the ID of the entry field. | |
| 55 | + * | |
| 56 | + * @return int|null The entry field ID. | |
| 57 | + */ | |
| 58 | + public function getId(): ?int | |
| 59 | + { | |
| 60 | + return $this->id; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Get the field ID. | |
| 65 | + * | |
| 66 | + * @return int The field ID. | |
| 67 | + */ | |
| 68 | + public function getFieldId(): int | |
| 69 | + { | |
| 70 | + return $this->fieldId; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Get the field value. | |
| 75 | + * | |
| 76 | + * @return string The field value. | |
| 77 | + */ | |
| 78 | + public function getFieldValue(): string | |
| 79 | + { | |
| 80 | + return $this->fieldValue; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Get the entry ID. | |
| 85 | + * | |
| 86 | + * @return int The entry ID or null if not set. | |
| 87 | + */ | |
| 88 | + public function getEntryId(): int | |
| 89 | + { | |
| 90 | + return $this->entryId; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Validates the ID. | |
| 95 | + * | |
| 96 | + * @param int $id | |
| 97 | + * | |
| 98 | + * @return int | |
| 99 | + * @throws ValidationException | |
| 100 | + */ | |
| 101 | + private function validateId(int $id): int | |
| 102 | + { | |
| 103 | + if ($id < 0) { | |
| 104 | + throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); | |
| 105 | + } | |
| 106 | + return $id; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Validates a string value. | |
| 111 | + * | |
| 112 | + * @param string $value | |
| 113 | + * @param int $maxLength | |
| 114 | + * @param string $fieldName | |
| 115 | + * | |
| 116 | + * @return string | |
| 117 | + * | |
| 118 | + * @throws ValidationException | |
| 119 | + */ | |
| 120 | + private function validateString(string $value, int $maxLength, string $fieldName): string | |
| 121 | + { | |
| 122 | + if (strlen($value) > $maxLength) { | |
| 123 | + throw new ValidationException( | |
| 124 | + sprintf( | |
| 125 | + /* translators: 1: String value, 2: String max length. */ | |
| 126 | + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), | |
| 127 | + esc_html($fieldName), | |
| 128 | + $maxLength | |
| 129 | + ) | |
| 130 | + ); | |
| 131 | + } | |
| 132 | + return $value; | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Convert the EntryField object to an array. | |
| 137 | + * | |
| 138 | + * @return array<mixed> The array representation of the EntryField. | |
| 139 | + */ | |
| 140 | + public function toArray(): array | |
| 141 | + { | |
| 142 | + return [ | |
| 143 | + 'id' => $this->getId(), | |
| 144 | + 'entryId' => $this->getEntryId(), | |
| 145 | + 'fieldId' => $this->getFieldId(), | |
| 146 | + 'fieldValue' => $this->getFieldValue(), | |
| 147 | + ]; | |
| 148 | + } | |
| 149 | +} | |