PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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/Notification/Notification.php +313 -0 0.6.1 → 1.5 View file →
@@ -1,0 +1,313 @@
1 +<?php
2 +
3 +namespace IvyForms\ValueObjects\Notification;
4 +
5 +use IvyForms\Common\Exceptions\InvalidArgumentException;
6 +use IvyForms\Common\Exceptions\ValidationException;
7 +use IvyForms\Services\Translations\BackendStrings;
8 +
9 +/**
10 + * Class Notification
11 + *
12 + * @package IvyForms\ValueObjects\Notification
13 + */
14 +final class Notification
15 +{
16 + /**
17 + * @var int
18 + */
19 + public int $id;
20 + /**
21 + * @var string
22 + */
23 + public string $name;
24 + /**
25 + * @var NotificationContact
26 + */
27 + public NotificationContact $contact;
28 + /**
29 + * @var bool
30 + */
31 + public bool $enabled;
32 + /**
33 + * @var string
34 + */
35 + public string $subject;
36 + /**
37 + * @var string
38 + */
39 + public string $message;
40 + /**
41 + * @var bool
42 + */
43 + public bool $showEmptyFields;
44 + /**
45 + * @var array<string, mixed>
46 + */
47 + public array $smartLogic;
48 + /**
49 + * @var int
50 + */
51 + public int $formId;
52 +
53 + /**
54 + * Form constructor.
55 + *
56 + * @param int $id
57 + * @param string $name
58 + * @param NotificationContact $contact
59 + * @param bool $enabled
60 + * @param string $subject
61 + * @param string $message
62 + * @param bool $showEmptyFields
63 + * @param array<string, mixed> $smartLogic
64 + * @param int $formId
65 + * @throws InvalidArgumentException|ValidationException
66 + */
67 + public function __construct(
68 + int $id,
69 + string $name,
70 + NotificationContact $contact,
71 + bool $enabled,
72 + string $subject,
73 + string $message,
74 + bool $showEmptyFields,
75 + array $smartLogic,
76 + int $formId
77 + ) {
78 + $this->id = $this->validateId($id);
79 + $this->name = $this->validateString($name, 255, 'name');
80 + $this->contact = $contact;
81 + $this->enabled = $this->validateBool($enabled);
82 + $this->subject = $this->validateString($subject, 255, 'subject');
83 + $this->message = $this->validateString($message, 4294967295, 'message');
84 + $this->showEmptyFields = $this->validateBool($showEmptyFields);
85 + $this->smartLogic = $this->validateSmartLogic($smartLogic);
86 + $this->formId = $this->validateId($formId);
87 + }
88 +
89 + /**
90 + * Validates the ID.
91 + *
92 + * @param int $id
93 + *
94 + * @return int
95 + * @throws InvalidArgumentException
96 + */
97 + private function validateId(int $id): int
98 + {
99 + if ($id < 0) {
100 + throw new InvalidArgumentException(BackendStrings::getExceptionStrings()['id_positive_integer']);
101 + }
102 + return $id;
103 + }
104 +
105 + /**
106 + * Validates a string value.
107 + *
108 + * @param string $value
109 + * @param int $maxLength
110 + * @param string $fieldName
111 + *
112 + * @return string
113 + *
114 + * @throws ValidationException
115 + */
116 + private function validateString(string $value, int $maxLength, string $fieldName): string
117 + {
118 + if (strlen($value) > $maxLength) {
119 + throw new ValidationException(
120 + sprintf(
121 + /* translators: 1: String value, 2: String max length. */
122 + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
123 + esc_html($fieldName),
124 + $maxLength
125 + )
126 + );
127 + }
128 + return $value;
129 + }
130 +
131 + /**
132 + * Validates a boolean field.
133 + *
134 + * @param bool $value
135 + *
136 + * @return bool
137 + */
138 + private function validateBool(bool $value): bool
139 + {
140 + return $value;
141 + }
142 +
143 + /**
144 + * Get the notification ID.
145 + *
146 + * @return int
147 + */
148 + public function getId(): int
149 + {
150 + return $this->id;
151 + }
152 +
153 + /**
154 + * Get the notification name.
155 + *
156 + * @return string
157 + */
158 + public function getName(): string
159 + {
160 + return $this->name;
161 + }
162 +
163 + /**
164 + * Get the notification sender.
165 + *
166 + * @return string
167 + */
168 + public function getSender(): string
169 + {
170 + return $this->contact->getSender();
171 + }
172 +
173 + /**
174 + * Get the notification reply-to address.
175 + *
176 + * @return string
177 + */
178 + public function getReplyTo(): string
179 + {
180 + return $this->contact->getReplyTo();
181 + }
182 +
183 + /**
184 + * Get the notification receiver.
185 + *
186 + * @return string
187 + */
188 + public function getReceiver(): string
189 + {
190 + return $this->contact->getReceiver();
191 + }
192 +
193 + /**
194 + * Get the notification CC addresses.
195 + *
196 + * @return string
197 + */
198 + public function getCc(): string
199 + {
200 + return $this->contact->getCc();
201 + }
202 +
203 + /**
204 + * Get the notification BCC addresses.
205 + *
206 + * @return string
207 + */
208 + public function getBcc(): string
209 + {
210 + return $this->contact->getBcc();
211 + }
212 +
213 + /**
214 + * Get the notification status.
215 + *
216 + * @return bool
217 + */
218 + public function isEnabled(): bool
219 + {
220 + return $this->enabled;
221 + }
222 +
223 + /**
224 + * Get the notification subject.
225 + *
226 + * @return string
227 + */
228 + public function getSubject(): string
229 + {
230 + return $this->subject;
231 + }
232 +
233 + /**
234 + * Get the notification message.
235 + *
236 + * @return string
237 + */
238 + public function getMessage(): string
239 + {
240 + return $this->message;
241 + }
242 +
243 + /**
244 + * Get the show empty fields status.
245 + *
246 + * @return bool
247 + */
248 + public function isShowEmptyFields(): bool
249 + {
250 + return $this->showEmptyFields;
251 + }
252 +
253 + /**
254 + * Get the smart logic status.
255 + *
256 + * @return array<string, mixed>
257 + */
258 + public function getSmartLogic(): array
259 + {
260 + return $this->smartLogic;
261 + }
262 +
263 + /**
264 + * Normalize smart logic payload structure.
265 + *
266 + * @param array<string, mixed> $smartLogic
267 + * @return array<string, mixed>
268 + */
269 + private function validateSmartLogic(array $smartLogic): array
270 + {
271 + $match = $smartLogic['match'] ?? 'any';
272 +
273 + return [
274 + 'enabled' => (bool)($smartLogic['enabled'] ?? false),
275 + 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any',
276 + 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [],
277 + ];
278 + }
279 +
280 + /**
281 + * Get the form ID associated with the notification.
282 + *
283 + * @return int
284 + */
285 + public function getFormId(): int
286 + {
287 + return $this->formId;
288 + }
289 +
290 + /**
291 + * Convert the notification to an array.
292 + *
293 + * @return array<string, mixed>
294 + */
295 + public function toArray(): array
296 + {
297 + return [
298 + 'id' => $this->getId(),
299 + 'name' => $this->getName(),
300 + 'sender' => $this->getSender(),
301 + 'replyTo' => $this->getReplyTo(),
302 + 'receiver' => $this->getReceiver(),
303 + 'cc' => $this->getCc(),
304 + 'bcc' => $this->getBcc(),
305 + 'enabled' => $this->isEnabled(),
306 + 'subject' => $this->getSubject(),
307 + 'message' => $this->getMessage(),
308 + 'showEmptyFields' => $this->isShowEmptyFields(),
309 + 'smartLogic' => $this->getSmartLogic(),
310 + 'formId' => $this->getFormId(),
311 + ];
312 + }
313 +}