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