PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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 / Notification / Notification.php

Notification.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/ValueObjects/Notification/Notification.php

292 lines 6.5 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\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 status.
195 *
196 * @return bool
197 */
198 public function isEnabled(): bool
199 {
200 return $this->enabled;
201 }
202
203 /**
204 * Get the notification subject.
205 *
206 * @return string
207 */
208 public function getSubject(): string
209 {
210 return $this->subject;
211 }
212
213 /**
214 * Get the notification message.
215 *
216 * @return string
217 */
218 public function getMessage(): string
219 {
220 return $this->message;
221 }
222
223 /**
224 * Get the show empty fields status.
225 *
226 * @return bool
227 */
228 public function isShowEmptyFields(): bool
229 {
230 return $this->showEmptyFields;
231 }
232
233 /**
234 * Get the smart logic status.
235 *
236 * @return array<string, mixed>
237 */
238 public function getSmartLogic(): array
239 {
240 return $this->smartLogic;
241 }
242
243 /**
244 * Normalize smart logic payload structure.
245 *
246 * @param array<string, mixed> $smartLogic
247 * @return array<string, mixed>
248 */
249 private function validateSmartLogic(array $smartLogic): array
250 {
251 $match = $smartLogic['match'] ?? 'any';
252
253 return [
254 'enabled' => (bool)($smartLogic['enabled'] ?? false),
255 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any',
256 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [],
257 ];
258 }
259
260 /**
261 * Get the form ID associated with the notification.
262 *
263 * @return int
264 */
265 public function getFormId(): int
266 {
267 return $this->formId;
268 }
269
270 /**
271 * Convert the notification to an array.
272 *
273 * @return array<string, mixed>
274 */
275 public function toArray(): array
276 {
277 return [
278 'id' => $this->getId(),
279 'name' => $this->getName(),
280 'sender' => $this->getSender(),
281 'replyTo' => $this->getReplyTo(),
282 'receiver' => $this->getReceiver(),
283 'enabled' => $this->isEnabled(),
284 'subject' => $this->getSubject(),
285 'message' => $this->getMessage(),
286 'showEmptyFields' => $this->isShowEmptyFields(),
287 'smartLogic' => $this->getSmartLogic(),
288 'formId' => $this->getFormId(),
289 ];
290 }
291 }
292