| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Entity\Entry; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Helpers\EntryHelper; |
| 16 |
use IvyForms\ValueObjects\Entry\Entry as EntryValueObject; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Entry |
| 20 |
* |
| 21 |
* @package IvyForms\Entity\Entry |
| 22 |
*/ |
| 23 |
class Entry |
| 24 |
{ |
| 25 |
public const DEFAULT_STATUS = 'unread'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var EntryValueObject |
| 29 |
*/ |
| 30 |
private EntryValueObject $entry; |
| 31 |
|
| 32 |
/** |
| 33 |
* Entry constructor. |
| 34 |
* |
| 35 |
* @param EntryValueObject $entry The entry object. |
| 36 |
*/ |
| 37 |
public function __construct(EntryValueObject $entry) |
| 38 |
{ |
| 39 |
$this->entry = $entry; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the ID of the entry. |
| 44 |
* |
| 45 |
* @return int The entry ID. |
| 46 |
*/ |
| 47 |
public function getId(): int |
| 48 |
{ |
| 49 |
return $this->entry->getId(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Set the ID of the entry. |
| 54 |
* |
| 55 |
* @param int $entryId The entry ID to set. |
| 56 |
*/ |
| 57 |
public function setId(int $entryId): void |
| 58 |
{ |
| 59 |
$this->entry->id = $entryId; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the form ID associated with the entry. |
| 64 |
* |
| 65 |
* @return int The form ID. |
| 66 |
*/ |
| 67 |
public function getFormId(): int |
| 68 |
{ |
| 69 |
return $this->entry->getFormId(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Set the form ID associated with the entry. |
| 74 |
* |
| 75 |
* @param int $formId The form ID to set. |
| 76 |
*/ |
| 77 |
public function setFormId(int $formId): void |
| 78 |
{ |
| 79 |
$this->entry->formId = $formId; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the user ID associated with the entry. |
| 84 |
* |
| 85 |
* @return int|null The user ID or null if not set. |
| 86 |
*/ |
| 87 |
public function getUserId(): ?int |
| 88 |
{ |
| 89 |
return $this->entry->getUserId(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the user ID associated with the entry. |
| 94 |
* |
| 95 |
* @param int|null $userId The user ID to set. |
| 96 |
*/ |
| 97 |
public function setUserId(?int $userId): void |
| 98 |
{ |
| 99 |
$this->entry->userId = $userId; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the date the entry was created. |
| 104 |
* |
| 105 |
* @return string The date the entry was created. |
| 106 |
*/ |
| 107 |
public function getDateCreated(): string |
| 108 |
{ |
| 109 |
return $this->entry->getDateCreated(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the date the entry was last edited. |
| 114 |
* |
| 115 |
* @return string|null The date the entry was last edited or null if not set. |
| 116 |
*/ |
| 117 |
public function getDateEdited(): ?string |
| 118 |
{ |
| 119 |
return $this->entry->getDateEdited(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get the status of the entry. |
| 124 |
* |
| 125 |
* @return string The status of the entry. |
| 126 |
*/ |
| 127 |
public function getStatus(): string |
| 128 |
{ |
| 129 |
return $this->entry->getStatus(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Set the status of the entry. |
| 134 |
* |
| 135 |
* @param string $status The status to set. |
| 136 |
*/ |
| 137 |
public function setStatus(string $status): void |
| 138 |
{ |
| 139 |
$this->entry->status = $status; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the IP address associated with the entry. |
| 144 |
* |
| 145 |
* @return string|null The IP address or null if not set. |
| 146 |
*/ |
| 147 |
public function getIpAddress(): ?string |
| 148 |
{ |
| 149 |
return $this->entry->getIpAddress(); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set the IP address associated with the entry. |
| 154 |
* |
| 155 |
* @param string|null $ipAddress The IP address to set. |
| 156 |
*/ |
| 157 |
public function setIpAddress(?string $ipAddress): void |
| 158 |
{ |
| 159 |
$this->entry->ipAddress = $ipAddress; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get the user agent associated with the entry. |
| 164 |
* |
| 165 |
* @return string|null The user agent or null if not set. |
| 166 |
*/ |
| 167 |
public function getUserAgent(): ?string |
| 168 |
{ |
| 169 |
return $this->entry->getUserAgent(); |
| 170 |
} |
| 171 |
/** |
| 172 |
* Set the user agent associated with the entry. |
| 173 |
* |
| 174 |
* @param string|null $userAgent The user agent to set. |
| 175 |
*/ |
| 176 |
public function setUserAgent(?string $userAgent): void |
| 177 |
{ |
| 178 |
$this->entry->userAgent = $userAgent; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get the source URL of the entry. |
| 183 |
* |
| 184 |
* @return string|null The source URL or null if not set. |
| 185 |
*/ |
| 186 |
public function getSourceURL(): ?string |
| 187 |
{ |
| 188 |
return $this->entry->getSourceURL(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Set the source URL of the entry. |
| 193 |
* |
| 194 |
* @param string|null $sourceURL The source URL to set. |
| 195 |
*/ |
| 196 |
public function setSourceURL(?string $sourceURL): void |
| 197 |
{ |
| 198 |
$this->entry->sourceURL = $sourceURL; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set the starred status of the entry. |
| 203 |
* |
| 204 |
* @param bool $starred True if the entry is starred, false otherwise. |
| 205 |
*/ |
| 206 |
public function setStarred(bool $starred): void |
| 207 |
{ |
| 208 |
$this->entry->starred = $starred; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Check if the entry is starred. |
| 213 |
* |
| 214 |
* @return bool True if the entry is starred, false otherwise. |
| 215 |
*/ |
| 216 |
public function isStarred(): bool |
| 217 |
{ |
| 218 |
return $this->entry->isStarred(); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Convert the entry entity to an array. |
| 223 |
* |
| 224 |
* @param bool $raw If true, returns raw values (for DB), otherwise formatted for response. |
| 225 |
* @return array<string, mixed> The form entity as an array. |
| 226 |
*/ |
| 227 |
public function toArray(bool $raw = false): array |
| 228 |
{ |
| 229 |
return [ |
| 230 |
'id' => $this->getId(), |
| 231 |
'formId' => $this->getFormId(), |
| 232 |
'userId' => $this->getUserId(), |
| 233 |
'author' => get_userdata($this->getUserId())->user_nicename ?? '', |
| 234 |
'dateCreated' => $this->getDateCreated(), |
| 235 |
'dateEdited' => $this->getDateEdited(), |
| 236 |
'status' => $this->getStatus(), |
| 237 |
'ipAddress' => $this->getIpAddress(), |
| 238 |
'userAgent' => $raw ? $this->getUserAgent() : EntryHelper::getBrowserName($this->getUserAgent()), |
| 239 |
'sourceURL' => $this->getSourceURL(), |
| 240 |
'starred' => $this->isStarred(), |
| 241 |
]; |
| 242 |
} |
| 243 |
} |
| 244 |
|