| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\Entry; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ValidationException; |
| 11 |
use IvyForms\Common\Helpers\EntryHelper; |
| 12 |
use IvyForms\Services\Translations\BackendStrings; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Entry |
| 16 |
* |
| 17 |
* @package IvyForms\ValueObjects\Entry |
| 18 |
*/ |
| 19 |
final class Entry |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
public int $id; |
| 25 |
/** |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public int $formId; |
| 29 |
/** |
| 30 |
* @var int|null |
| 31 |
*/ |
| 32 |
public ?int $userId = null; |
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public string $dateCreated; |
| 37 |
/** |
| 38 |
* @var string|null |
| 39 |
*/ |
| 40 |
public ?string $dateEdited = null; |
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public string $status; |
| 45 |
/** |
| 46 |
* @var string|null |
| 47 |
*/ |
| 48 |
public ?string $ipAddress = null; |
| 49 |
/** |
| 50 |
* @var string|null |
| 51 |
*/ |
| 52 |
public ?string $userAgent = null; |
| 53 |
/** |
| 54 |
* @var string|null |
| 55 |
*/ |
| 56 |
public ?string $sourceURL = null; |
| 57 |
/** |
| 58 |
* @var bool |
| 59 |
*/ |
| 60 |
public bool $starred = false; |
| 61 |
|
| 62 |
/** |
| 63 |
* Entry constructor. |
| 64 |
* |
| 65 |
* @param int $id |
| 66 |
* @param int $formId |
| 67 |
* @param int|null $userId |
| 68 |
* @param string $status |
| 69 |
* @param string|null $ipAddress |
| 70 |
* @param string|null $userAgent |
| 71 |
* @param string|null $sourceURL |
| 72 |
* @throws ValidationException |
| 73 |
*/ |
| 74 |
public function __construct( |
| 75 |
int $id, |
| 76 |
int $formId, |
| 77 |
?int $userId, |
| 78 |
string $status, |
| 79 |
?string $ipAddress, |
| 80 |
?string $userAgent, |
| 81 |
?string $sourceURL |
| 82 |
) { |
| 83 |
$this->id = $this->validateId($id); |
| 84 |
$this->formId = $this->validateId($formId); |
| 85 |
$this->userId = $this->validateUserId($userId); |
| 86 |
$this->dateCreated = gmdate('Y-m-d H:i:s'); |
| 87 |
$this->dateEdited = gmdate('Y-m-d H:i:s'); |
| 88 |
$this->status = $this->validateString($status, 45, 'status'); |
| 89 |
$this->ipAddress = $this->validateString($ipAddress, 45, 'ipAddress'); |
| 90 |
$this->userAgent = $this->validateString($userAgent, 255, 'userAgent'); |
| 91 |
$this->sourceURL = $this->validateString($sourceURL, 255, 'sourceURL'); |
| 92 |
$this->starred = false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the ID of the entry. |
| 97 |
* |
| 98 |
* @return int The entry ID. |
| 99 |
*/ |
| 100 |
public function getId(): int |
| 101 |
{ |
| 102 |
return $this->id; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the form ID associated with the entry. |
| 107 |
* |
| 108 |
* @return int The form ID. |
| 109 |
*/ |
| 110 |
public function getFormId(): int |
| 111 |
{ |
| 112 |
return $this->formId; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the user ID associated with the entry. |
| 117 |
* |
| 118 |
* @return int|null The user ID or null if not set. |
| 119 |
*/ |
| 120 |
public function getUserId(): ?int |
| 121 |
{ |
| 122 |
return $this->userId; |
| 123 |
} |
| 124 |
/** |
| 125 |
* Get the date the entry was created. |
| 126 |
* |
| 127 |
* @return string The creation date in 'Y-m-d H:i:s' format. |
| 128 |
*/ |
| 129 |
public function getDateCreated(): string |
| 130 |
{ |
| 131 |
return $this->dateCreated; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get the date the entry was last edited. |
| 136 |
* |
| 137 |
* @return string|null The last edited date or null if not set. |
| 138 |
*/ |
| 139 |
public function getDateEdited(): ?string |
| 140 |
{ |
| 141 |
return $this->dateEdited; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the status of the entry. |
| 146 |
* |
| 147 |
* @return string The status. |
| 148 |
*/ |
| 149 |
public function getStatus(): string |
| 150 |
{ |
| 151 |
return $this->status; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the IP address associated with the entry. |
| 156 |
* |
| 157 |
* @return string|null The IP address or null if not set. |
| 158 |
*/ |
| 159 |
public function getIpAddress(): ?string |
| 160 |
{ |
| 161 |
return $this->ipAddress; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get the user agent string associated with the entry. |
| 166 |
* |
| 167 |
* @return string|null The user agent string or null if not set. |
| 168 |
*/ |
| 169 |
public function getUserAgent(): ?string |
| 170 |
{ |
| 171 |
return $this->userAgent; |
| 172 |
} |
| 173 |
/** |
| 174 |
* Get the source URL associated with the entry. |
| 175 |
* |
| 176 |
* @return string|null The source URL or null if not set. |
| 177 |
*/ |
| 178 |
public function getSourceURL(): ?string |
| 179 |
{ |
| 180 |
return $this->sourceURL; |
| 181 |
} |
| 182 |
/** |
| 183 |
* Check if the entry is starred. |
| 184 |
* |
| 185 |
* @return bool True if the entry is starred, false otherwise. |
| 186 |
*/ |
| 187 |
public function isStarred(): bool |
| 188 |
{ |
| 189 |
return $this->starred; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get the author name for the entry. |
| 194 |
* |
| 195 |
* @return string The author name or 'Guest' if no user is associated. |
| 196 |
*/ |
| 197 |
public function getAuthor(): string |
| 198 |
{ |
| 199 |
if ($this->getUserId() === null) { |
| 200 |
return esc_html__('Guest', 'ivyforms'); |
| 201 |
} |
| 202 |
|
| 203 |
$userData = get_userdata($this->getUserId()); |
| 204 |
return $userData ? $userData->user_nicename : esc_html__('Guest', 'ivyforms'); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Validates the ID. |
| 209 |
* |
| 210 |
* @param int $id |
| 211 |
* |
| 212 |
* @return int |
| 213 |
* @throws ValidationException |
| 214 |
*/ |
| 215 |
private function validateId(int $id): int |
| 216 |
{ |
| 217 |
if ($id < 0) { |
| 218 |
throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); |
| 219 |
} |
| 220 |
return $id; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Validates the user ID (nullable). |
| 225 |
* |
| 226 |
* @param int|null $userId |
| 227 |
* |
| 228 |
* @return int|null |
| 229 |
* @throws ValidationException |
| 230 |
*/ |
| 231 |
private function validateUserId(?int $userId): ?int |
| 232 |
{ |
| 233 |
if ($userId === null) { |
| 234 |
return null; |
| 235 |
} |
| 236 |
if ($userId < 0) { |
| 237 |
throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); |
| 238 |
} |
| 239 |
return $userId; |
| 240 |
} |
| 241 |
/** |
| 242 |
* Validates a string value (nullable). |
| 243 |
* |
| 244 |
* @param string|null $value |
| 245 |
* @param int $maxLength |
| 246 |
* @param string $fieldName |
| 247 |
* |
| 248 |
* @return string|null |
| 249 |
* |
| 250 |
* @throws ValidationException |
| 251 |
*/ |
| 252 |
private function validateString(?string $value, int $maxLength, string $fieldName): ?string |
| 253 |
{ |
| 254 |
if ($value === null) { |
| 255 |
return null; |
| 256 |
} |
| 257 |
if (strlen($value) > $maxLength) { |
| 258 |
throw new ValidationException( |
| 259 |
sprintf( |
| 260 |
/* translators: 1: String value, 2: String max length. */ |
| 261 |
esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 262 |
esc_html($fieldName), |
| 263 |
$maxLength |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
return $value; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Convert the Entry object to an array. |
| 272 |
* |
| 273 |
* @return array<mixed> The array representation of the Entry. |
| 274 |
*/ |
| 275 |
public function toArray(): array |
| 276 |
{ |
| 277 |
return [ |
| 278 |
'id' => $this->getId(), |
| 279 |
'formId' => $this->getFormId(), |
| 280 |
'userId' => $this->getUserId(), |
| 281 |
'author' => $this->getAuthor(), |
| 282 |
'dateCreated' => $this->getDateCreated(), |
| 283 |
'dateEdited' => $this->getDateEdited(), |
| 284 |
'status' => $this->getStatus(), |
| 285 |
'ipAddress' => $this->getIpAddress(), |
| 286 |
'userAgent' => EntryHelper::getBrowserName($this->getUserAgent()), |
| 287 |
'sourceURL' => $this->getSourceURL(), |
| 288 |
'starred' => $this->isStarred(), |
| 289 |
]; |
| 290 |
} |
| 291 |
} |
| 292 |
|