PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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.0, at backend/src/ValueObjects/Notification/Notification.php

275 lines 5.9 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 bool
46 */
47 public bool $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 bool $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 bool $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->validateBool($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 bool
237 */
238 public function isSmartLogic(): bool
239 {
240 return $this->smartLogic;
241 }
242
243 /**
244 * Get the form ID associated with the notification.
245 *
246 * @return int
247 */
248 public function getFormId(): int
249 {
250 return $this->formId;
251 }
252
253 /**
254 * Convert the notification to an array.
255 *
256 * @return array<string, mixed>
257 */
258 public function toArray(): array
259 {
260 return [
261 'id' => $this->getId(),
262 'name' => $this->getName(),
263 'sender' => $this->getSender(),
264 'replyTo' => $this->getReplyTo(),
265 'receiver' => $this->getReceiver(),
266 'enabled' => $this->isEnabled(),
267 'subject' => $this->getSubject(),
268 'message' => $this->getMessage(),
269 'showEmptyFields' => $this->isShowEmptyFields(),
270 'smartLogic' => $this->isSmartLogic(),
271 'formId' => $this->getFormId(),
272 ];
273 }
274 }
275