PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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
ivyforms / backend / src / ValueObjects / Entry / Entry.php

Entry.php in The Innovative Form Builder – IvyForms 0.3.1, at backend/src/ValueObjects/Entry/Entry.php

253 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Repository\Entry\EntryRequestHelper;
12
13 /**
14 * Class Entry
15 *
16 * @package IvyForms\ValueObjects\Entry
17 */
18 final class Entry
19 {
20 /**
21 * @var int
22 */
23 public int $id;
24 /**
25 * @var int
26 */
27 public int $formId;
28 /**
29 * @var int|null
30 */
31 public ?int $userId = null;
32 /**
33 * @var string
34 */
35 public string $dateCreated;
36 /**
37 * @var string|null
38 */
39 public ?string $dateEdited = null;
40 /**
41 * @var string
42 */
43 public string $status;
44 /**
45 * @var string|null
46 */
47 public ?string $ipAddress = null;
48 /**
49 * @var string|null
50 */
51 public ?string $userAgent = null;
52 /**
53 * @var string|null
54 */
55 public ?string $sourceURL = null;
56 /**
57 * @var bool
58 */
59 public bool $starred = false;
60
61 /**
62 * Entry constructor.
63 *
64 * @param int $id
65 * @param int $formId
66 * @param int|null $userId
67 * @param string $status
68 * @param string|null $ipAddress
69 * @param string|null $userAgent
70 * @param string|null $sourceURL
71 * @throws ValidationException
72 */
73 public function __construct(
74 int $id,
75 int $formId,
76 ?int $userId,
77 string $status,
78 ?string $ipAddress,
79 ?string $userAgent,
80 ?string $sourceURL
81 ) {
82 $this->id = $this->validateId($id);
83 $this->formId = $this->validateId($formId);
84 $this->userId = $this->validateId($userId);
85 $this->dateCreated = gmdate('Y-m-d H:i:s');
86 $this->dateEdited = gmdate('Y-m-d H:i:s');
87 $this->status = $this->validateString($status, 45, 'status');
88 $this->ipAddress = $this->validateString($ipAddress, 45, 'ipAddress');
89 $this->userAgent = $this->validateString($userAgent, 255, 'userAgent');
90 $this->sourceURL = $this->validateString($sourceURL, 255, 'sourceURL');
91 $this->starred = false;
92 }
93
94 /**
95 * Get the ID of the entry.
96 *
97 * @return int The entry ID.
98 */
99 public function getId(): int
100 {
101 return $this->id;
102 }
103
104 /**
105 * Get the form ID associated with the entry.
106 *
107 * @return int The form ID.
108 */
109 public function getFormId(): int
110 {
111 return $this->formId;
112 }
113
114 /**
115 * Get the user ID associated with the entry.
116 *
117 * @return int|null The user ID or null if not set.
118 */
119 public function getUserId(): ?int
120 {
121 return $this->userId;
122 }
123 /**
124 * Get the date the entry was created.
125 *
126 * @return string The creation date in 'Y-m-d H:i:s' format.
127 */
128 public function getDateCreated(): string
129 {
130 return $this->dateCreated;
131 }
132
133 /**
134 * Get the date the entry was last edited.
135 *
136 * @return string|null The last edited date or null if not set.
137 */
138 public function getDateEdited(): ?string
139 {
140 return $this->dateEdited;
141 }
142
143 /**
144 * Get the status of the entry.
145 *
146 * @return string The status.
147 */
148 public function getStatus(): string
149 {
150 return $this->status;
151 }
152
153 /**
154 * Get the IP address associated with the entry.
155 *
156 * @return string|null The IP address or null if not set.
157 */
158 public function getIpAddress(): ?string
159 {
160 return $this->ipAddress;
161 }
162
163 /**
164 * Get the user agent string associated with the entry.
165 *
166 * @return string|null The user agent string or null if not set.
167 */
168 public function getUserAgent(): ?string
169 {
170 return $this->userAgent;
171 }
172 /**
173 * Get the source URL associated with the entry.
174 *
175 * @return string|null The source URL or null if not set.
176 */
177 public function getSourceURL(): ?string
178 {
179 return $this->sourceURL;
180 }
181 /**
182 * Check if the entry is starred.
183 *
184 * @return bool True if the entry is starred, false otherwise.
185 */
186 public function isStarred(): bool
187 {
188 return $this->starred;
189 }
190 /**
191 * Validates the ID.
192 *
193 * @param int $id
194 *
195 * @return int
196 * @throws ValidationException
197 */
198 private function validateId(int $id): int
199 {
200 if ($id < 0) {
201 throw new ValidationException("Id must be a positive integer.");
202 }
203 return $id;
204 }
205 /**
206 * Validates a string value.
207 *
208 * @param string $value
209 * @param int $maxLength
210 * @param string $fieldName
211 *
212 * @return string
213 *
214 * @throws ValidationException
215 */
216 private function validateString(string $value, int $maxLength, string $fieldName): string
217 {
218 if (strlen($value) > $maxLength) {
219 throw new ValidationException(
220 sprintf(
221 /* translators: 1: String value, 2: String max length. */
222 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
223 esc_html($fieldName),
224 $maxLength
225 )
226 );
227 }
228 return $value;
229 }
230
231 /**
232 * Convert the Entry object to an array.
233 *
234 * @return array<mixed> The array representation of the Entry.
235 */
236 public function toArray(): array
237 {
238 return [
239 'id' => $this->getId(),
240 'formId' => $this->getFormId(),
241 'userId' => $this->getUserId(),
242 'author' => get_userdata($this->getUserId())->user_nicename ?? '',
243 'dateCreated' => $this->getDateCreated(),
244 'dateEdited' => $this->getDateEdited(),
245 'status' => $this->getStatus(),
246 'ipAddress' => $this->getIpAddress(),
247 'userAgent' => EntryRequestHelper::getBrowserName($this->getUserAgent()),
248 'sourceURL' => $this->getSourceURL(),
249 'starred' => $this->isStarred(),
250 ];
251 }
252 }
253